Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Knockout - Lazy data loading into observables
// thanks to rniemeyer!
// http://www.knockmeout.net/2011/06/lazy-loading-observable-in-knockoutjs.html
ko.extenders.lazy = function(target, callback){
var result = ko.computed({
read: function(){
if (!result.loaded()) callback.call(context);
return target();
},
write: function(newValue){
result.loaded(true);
target(newValue);
},
deferEvaluation: true
});
result.loaded = ko.observable(false);
result.refresh = function(){ result.loaded(false); };
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.