2013-01-21 122 views
1

我一直在盯着這一段時間,似乎無法弄清楚爲什麼一些AMD在加載它們作爲依賴關係後不可用。我有一個稱爲「模型」的自定義模塊;在我的MVC項目中配置了一個包含虛擬路徑「/scripts/models.js」的包。當我在require.config中定義它並作爲依賴項時,它將加載該文件。我可以看到它被請求和發現。從要求沒有錯誤。但是,當我試圖將它作爲傳入我的路由器的加載的依賴性參數引用時,它是未定義的(models.userModel)。需要js不加載模塊

我在這裏做錯了嗎?我沒有看到任何循環依賴關係,我試圖通過給它命名來定義模型模塊。無論我是全局定義還是在我的router.js文件中通過路徑請求模塊,它都是未定義的。

app.js.主配置。 (下)

require.config({ 
    baseUrl: "/scripts/app", 
    paths: { 
     jquery: "../jquery", 
     underscore: "libs/underscore", 
     backbone: "libs/backbone", 
     kendo: "libs/kendo", 
     models: "../models" 
    }, 
    // We shim Backbone since it doesn't declare an AMD module 
    shim: { 
     underscore: { 
      exports: "_" 
     }, 
     backbone: { 
      deps: ["underscore", "jquery"], 
      exports: "Backbone" 
     } 
    }, 
}); 

require([ 
    "jquery", 
    "backbone", 
    "kendo", 
    "models", 
    "router" 
], function ($, backbone, kendo, models, router) { 
    alert("config-start"); 
}); 

user.js.包含在models.js包中。 (如下圖)

define({ 
    userModel : kendo.observable({ 
     datasource: kendo.data.DataSource({ 
      transport: { 
       read: { 
        url: "/api/usersettings", 
        dataType: "json", 
        type: "GET" 
       }, 
       update: { 
        url: "/api/usersettings", 
        dataType: "json", 
        type: "PUT" 
       } 
      }, 
      schema: { 
       model: { 
        id: "UserId" 
       } 
      }, 
      parameterMap: function (options, operation) { 
       if (operation !== "read" && options.models) { 
        return { 
         models: kendo.stringify(options.models) 
        }; 
       } 
       return options; 
      } 
     }), 
     save: function() { 
      this.data.sync(); 
     }, 
    }) 
}); 

router.js文件(如下圖)

define(["jquery", 
    "backbone", 
    "models" 
], function ($, backbone, models) { 

    /** 
    * The Router class contains all the routes within the application - 
    * i.e. URLs and the actions that will be taken as a result. 
    * 
    * @type {Router} 
    */ 

    var Router = Backbone.Router.extend({ 
     contentArea: $("#MainContent"), 
     routes: { 
      "user/usersettings/contact": "contact", 
      "user/usersettings/security": "security", 
      "user/usersettings/dashboard": "dashboard", 
      "user/usersettings/permissions": "permissions", 
      "user/usersettings/colors": "colors" 
     }, 
     contact: function() { 
      var contactTemplate = kendo.template($("#usersettings-usercontact").html()); 
      this.contentArea.empty(); 
      this.contentArea.html(contactTemplate); 

      kendo.bind(this.contentArea, models.userModel); // models is undefined 
     }, 
     security: function() { 

     }, 
     dashboard: function() { 

     }, 
     permissions: function() { 

     }, 
     colors: function() { 

     } 
    }); 

    // Create a router instance 
    var router = new Router(); 

    //Begin routing 
    Backbone.history.start(); 

    return router; 
}); 

也許我失去了一些東西很明顯,但我一直無法加載「模式」作爲外部依賴。從router.js引用時它是未定義的。在「聯繫」功能。

+2

你不顯示'models.js'代碼 - 它返回/輸出正確的值? – jevakallio

+0

models.js是一個配置爲從目錄中提取全部內容的包。這個問題是我沒有從models.js返回值(正如Andreas所述)。感謝您的期待! – Sullify

回答

1

定義需要一個函數,該函數將返回一個值,然後這個值將在另一個模塊需要時被注入。

Source code comment:

/** 
* The function that handles definitions of modules. Differs from 
* require() in that a string for the module should be the first argument, 
* and the function to execute after dependencies are loaded should 
* return a value to define the module corresponding to the first argument's 
* name. 
*/ 
define(function(){ 
    return { 
    userModel: ... 
    } 
}) 
+0

是的,我明白了。這就對了。感謝您花時間。不勝感激! – Sullify