Skip to content

Instantly share code, notes, and snippets.

@mixonic
Created December 11, 2013 15:33
Show Gist options
  • Save mixonic/7912473 to your computer and use it in GitHub Desktop.
Save mixonic/7912473 to your computer and use it in GitHub Desktop.
// Ember.computed.slice managing the array
Ember.computed.slice = function(dependentKey, begin, end) {
var size = end - begin,
slicedIndex;
var options = {
addedItem: function(array, item, changeMeta, instanceMeta) {
slicedIndex = changeMeta.index - begin;
if (slicedIndex >= 0 && slicedIndex < size) {
if (slicedIndex >= array.length) {
array.push(item);
} else {
array.insertAt(changeMeta.index - begin, item);
}
if (array.length > size) {
array.pop();
}
}
return array;
},
removedItem: function(array, item, changeMeta, instanceMeta) {
slicedIndex = changeMeta.index - begin;
if (slicedIndex >= 0 && slicedIndex < size) {
array.removeAt(slicedIndex);
if (changeMeta.arrayChanged.length > size) {
var fill = changeMeta.arrayChanged[end];
if (fill) { array.push(fill); }
}
}
return array;
}
};
return Ember.arrayComputed(dependentKey, options);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment