Update ember-model

This commit is contained in:
Piotr Sarnacki 2014-01-28 22:23:57 +01:00
parent c808d3d3b8
commit 5562e72492

View File

@ -196,6 +196,8 @@ Ember.FilteredRecordArray = Ember.RecordArray.extend({
throw new Error('FilteredRecordArrays must be created with filterProperties'); throw new Error('FilteredRecordArrays must be created with filterProperties');
} }
this._recordsCache = Ember.A([]);
var modelClass = get(this, 'modelClass'); var modelClass = get(this, 'modelClass');
modelClass.registerRecordArray(this); modelClass.registerRecordArray(this);
@ -233,10 +235,16 @@ Ember.FilteredRecordArray = Ember.RecordArray.extend({
}); });
}, },
recordIsObserved: function(record) {
return this._recordsCache.contains(record);
},
registerObserversOnRecord: function(record) { registerObserversOnRecord: function(record) {
var self = this, var self = this,
filterProperties = get(this, 'filterProperties'); filterProperties = get(this, 'filterProperties');
this._recordsCache.pushObject(record);
for (var i = 0, l = get(filterProperties, 'length'); i < l; i++) { for (var i = 0, l = get(filterProperties, 'length'); i < l; i++) {
record.addObserver(filterProperties[i], self, 'updateFilterForRecord'); record.addObserver(filterProperties[i], self, 'updateFilterForRecord');
} }
@ -1023,17 +1031,18 @@ Ember.Model.reopenClass({
return record; return record;
}, },
addToRecordArrays: function(record) { addToRecordArrays: function(record) {
if (this._findAllRecordArray) { if (this._findAllRecordArray && !this._findAllRecordArray.contains(record)) {
this._findAllRecordArray.pushObject(record); this._findAllRecordArray.pushObject(record);
} }
if (this.recordArrays) { if (this.recordArrays) {
this.recordArrays.forEach(function(recordArray) { this.recordArrays.forEach(function(recordArray) {
if (recordArray instanceof Ember.FilteredRecordArray) { if (recordArray instanceof Ember.FilteredRecordArray) {
recordArray.registerObserversOnRecord(record); if(!recordArray.recordIsObserved(record)) {
recordArray.updateFilterForRecord(record); recordArray.registerObserversOnRecord(record);
} else { recordArray.updateFilterForRecord(record);
}
} else if(!recordArray.contains(record)) {
recordArray.pushObject(record); recordArray.pushObject(record);
} }
}); });