我对Durandal还是很陌生,但是考虑到我在jQuery中的背景和一点点KnockoutJS,考虑到这是一个很棒的框架,但是我遇到了在Durandal中使用RequireJS的两种不同方法。
在Durandal示例中,使用以下语法:Durandal / App / samples / modal / index.js
define(['durandal/app', './customModal'], function (app, CustomModal) {
return {
showCustomModal: function() {
app.showModal(new CustomModal()).then(function(response) {
app.showMessage('You answered "' + response + '".');
});
}
};
});
这里的依赖项/要求(这里是正确的词吗?)被定义为define(['durandal/app', './customModal']
)调用的第一个参数。
在Durandal文档中,使用以下语法:Creating a Module
define(function(require){
var backend = require('backend');
return {
customers:ko.observableArray([]),
activate:function(){
var that = this;
return backend.getCustomers().then(function(results){
that.customers(results);
});
}
};
});
这里仅将函数传递给定义的调用,而require作为对该函数的依赖/要求。然后调用require函数以获取所需的实际依赖关系/需求(var backend = require('backend');
)。
来自.net IoC背景,第一个看起来是正确的,定义了我的要求,然后让requirejs框架为我找到依赖项,第二个看起来像是服务定位器的反模式。
我对这两种方法的解释是否正确,应该使用第一种方法,并且文档有误(!)?
如果没有,这两种方法的优缺点是什么?建议使用哪个,为什么?
非常感谢
根据requirejs的网站,如果您使用durandal解决其依赖关系的方式:
define(function(require) { var app = require('durandal/app'); app.start(); }
“此包装器依赖于Function.prototype.toString()来提供函数内容的有用的字符串值。这在PS3等某些设备和某些较旧的Opera移动浏览器上不起作用。 -quoted from requirejs但是那是一个极端的情况,我认为没有太多人在乎。除此之外。.我只会选择最适合您的套房。
我不确定您的服务定位器反模式是什么意思。我很确定这两种方法都是有效的,而且您选择这样做只是为了美观。
编辑**
Durandal不再使用上述模式来解决其依赖性。现在使用这种方式:
define(['./app','./system'], function (app, system) { //... });