【问题标题】:How to use requires to return constructor functions that have dependencies如何使用 requires 返回具有依赖关系的构造函数
【发布时间】: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


【解决方案1】:
上面示例中的

index 有两个问题。 a) 在定义中使用[backend] 代替['backend'] 和b) 使用ko 而不定义它。两者都可能出现复制/粘贴错误。

假设 someothermodule 与 index.js 位于同一目录中,那么定义它就足够了

define(['./someothermodule', ...], function( someothermodule, ... ) 

这是完整的例子:

backend.js

/*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;
});

someothermodule.js

/*globals define*/
define(function(){
    "use strict";
    var whatever = function(){
        return 'whatever return value';
    };

    return {
        whatever: whatever
    };
});

index.js

/*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);
            });
        }
    };
});

现场版可在:http://dfiddle.github.io/dFiddle-2.0/#so/19551060

【讨论】:

  • 你的权利,它确实有效,我必须修复一个脚本错误或其他东西,因为它现在在我的项目中工作。感谢您的帮助。
猜你喜欢
  • 2017-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多