Created
December 11, 2013 15:33
-
-
Save mixonic/7912473 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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