我有以下代码:
// viewModel
var cardViewModel = function (data) {
var self = this;
ko.mapping.fromJS(data, {}, self);
self.isNew = ko.observable(false);
// etc
}
// mapping
var mapping = {
create: function (options) {
var innerModel = ko.mapping.fromJS(options.data, {
'cards': {
create: function (options) {
//debugger;
return new cardViewModel(options.data);
}
} // etc
});
// data
var setData = dataservice.getSet();
var observableData = ko.mapping.fromJS(setData, mapping);
在我的 viewModel 中,我想添加如下内容:
self.save = function() {
var card = new cardViewModel(dataservice.save(this)) // works fine
innerModel.cards.push(card) // doesn't work because I don't have access to innerModel from the card viewModel
}
现在我只是从viewModels触发事件并从innerModel监听它们,但这有点混乱,想看看是否有任何方法可以从setViewModel访问innerModel.sets()。