2012-12-27 60 views
1

我創建了一個簡單的requireJS插件。在node.js中使用RequireJS插件amdefine

它運作良好,在瀏覽器,但在沒有的NodeJS ...該插件可以使用here

,作爲一個插件,不只是一個模塊目前的代碼是這樣的:

if (typeof define !== 'function') 
    var define = require('amdefine')(module); 

define(function (require, exports, module) { 
    var _ = require("underscore"), 
     Backbone = require("backbone"); 

    ... 

    var plugin = { 
     load:function (name, require, load, config) { 
      var resources = this.parseResources(name); 
      require(resources, function() { 
       var localValidator = global.Validator.extend({}); 
       _.each(arguments, function (resource) { 
        localValidator.customize(resource); 
       }, this); 
       var local = _.extend({}, global, { 
        Validator:localValidator, 
        Model:global.Model.extend({ 
         Validator:localValidator 
        }), 
        SyncModel:global.SyncModel.extend({ 
         Validator:localValidator 
        }) 
       }); 
       load(local); 
      }); 
     }, 
     parseResources:function (name) { 
      if (name == "") 
       return []; 
      return name.split("+"); 
     } 
    }; 

    var global = { 
     version:"1.0.1", 
     View:View, 
     Aggregator:Aggregator, 
     Messenger:Messenger, 
     Model:AsyncModel, 
     SyncModel:SyncModel, 
     Validator:Validator, 
     Runner:Runner, 
     DependencyResolver:DependencyResolver, 
     load:plugin.load.bind(plugin) 
    }; 

    module.exports = global; 

}); 

我嘗試它是否有效與的NodeJS茉莉稱之爲單元測試,但是它會顯示無法找到的文件名:

if (typeof define !== 'function') { var define = require('amdefine')(module, require) } 
require("./validation"); //works 
require("./validation!./basicTests"); //does not work, but the plugin and path is ok... 

堆棧:

Message: 
    Error: Cannot find module './validation!./basicTests' 
    Stacktrace: 
    Error: Cannot find module './validation!./basicTests' 
    at Function.Module._resolveFilename (module.js:338:15) 
    at Function.Module._load (module.js:280:25) 
    at Module.require (module.js:362:17) 
    at require (module.js:378:17) 
    at null.<anonymous> (D:\creation\software developer\projects\dev\document root\src\validation.plugin.spec.js:9:17) 
    at jasmine.Env.describe (D:\creation\software developer\libs\jasmine-node\lib\jasmine-node/jasmine-2.0.0.rc1.js:791:21) 
    at describe (D:\creation\software developer\libs\jasmine-node\lib\jasmine-node/jasmine-2.0.0.rc1.js:575:27) 
    at Object.<anonymous> (D:\creation\software developer\projects\dev\document root\src\validation.plugin.spec.js:7:1) 
    at Module._compile (module.js:449:26) 
    at Object.Module._extensions..js (module.js:467:10) 

是否有可能獲得某種工作?

編輯:

取得了一些進展:

if (typeof define !== 'function') 
    var define = require('amdefine')(module, require); 

define(function (require, exports, module) { 
    var _ = require("underscore"), 
     Backbone = require("backbone"), 
     global = require("./validation"), 
     empty = require("./validation!"), 
     local = require("./validation!../src/basicTests"); 

    describe("validation", function() { 
     expect(global).not.toBe(undefined); 
     expect(empty).not.toBe(undefined); //fail 
     expect(local).not.toBe(undefined); //fail 
    }); 
}); 

在本地對象似乎是好了plugin.load,我給它加載方法,但它不與到達要求。至少在一個定義標籤,它調用amdefine需要,而不是CommonJS的...

EDIT2:

我發現,amdefine網站:

加載插件,只要支持他們同時調用它們的加載() 回調。所以那些做網絡請求的將不會 工作。但是像文本這樣的插件可以在本地加載文本文件。

我不知道需要被稱爲異步。

回答

2

我修改我的插件調用由amdefine同步,並通過瀏覽器腳本異步要求。

var amdefine = false; 
if (typeof define !== 'function') 
    var define = require('amdefine')(module, require), 
     amdefine = true; 

define(function (require, exports, module) { 
    var _ = require("underscore"), 
     Backbone = require("backbone"); 


    ... 


    var plugin = { 
     load:function (name, _require, load, config) { 
      var resources = this.parseResources(name); 
      var require = amdefine ? function (resources, callback) { 
       var modules = []; 
       _.each(resources, function (resource) { 
        modules.push(_require(resource)); 
       }); 
       callback.apply(null, modules); 
      } : _require; 
      require(resources, function() { 
       var localValidator = global.Validator.extend({}); 
       _.each(arguments, function (resource) { 
        localValidator.customize(resource); 
       }, this); 
       var local = _.extend({}, global, { 
        Validator:localValidator, 
        Model:global.Model.extend({ 
         Validator:localValidator 
        }), 
        SyncModel:global.SyncModel.extend({ 
         Validator:localValidator 
        }) 
       }); 
       load(local); 
      }); 
     }, 
     parseResources:function (name) { 
      if (name == "") 
       return []; 
      return name.split(":"); 
     } 
    }; 

    ... 

});