-

730 Ash Ct. 730 Ash Ct.

by Asle Gundersby

HTML

<script src="http://knockoutjs.com/downloads/knockout-3.3.0.js"></script>
<script src="https://rawgit.com/rniemeyer/knockout-postbox/master/build/knockout-postbox.js"></script>
<div id="one">
    <input data-bind="value: test" />
</div>

<div id="two">
    <input data-bind="value: test2" />
</div>

JavaScript

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) {
    ...