简而言之,我正在创建一个可排序的表,可以根据项目是 new ,更新还是需要删除来过滤。 FilteredParts
属性需要同时观察query
,sortBy
和model.@each.isDirty
,以确保表格是最新的。更改过滤器和排序很快,但是如果我想更新集合中的任何模型,它会非常慢。有没有办法使用model.@each.isDirty
或类似的东西,以免它不那么费力?
这是我的控制器:
App.VendorPartsController = Ember.ArrayController.extend(App.PartTableMixin,{
queryParams: ['query','sortBy'],
query: "all",
hasChanges: function(){
if(this.get('filteredParts').isAny('isDirty',true))
{
return true;
}
return false;
}.property('filteredParts.@each.isDirty'),
hasNew: function() {
if(this.get('filteredParts').isAny('isNew',true))
{
return true;
}else{
return false;
}
}.property('filteredParts.@each.isNew'),
hasUpdated: function() {
var parts = this.get('filteredParts');
return parts.any(function(part){
return part.get('isDirty') && !(part.get('isNew')) && !(part.get('deletable'));
});
}.property('filteredParts.@each.isDirty'),
hasDeleted: function() {
if(this.get('filteredParts').isAny('deletable',true))
{
return true;
}
else
{
return false;
}
}.property('filteredParts.@each.deletable'),
filteredParts: function(){
var parts = this.get('model');
var query = this.get('query');
var sortBy = this.get('sortBy');
switch(query){
case "new":
return parts.filterProperty('isNew',true).sortBy(sortBy);
break;
case "updated":
return parts.filter(function(part){
return part.get('isDirty') && !(part.get('isNew')) && !(part.get('deletable'));
}).sortBy(sortBy);
break;
case "deleted":
return parts.filterProperty('deletable',true).sortBy(sortBy);
break;
case "all":
return parts.sortBy(sortBy);
break;
}
}.property('query','sortBy','model.@each.isDirty'),
actions: {
undoAll: function() {
this.get('model').forEach(function(part){
part.rollback();
})
this.transitionToRoute('vendor.parts');
}
}
})
这是一个我可能会添加回控制器的mixin:
App.PartTableMixin = Ember.Mixin.create({
sortAscending: false,
sortBy: 'vendor_part_number',
outOfParts: function()
{
if(this.get('filteredParts').length < 1)
{
return true;
}else
{
return false;
}
}.property('filteredParts.length'),
actions: {
trash: function(model){
if(model.get('isNew'))
{
model.rollback();
}
else
{
model.set('deletable',true);
}
},
undo: function(model) {
model.rollback();
}
}
});