【发布时间】:2012-01-31 21:06:36
【问题描述】:
**更新
我正在使用带有 jquery 的 knockoutjs 来构建应用程序。
因为我使用的是 jquery,所以我必须在 jquery 函数中包装敲除。这很好,除了通过应用程序我为各种不同的页面重新使用相同的对象/功能。因为该对象包含对淘汰赛的引用,所以我不能将它们放在全局文件中,这意味着我有很多重复,严重地使我的代码膨胀并使其非常难以管理。
所以
//global file
//this is fine because it dose not reference knockout
var global_cust = function(){
this.name: "dave";
this.isAwkward: true;
}
//this is not fine because it references knockout
var knockout_global_customer = function(properties){
this.name: ko.observable(properties.name? properties.name: "unknown"),
this.isAwkward: ko.observable(properties.isAwkward? properties.isAwkward: true),
}
//page 1
(function($){
//i have to include the customer in every page of aplplication that uses it. I would like this in a global file or someway of Precompiling it so don't have to include it in every page - a bit like php include but for JS
var customer = function(properties){
this.name: ko.observable(properties.name? properties.name: "unknown"),
this.isAwkward: ko.observable(properties.isAwkward? properties.isAwkward: true),
}
var viewModel = {
currentCustomer: ko.observable(new customer()),
globalCustomer: ko.observable(new global_cust()),
}
ko.applyBindings(viewModel);
})(jQuery);
不管怎样,还是有预处理文件的工具,这样我就不必在每个页面上不断复制相同的对象/功能
谢谢
【问题讨论】:
-
为什么需要访问外部的视图模型?
-
感谢您的评论,不确定您所说的世界是什么意思,但我需要的不是全局视图模型,而是视图模型引用的客户函数。对于我的应用程序的每个页面,我都有一个不同的视图模型,其中包含不同的属性,其中一些引用对象/函数。
标签: jquery design-patterns structure knockout.js