我在使用 knockout.js 从子对象获取父对象的特定字段时遇到困难。
function Child(info){
var self = this;
self.x = info.x;
self.y = info.y;
self.parentThing = parent.fieldToGet(); // This is when I 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]);
层次结构是 Main 有几个 Child。在 Child 对象中,我想获取其父母的“属性”:fieldToGet。