【问题标题】:Use filter.js streaming with Wordpress JSON API将 filter.js 流与 Wordpress JSON API 一起使用
【发布时间】:2014-10-12 09:34:33
【问题描述】:

我的 filter.jsWordpress JSON API 配合得很好,但我不得不告诉 JSON API 输出所有帖子,但指定 &count=-1(通常它们在 10 秒内分页)。

虽然帖子数量不多,但这很好,但随着添加的帖子越多,Wordpress JSON API 生成 JSON 所需的时间就越长。

filter.js 提供流式传输,它可以抓取 JSON 文件的块并逐步将它们添加到页面中。

真正的问题:

如何获取“流式传输”(其 AJAX 请求格式为 .json?offset=0&limit=50)以使用 Wordpress JSON API 分页结果?每个新页面都需要一个新的Wordpress JSON API 调用&page=2 等。

以下是我目前拥有的相关代码,但您可以在此处的粘贴箱中找到所有代码:http://pastebin.com/EKhBddmh

apiLocation: '/api/get_posts/?post_type=business&count=-1',

settings: {
  filter_on_init: true,
  filter_criteria: {
    location: ['.js__filter-location .TYPE.any', 'taxonomy_business_location.ARRAY.slug'],
    type: ['.js__filter-type .TYPE.any', 'taxonomy_business_type.ARRAY.slug']
  },
  search: {
    input: '#filterSearch',
    search_in: '.media__title, .media__body, .media__footer'
  },
  filter_types: {
    any: function( current_value, option ){
      if ( current_value === '') { return true; }
      else { return current_value === option; }
    }
  },

  streaming: {
    data_url: filter.apiLocation,
    stream_after: 1,
    batch_size: 10
  },
}

init: function(){
  return FilterJS( filter.get_api_data( filter.apiLocation ).posts, '#resultsList', filter.view, filter.settings );
}


get_api_data: function( api_location ){

  var data;

  $.ajax({
    async: false, //thats the trick
    url: api_location,
    dataType: 'json',
    success: function( response ){
       data = response;
    }
  });

  return data;

},

【问题讨论】:

    标签: jquery wordpress performance filtering json-api


    【解决方案1】:

    我还没有测试过这个,但是这样的东西应该可以工作......

    尝试添加这些配置更改。 Filter.js 会将params 选项作为查询参数添加到请求中:

    filter = {
    
        filterCount: 0,
        apiLocation: '/api/get_posts',
    
        settings: {
    
            ... keep current settings, including streaming parameter ...
    
            params: {
                'post_type': 'business',
                'page': 1,
                'count': 10
            },
            callbacks: {
                after_filter: function( result ){
    
                    ... keep current function ...
    
                },
                before_add: function() {
                    filter.settings.params.page++;
                }
            }
        }
    }
    

    删除get_api_data 函数(让filter.js 处理ajax 请求)并将init 更改为:

    init: function(){
    
        if ( $('body').hasClass('view-map') ) {
            window.googleMap.init();
        }
    
        filter.bind();
        filter.create_results();
    
        return FilterJS([], '#resultsList', filter.view, filter.settings);
    }
    

    【讨论】:

    • 感谢您的帮助。据我所知,params 从未被使用过。没有加载任何内容,也没有控制台错误。
    • 文档没有提及params 对象。
    • 没错,但是filter.js的fetchData函数使用了params = this.options.params || {}。在 ajax 调用中使用:$.getJSON(opts.data_url, params)。因为this.options 等于filter.settings,所以params 选项应该传递给ajax 调用。你用的是最新的 filter.js 吗?
    • 我使用的是最新的 1.5.2。这是我根据您的建议实现的代码:pastebin.com/ibZ3UT8X
    • 哪些请求被发送到服务器? (检查您的浏览器控制台)。您还应该添加 count 参数,请参阅答案。
    猜你喜欢
    • 1970-01-01
    • 2013-06-23
    • 1970-01-01
    • 2017-06-05
    • 1970-01-01
    • 2018-07-18
    • 2017-12-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多