one = function(){
var self = this;
self.test = ko.observable().syncWith("blah");
self.relatedInfo = ko.observable();
// "load" 'relatedinfo' from db - here simulated by a simple assign of value
self.relatedInfo("Compadre!");
// this is the way I couple data from different database-tables into one object
// 1. I load data asyncronously with ajax, then
// 2. trugger the computed (below) when the observable gets the data, then
// 3. calculate the updated observable based on the newly loaded data
ko.computed(function(){
var mystring = self.test();
if (mystring){
if (self.relatedInfo()){
mystring += " " + self.relatedInfo();
self.text(mystring);
}
}
// I normally user ko.utils.eachArray to loop over a list of objects then to match
// each with another list of objects using ko.utils.firstArray. Ex:
// hook up statistiscs on Project
//ko.computed(function(){
// each(self.ProjectStatistics(), function (stats) {
// var match = koFirst(self.Projects(), function (project) {
// return project.projectID == stats.projectID;
// });
// if (match) { // matching project.
// match.statistics(stats);
// }
// });
//});
//
// where
// self.Projects = ko.observableArray(); // loaded and filled with Project-objects
// self.ProjectStatistics = ko.observableArray(); loaded and filled with ProjectProjectStatistic-objects
// and Project = function(data){ ... this.statistics = ko.observable(); ...};
});
};
two = function(){
var self = this;
self.test2 = ko.observable('Hi').syncWith("blah");
};
ko.subscribable.fn.subscribeChanged = function (callback) {
var oldValue;
this.subscribe(function (_oldValue) {
...
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.