我有2個交叉引用的型號。這可能是這樣的:2個型號的交叉引用
MainModel:
define(
[ 'durandal/app', 'durandal/plugins/router', 'models/Shell', 'models/EditModel' ],
function (app, router, shell, editModel) {
//...
return {
//...
// This function should be accessible by the EditModel
update: function() {
//...
},
showEditView: function() {
// Initialise the EditModel with some data and show the according view afterwards
editModel.init('set some important stuff here...');
router.navigateTo('#/EditView');
}
//...
};
}
);
EditModel:
define(
[ 'durandal/app', 'durandal/plugins/router', 'models/Shell', 'models/MainModel' ],
function (app, router, shell, mainModel) {
//...
return {
//...
// This function should be accessible by the MainModel
init: function() {
//...
},
showMainView: function() {
// Update the the MainModel with some data and show the according view afterwards
mainModel.update('set new data here...');
router.navigateTo('#/MainView');
}
//...
};
}
);
可惜,這是行不通的。如果我在MainView上加載我的頁面並調用showEditView,則變量editView是已知的,並且一切正常,但是EditModel中的變量mainModel未定義,因此調用mainModel.update(...)
失敗。 如果我在EditView上加載我的頁面,但在「相反方向」(EditModel中的var mainModel已知,但MainModel中的editModel未定義),則會發生同樣的事情。
這是一個已知的問題,如果是這樣的話:我怎樣才能繞過它?
感謝