Skip to content

Instantly share code, notes, and snippets.

@go1dfish
Created October 2, 2012 02:45
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save go1dfish/3815888 to your computer and use it in GitHub Desktop.
Save go1dfish/3815888 to your computer and use it in GitHub Desktop.
Ember.js ArrayProxy based ArrayFilter implementation
Ember.ArrayFilter = Em.ArrayProxy.extend({
init: function() {
this._filterContentDidChange();
this._super();
},
arrangedContent: function() {
var af = this;
return Ember.ArrayFilterSortProxy.create({
arrayFilter: af,
sortableBinding: 'arrayFilter.content',
init: function() {
this.set('content', Em.A([]));
}
});
}.property(),
filterItem: function(item) {
return true;
},
refilter: function() {
var arrangedContent = this.get('arrangedContent.content'),
content = this.get('content'),
contentLength = this.get('content.length'),
indexMap = this.get('indexMap');
for (var i=0; i<contentLength; i++) {
var item = content.objectAt(i);
this.updateItem(item);
}
},
_filterContentDidChange: function() {
this.refilter();
}.observes('content'),
updateItem: function(item) {
var arrangedContent = this.get('arrangedContent.content');
var shouldBeInArray = this.filterItem(item);
var itemIndex = arrangedContent.indexOf(item);
var alreadyInArray = itemIndex != -1;
if (shouldBeInArray && !alreadyInArray) {
arrangedContent.pushObject(item);
} else if (!shouldBeInArray && alreadyInArray) {
arrangedContent.removeObject(item);
}
},
contentArrayWillChange: function(content, idx, removedCnt, addedCnt) {
var arrangedContent = this.get('arrangedContent.content');
if (removedCnt) {
for (var i=idx; i<idx + removedCnt; i++) {
arrangedContent.removeObject(content.objectAt(i));
}
}
},
contentArrayDidChange: function(content, idx, removedCnt, addedCnt) {
this._super(content, idx, removedCnt, addedCnt);
var removedEndIdx = idx + removedCnt,
indexMap = this.get('indexMap'),
arrangedContent = this.get('arrangedContent.content'),
indexMapInsertIdx = -1;
if (addedCnt) {
for (var i=idx; i<idx + addedCnt; i++) {
this.updateItem(content.objectAt(i));
}
}
}
});
Ember.ArrayFilterSortProxy = Em.ArrayProxy.extend(Ember.SortableMixin, {
sortPropertiesBinding: 'sortable.sortProperties'
});
Ember.ArrayFilter.property = function(arrayPath, filterFunction, filterObservers) {
var observers = Em.A([]);
filterObservers = filterObservers || Em.A([]);
filterObservers.forEach(function(item) {
observers.pushObject('bindContext.' + item);
});
arrayPath = 'bindContext.' + arrayPath;
var filterDidChange = function() {
this.refilter();
};
filterDidChange.observes.apply(filterDidChange, observers);
return function() {
return Ember.ArrayFilter.create({
bindContext: this,
contentBinding: arrayPath,
filterDidChange: filterDidChange,
filterItem: filterFunction,
get: function(key) {
var result = this._super(key);
if (typeof result != 'undefined') {
return result;
}
return this._super('bindContext.' + key);
}
});
}.property();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment