Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Requirejs 如何使用需要返回具有依赖项的构造函数_Requirejs_Durandal_Durandal 2.0 - Fatal编程技术网

Requirejs 如何使用需要返回具有依赖项的构造函数

Requirejs 如何使用需要返回具有依赖项的构造函数,requirejs,durandal,durandal-2.0,Requirejs,Durandal,Durandal 2.0,如何正确使用Require.js加载一个模块,该模块返回一个具有Require依赖项的构造函数 我的问题似乎是一个范围问题,我看到了一些在返回的构造函数中可用的模块,如“durandal/app”,我看不出它们的范围与我定义的模块有什么不同 此示例是从Durandal创建模块修改而来的 })) 其他模块: define([], function(){ var whatever = function(){ alert("whatever"); }; return { whate

如何正确使用
Require.js
加载一个模块,该模块返回一个具有Require依赖项的构造函数

我的问题似乎是一个范围问题,我看到了一些在返回的构造函数中可用的模块,如“durandal/app”,我看不出它们的范围与我定义的模块有什么不同

此示例是从Durandal创建模块修改而来的

}))

其他模块:

define([], function(){

var whatever = function(){
    alert("whatever");
};

return {
    whatever: whatever
};

}))

上例中的索引有两个问题。a) 在define
[backend]
中,使用了
['backend']
而不是
[/backend']
和b)
ko
是在没有定义的情况下使用的。两者都可能存在复制/粘贴错误

假设someothermodule与index.js位于同一目录中,就可以这样定义它了

define(['./someothermodule', ...], function( someothermodule, ... ) 
以下是完整的示例:

backend.js someothermodule.js index.js
现场版本可在以下网址获得:

someothermodule的外观如何?将someothermodule添加到原始帖子似乎还可以。检查浏览器控制台是否存在其他错误。加载someOtherModule.js时是否看到任何错误?没错,它确实有效,我必须修复一个脚本错误或其他错误,因为它现在正在我的项目中工作。谢谢你的帮助。
define([], function(){

var whatever = function(){
    alert("whatever");
};

return {
    whatever: whatever
};
define(['./someothermodule', ...], function( someothermodule, ... ) 
/*globals define*/
define(['./someothermodule', 'durandal/app', 'jquery'], function( someothermodule, app, $ ) {
    "use strict";
    var backend = function( username, password ) {
        this.username = username;
        this.password = password;
        this.whatever = someothermodule.whatever();
        app.trigger('whatever', this.whatever);
    };

    backend.prototype.getCustomers = function() {
        //do some ajax and return a promise
        return $.getJSON('app/so/19551060/fixtures/customer.json');
    };
    return backend;
});
/*globals define*/
define(function(){
    "use strict";
    var whatever = function(){
        return 'whatever return value';
    };

    return {
        whatever: whatever
    };
});
/*globals define*/
define(['./backend', 'knockout'], function(backend, ko){
    "use strict";
    return {
        customers:ko.observableArray([]),
        activate:function(){
            var that = this;
            var service = new backend('username', 'password');

            return service.getCustomers().then(function(results){
                that.customers(results);
            });
        }
    };
});