knockout.js - Knockoutjs get parent object -


i having difficulties on getting parent object's field child object using knockout.js.

function child(info){     var self = this;     self.x = info.x;     self.y = info.y;     self.parentthing = parent.fieldtoget();  // when had problem }  function main() {     var self = this;     self.fieldtoget = ko.observable();             self.things = ko.observablearray();     self.postfunction = function(){         $.post('xxx.php', $("form#filterform").serialize(), function(data){                 var mappedthing = $.map(data.data, function(info){return new child(info); });                 self.things(mappedthing);             }         }, 'json');     }; } var main = new main(); ko.applybindings(main, $("div#main")[0]); 



hierachy main has several child. in child object want parents 'attribute': fieldtoget.

i think solution pass parent class child parameter.

function child(info, parent){     var self = this;     self.parent = parent;     self.x = info.x;     self.y = info.y;     self.parentthing = parent.fieldtoget();  // when had problem }   function main() {     var self = this;     self.fieldtoget = ko.observable();     self.things = ko.observablearray();     self.postfunction = function()     {         $.post('xxx.php', $("form#filterform").serialize(), function(data) {               var mappedthing = $.map(data.data, function(info) {                   return new child(info, self);                       });               self.things(mappedthing);         }, 'json');     }; }   var main = new main();   ko.applybindings(main, $("div#main")[0]); 

as can see in example parent instance (self) passed parameter child class in constructor (function child(info, parent))


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -