Created
August 29, 2013 11:22
-
-
Save b1ff/6376862 to your computer and use it in GitHub Desktop.
This file contains 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
__patchItemsProperty: function (excludeIds) { | |
if (!ko.isObservable(excludeIds)) { | |
throw new Error('Expected exclude ids to be observable array'); | |
}; | |
var self = this, | |
oldItems = {}; // in this hash will be stored all files, key-> file.Id(), value -> file | |
self.__fillHashFromItems(oldItems, self.items()); | |
self.__filterItems(excludeIds(), oldItems); | |
excludeIds.subscribe(function (newIds) { | |
self.__filterItems(newIds, oldItems); | |
}); | |
// NOTE (EM): please don't kick me for next code | |
// this code make subscribe on "items" update and rebuild hash with all files | |
// after this on first items notification has will be rebuilded | |
// to avoid recursion previous subscribtion will diposed and hash rebuild re-subscribe | |
var subscription = self.__subscribeHashToItemsUpdate(oldItems, function() { | |
subscription.dispose(); | |
self.__filterItems(excludeIds(), oldItems); | |
self.__subscribeHashToItemsUpdate(oldItems, _.identity); | |
}); | |
}, | |
__subscribeHashToItemsUpdate: function(oldItems, externalHandle) { | |
var self = this; | |
return self.items.subscribe(function (newItems) { | |
self.__fillHashFromItems(oldItems, newItems); | |
externalHandle.call(self, newItems); | |
}); | |
}, | |
__fillHashFromItems: function (hash, items) { | |
_.each(items, function (item) { | |
hash[item.Id()] = item; | |
}); | |
}, | |
__filterItems: function (idsToExclude, oldItems) { | |
var filteredItems = _.chain(oldItems) | |
.filter(function (file, key) { return !_.contains(idsToExclude, key); }) | |
.map(function (file) { return file; }) | |
.value(); | |
this.items(filteredItems); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment