【问题标题】:ember model find query with params doesn't display on pagination带有参数的ember模型查找查询不会显示在分页上
【发布时间】:2015-02-05 01:11:57
【问题描述】:

2我有一个 Ember 应用程序,它连接到一个获取文章的 api。我利用分页来获得每个请求 10 篇文章。这行得通。但现在我想在请求中添加排序。我通过使用 store.find 中的额外参数来实现这一点。

但是,由于某种原因,如果我使用 'return this.store.find('article', params);'而不是'return this.store.find('article');' getMore 函数中的新文章(仍然请求并正确添加到商店!)未显示或呈现。但是当我从模型中的 store.find 中删除 params 参数时,它确实有效。这里可能是什么情况?

模板/articles.hbs

<script type="text/x-handlebars" data-template-name="articles">

      {{#each itemController="article"}}

          <div class="item">
              //...
          </div>
      {{/each}}

    </script>

路由/articles.js

import Ember from 'ember';

export default Ember.Route.extend(Ember.UserApp.ProtectedRouteMixin, {
  model: function(params) {

    var params2 = {page: 1, per_page: 10, sort: params.sort};
    return this.store.find('article', params2);
  },
  setupController: function(controller, model) {
    controller.set('content', model);
  },
  actions:{
    //...
    },

    getMore: function() {
      // don't load new data if we already are
      //if (this.get('loadingMore')) return;

      //this.set('loadingMore', true);
      var meta = this.store.metadataFor("article");
      if (meta.hasmore) {

        var controller = this.get('controller'),
          nextPage = controller.get('page') + 1,
          perPage = controller.get('perPage'),
          sorting = controller.get('sort'),
          items;

        var params = {page: nextPage, per_page: perPage, sort: sorting};

        this.store.findQuery('article', params).then(function (articles) {
          controller.set('page', controller.get('page') + 1);
          //this.set('loadingMore', false);
        });
      }
      else{
        $('#pagination_spinner').hide();
      }
    },
    queryParamsDidChange: function() {
      this.refresh();
    }

  }

});

控制器/articles.js

import Ember from 'ember';

var ArticlesController = Ember.ArrayController.extend({
  itemController: 'article',
  queryParams: ['sort'],
  sort: 'rating',

  page: 1,
  perPage: 10


});

export default ArticlesController;

views/articles.js

import Ember from 'ember';

export default Ember.View.extend({

  didInsertElement: function(){
    //this.scheduleMasonry();
    this.applyMasonry();
    // we want to make sure 'this' inside `didScroll` refers
    // to the IndexView, so we use jquery's `proxy` method to bind it
    //this.applyMasonry();
    $(window).on('scroll', $.proxy(this.didScroll, this));

  },

  willDestroyElement: function(){
    this.destroyMasonry();
    // have to use the same argument to `off` that we did to `on`
    $(window).off('scroll', $.proxy(this.didScroll, this));
  },

  // this is called every time we scroll
  didScroll: function(){
    if (this.isScrolledToBottom()) {
      $('#pagination_spinner').addClass('active');
      this.get('controller').send('getMore');
    }
  },

  scheduleMasonry: (function(){
    Ember.run.scheduleOnce('afterRender', this, this.applyMasonry);
  }).observes('controller.model.@each'), //TODO check

  applyMasonry: function(){
    $('#pagination_spinner').removeClass('active');

    var $galleryContainer = $('#galleryContainer');
    $galleryContainer.imagesLoaded(function() {
      // check if masonry is initialized
      var msnry = $galleryContainer.data('masonry');
      if ( msnry ) {
        msnry.reloadItems();
        // disable transition
        var transitionDuration = msnry.options.transitionDuration;
        msnry.options.transitionDuration = 0;
        msnry.layout();
        // reset transition
        msnry.options.transitionDuration = transitionDuration;
      } else {
        // init masonry
        $galleryContainer.masonry({
          itemSelector: '.item',
          columnWidth: 0,
          "isFitWidth": true
        });
      }
    });
  },

  destroyMasonry: function(){
    $('#galleryContainer').masonry('destroy');
  },

  // we check if we are at the bottom of the page
  isScrolledToBottom: function(){
    var distanceToViewportTop = (
    $(document).height() - $(window).height());
    var viewPortTop = $(document).scrollTop();

    if (viewPortTop === 0) {
      // if we are at the top of the page, don't do
      // the infinite scroll thing
      return false;
    }

    return (viewPortTop - distanceToViewportTop === 0);
  }
});

【问题讨论】:

    标签: sorting ember.js pagination ember-data


    【解决方案1】:

    我没有想到什么聪明的事,但也许就是这样......

    你已经明白了:

    if (meta.hasmore) {
    

    在您的 getMore() 函数中。是不是您在一个响应中有这个 meta 字段而在另一个响应中忘记了?

    【讨论】:

    • 不,不会改变任何东西。行:controller.set('page', controller.get('page') + 1);执行正确,但是刚刚添加到商店的文章没有显示出来。
    • hbs中的表是怎么实现的?也许你只是缺少合适的听众?
    • 我将它插入到我的帖子中。我的 {{#each} 是否正确实施?因为那应该监听商店的变化,对吧?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多