Created
October 10, 2015 17:55
-
-
Save ostgals/a84364d366b0b8908d85 to your computer and use it in GitHub Desktop.
Knockout - Lazy data loading into observables
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
// 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