我的视图模型开始变得非常大,所以我决定将它拆分为多个文件。我已经尝试了许多不同的方法,但没有任何效果。
我的视图模型如下所示:
namespace.model = function(constructorParam) {
var self = this;
self.param1 = ko.observable(constructorParam.param1);
self.param2 = ko.observable(privateFunction(constructorParam));
self.clickEvent = function() {
// do something with params
// call some private funcitons
privateFunction2(self.param2);
};
function privateFunction(param) {
// do some stuff
}
function privateFunction2(param) {
// do some stuff
}
};
我需要跨多个文件访问私有函数和可观察参数。我的最终模型应该是这样的:
// file 1
// contains constructor and param initialization + many common private helper funcitons
namespace.model = function(constructorParam) {
var self = this;
self.param1 = ko.observable(constructorParam.param1);
self.param2 = ko.observable(privateFunction(constructorParam));
function privateFunction(param) {
// do some stuff
}
function privateFunction2(param) {
// do some stuff
}
};
// file 2
// contains event hendlers
self.clickEvent = function () {
// i need to acces properties from namespace.model
self.param1
// call some private funcitons
privateFunction2(self.param2);
};
// view model initialization
ko.applyBindings(new namespace.model(initValues));
是否有可能通过淘汰赛实现这样的目标?谢谢