【发布时间】:2014-10-17 11:28:52
【问题描述】:
我有一个这样创建的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;
}
});
当我在数组上使用pushObject 或sortBy 时,数据绑定工作得很好。但是当我在这个数组上使用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:
从列表中删除对象时,我注意到非常奇怪的行为:
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>
对象orders 是Ember.Object 的ArrayProxy。数组由this.create_sortable_array 创建。数组中的每个对象都有这样的结构:
Em.Object.create({
date : date,
time : date.getTime(),
orders : this.create_orders_array()
})
属性orders 也是ArrayProxy。每个项目都是DS.Model 实例。
方法:
this.create_orders_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
});
}
this.create_sortable_array:
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}}:
App.OrdersRoute = App.AppRoute.extend({
renderTemplate: function(){
this._super()
this.render('order/list', into: 'orders', controller: 'orders')
this.render({'order/show', into: 'orders', outlet: 'main_view'})
this.render('order/smart_search', into: 'application', outlet: 'smart_search', controller: 'orders')
}
});
【问题讨论】:
标签: javascript data-binding ember.js