Ember数组:`removeObject`上没有渲染

时间:2014-10-17 11:28:52

标签: javascript data-binding ember.js

我创建了Ember.ArrayProxy,如下所示:

Em.ArrayProxy.createWithMixins(Em.Array, Em.MutableArray, Em.SortableMixin, {
  content         : Em.A(arr),
  sortProperties  : properties,
  sortAscending   : fn ? fn : void(0)
}).reopen({
  isEmpty: function(){
    return this.get('length') < 1;
  }
});

当我在数组上使用pushObjectsortBy时,数据绑定非常有效。但是当我在这个数组上使用removeObject时,我没有更新html。我怎么能解决这个问题?

我的琥珀:

DEBUG: -------------------------------
DEBUG: Ember      : 1.7.1+pre.c1ec09ad
DEBUG: Ember Data : 1.0.0-beta.9
DEBUG: Handlebars : 1.3.0
DEBUG: jQuery     : 2.0.3
DEBUG: ------------------------------- 



<小时/>

UPD:

从列表中删除对象时,我注意到了非常奇怪的行为: console output



templates/order/list.handlebars中有一个简化的模板:

<ul class="scrollable_list">
  {{#each group in orders}}
    <li class="group">
      <div class="group_title">
        Orders at {{format_date group.date f="DD.MM.YYYY"}}
        <div class="label">{{group.count}}</div>
      </div>
    </li>
    <ul>
    {{#each order in group.orders}}
      <li {{bind-attr class=':order order.is_mine order.is_claimed'}}>
        {{#link-to 'orders.group.show' controller.group order}}
          {{partial 'order/item'}}
        {{/link-to}}
      </li>
    {{/each}}
    </ul>
  {{/each}}
</ul>



对象ordersArrayProxy的{​​{1}}。数组由Ember.Object创建。数组中的每个对象都具有以下结构:

this.create_sortable_array

属性Em.Object.create({ date : date, time : date.getTime(), orders : this.create_orders_array() }) 也是orders。每个项目都是ArrayProxy个实例。

方法:



this.create_orders_array

DS.Model



this.create_sortable_array

create_orders_array: function(){
  return this.create_sortable_array([], ['id'], true, function(a, b){
    a = Number(a);
    b = Number(b);
    return a > b ? 1 : -1
  });
}



模板将由路由器在create_sortable_array: function(arr, properties, desc, fn){ desc = (typeof desc == 'undefined' || desc == null) ? false : desc; fn = fn || void(0); return Em.ArrayProxy.createWithMixins( Em.MutableArray, Em.MutableEnumerable, Em.SortableMixin, { content : Em.A(arr), sortProperties : properties, sortAscending : fn ? fn : void(0) }).reopen({ isEmpty: function(){ return this.get('length') < 1 } }); } 呈现:

{{outlet orders}}

2 个答案:

答案 0 :(得分:0)

看起来它对我来说很好。您可能根本没有从列表中删除该项目。

var arr = [{a:'saf'},{a:'fdsa'},{a:'asd'}];
var foo = Em.ArrayProxy.createWithMixins(Em.Array, Em.MutableArray, Em.SortableMixin, {
  content         : arr,
  sortProperties  : ['a'],
  sortAscending   : true
}).reopen({
  isEmpty: function(){
    return this.get('length') < 1;
  }
});


App.IndexRoute = Ember.Route.extend({
  model: function() {
    Em.run.later(function(){
      foo.removeObject(foo.objectAt(1));
    },2000);
    return foo;
  }
});

http://emberjs.jsbin.com/bufoca/1/edit

答案 1 :(得分:0)

问题在于第三方库使用它自己的方法替换removeAt Array.prototype方法。因此,如果Ember从数组中删除元素,它会使用它自己的removeAt,后者被另一个替换。在这种情况下,未调用某些Ember挂钩,并且突变机制无法检测到任何更改。感谢大家的帮助和建议。

相关问题