Require.js
require 依存関係を持つコンストラクター関数を返すモジュールを 正しくロードするには、どうすればよいでしょうか?
私の問題はスコープの問題のようです。「durandal/app」のような返されたコンストラクター内で使用できるいくつかのモジュールを見てきましたが、定義したモジュールとは異なるスコープがどのように設定されているかわかりません。
この例は、デュランダルのモジュールの作成ドキュメントから変更されています
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;
});
define([バックエンド], function(バックエンド){
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
};
});