1

我有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;

},
4

1 回答 1

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);
}
于 2014-08-28T13:21:22.063 回答