1
我想分割我的視圖模型到多個可重複使用的視圖模型。 我有一個視圖模型,其中包含幾個下拉菜單和一個按鈕。訂閱從另一個視圖模型的值在淘汰賽js
var TopView = function() {
self.DropDownA = ko.observableArray();
self.selectedDDA = ko.observable();
self.DropDownB = ko.observableArray();
self.selectedDDB = ko.observable();
$.getJSON("someAPIurl", function (result) {
ko.mapping.fromJS(result, {}, self);
}); //this builds dropdownA
$self.selectedDDA.subscribe(function(newValue) {
$.getJSON("anotherAPI"+newValue, function (result) {
ko.mapping.fromJS(result, {}, self);
});
}; // this builds dropdownB
$self.buttonClicked = function() {
alert("I clicked!");
}
}
我主視圖模型看起來是這樣的:
var MainView = function() {
var self = this;
var topView = ko.observable({ TopView: new TopView() });
// How do i get the selected values from topView once the user clicks the button???
}
如何訂閱DropDownA和DropDownB選擇的值從我MAINVIEW ??? 請幫忙!謝謝!
你意識到你正在'TopView'構造函數中使用'self'而不事先將它分配給'this'? – 2013-02-11 17:29:19