var fakeText = " - lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ut tempus lorem. Etiam laoreet volutpat viverra. Vestibulum non nunc eu justo adipiscing rhoncus et eget leo. Ut tincidunt, orci a tristique aliquet, ipsum leo rutrum nisi, ac scelerisque leo magna quis justo. In quis lacus a tortor lacinia euismod et ut lectus. Quisque suscipit iaculis lacus. Pellentesque varius volutpat lacus, ac semper arcu porttitor sit amet. In feugiat pharetra laoreet. Nam condimentum gravida suscipit. Pellentesque ac lectus nec elit aliquam lobortis in sit amet massa.";
//an observable that retrieves its value when first bound
ko.onDemandObservable = function(callback, target) {
var _value = ko.observable(); //private observable
var result = ko.dependentObservable({
read: function() {
//if it has not been loaded, execute the supplied function
if (!result.loaded()) {
callback.call(target);
}
//always return the current value
return _value();
},
write: function(newValue) {
//indicate that the value is now loaded and set it
result.loaded(true);
_value(newValue);
},
deferEvaluation: true //do not evaluate immediately when created
});
//expose the current state, which can be bound against
result.loaded = ko.observable();
//load it again
result.refresh = function() {
result.loaded(false);
};
return result;
};
function Tab(id, name) {
this.id = id;
this.name = ko.observable(name);
//sample of using an on-demand observable here
this.details = ko.onDemandObservable(this.getDetails, this);
}
Tab.prototype.getDetails = function() {
$.ajax({
type: 'POST',
url: '/echo/json/',
data: {
json: ko.toJSON({
details: new Date().toLocaleTimeString() + " " + this.name() + fakeText
}),
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.