【发布时间】:2013-10-23 19:53:22
【问题描述】:
你如何正确地使用Require.js 来加载一个模块,该模块返回一个具有require 依赖的构造函数?
我的问题似乎是范围问题,我已经看到返回的构造函数中可用的一些模块,例如“durandal/app”,但我看不出它们的范围与我定义的模块有何不同。
这个例子是从 Durandal Creating a Module docs修改而来的
define([**someothermodule**, "durandal/app"], function(**someothermodule**, app){
var backend = function(username, password){
this.username = username;
this.password = password;
someothermodule.whatever(); <--someothermodule is not defined
app.whatever(); <-- this is in scope
};
backend.prototype.getCustomers = function(){
//do some ajax and return a promise
};
return backend;
});
定义([后端],函数(后端){
return {
customers:ko.observableArray([]),
activate:function(){
var that = this;
var service = new backend('username', 'password');
return service.getCustomers().then(function(results){
that.customers(results);
});
}
};
});
其他模块:
define([], function(){
var whatever = function(){
alert("whatever");
};
return {
whatever: whatever
};
});
【问题讨论】:
-
someothermodule怎么样? -
在原始帖子中添加了一些其他模块
-
似乎没问题。检查浏览器控制台是否有其他错误。您在加载 someOtherModule.js 时是否看到任何错误?
标签: requirejs durandal durandal-2.0