通过find更新关系或其他数据

时间:2013-05-10 14:12:50

标签: ember.js

编辑:更一般地说,如何调用find()并让它对结果集中的每个项执行某些操作

如何在不破坏绑定的情况下更新集合关系的成员?例如。一个模型有很多东西,但最初只加载前10个,然后我想用下一个替换那些等等,用于分页

我可以调用clear并添加底层的OrderedSet,但是会破坏我页面上的绑定,一切都会消失。

App.Category = DS.Model.extend({
  name: DS.attr('string'),
  type: DS.attr("string"),
  parent: DS.belongsTo('App.Category'),
  children: DS.hasMany('App.Category'),
  stories: DS.hasMany('App.Story'),

});

App.CategoryController = Ember.ObjectController.extend({
  page: 1,

  loadPage: function() {
    console.log(this.get('stories'))
    var s = App.Story.find({sid: this.get('id'), count: 12, page: this.get('page')})
    this.get('stories').clear()
    for (var i = 0, len = this.length; i < len; ++i) {
      this.get('stories').add(s[i])
    }
  },
  nextPage: function(event) {
    this.set('page', this.get('page') + 1)
    this.loadPage()

  },
  prevPage: function(events) {
    this.set('page', this.get('page') - 1)
    this.loadPage()
  },
  breadcrumb: function() {
    return ""
  }.property('parent'),
  sortProperties: ['created'],
  sortAscending: false,
  nextEnabled: function() {
    return true
  }.property('page'),
  prevEnabled: function() {
    if (this.get('page') > 1) {
      return true
    }
    return false
  }.property('page'),

});

2 个答案:

答案 0 :(得分:0)

通常,ORM不提供父对象中子级的分页。解决这个问题的常用方法是打破对象模型并为自己维护一个父对象和单独的可分页子对象集合。换句话说,您必须打破绑定,然后为了此页面的目的自己管理它们。

答案 1 :(得分:0)

我建议您执行以下操作,而不是绑定到在更改页面时清空和重新填充的单个数组:

  1. 将当前页面索引存储在控制器的page属性中(已完成)
  2. 将每个查询所需的项目数存储在控制器的count属性中(已完成)
  3. 创建一个取决于pageContentpage的计算属性count。它应该返回带有这些参数的find()查询
  4. 作为奖励,请pageContent缓存每个查询的效果
  5. 您的模型和控制器看起来像这样:

    App.Post = DS.Model.extend({
        title: DS.attr('string')
    });
    
    App.IndexController = Ember.Controller.extend({
        cache: [],
        counts: [5, 10, 20, 50],
    
        page: 0,
        count: 5,
    
        pageContent: function() {
            var page = this.get('page');
            var count = this.get('count');
            var key = this.keyFor(page, count);
    
            var cache = this.get('cache');
            if (!cache[key]) {
                // we don't have this pair of index and count cached,
                // so retrieve data from the api and cache it
                cache[key] = App.Post.find({page: page, count: count});
            }
    
            return cache[key];
        }.property('page', 'count'),
    
        // generates hash key for page and count pair
        keyFor: function(page, count) {
            return page + '-' + count;
        },
    
        // actions                                            
        next: function() {
            this.incrementProperty('page');
        },
    
        previous: function() {
            if(this.get('page') > 0) {
                this.decrementProperty('page');
            }
        }               
    });
    

    现在,在您的模板中,您绑定到pageContent

    <script type="text/x-handlebars" data-template-name="index">
        {{#each pageContent}}
            <p>-{{title}}</p>
        {{/each}}
    
        <button {{action previous}}>Previous</button>
        <button {{action next}}>Next</button>
        <br/>
    
        {{view Ember.Select contentBinding="counts" selectionBinding="count"}}
        items per page
    </script>
    

    Here's the whole thing running in a jsfiddle