2013-08-19 48 views
0

我想使用jQueryMobile,Backbone和RequireJs設置項目。以下是相關的代碼片段:jQueryMobile和骨幹 - 如何加載「路由器/移動路由器」

require([ "jquery", "backbone", "routers/mobileRouter" ], 
    function($, Backbone, Mobile) { 
     /* do something */ 
    } 
) ; 

它實際上來自here。運行代碼給出了「路由器/ mobileRouter」

GET http://localhost:9000/scripts/routers/mobileRouter.js 404 (Not Found) 

例如404,如果我搜索我的項目「mobileRouter.js」我得到以下

./app/bower_components/jquery-mobile/demos/examples/backbone-require/js/routers/mobileRouter.js 
./app/bower_components/jquery-mobile/dist/demos/examples/backbone-require/js/routers/mobileRouter.js 

這些演示/例子,那麼我應該如何加載這個,也許我需要安裝其他軟件包?任何關於這個文件的鏈接當然也可以幫助我!

更新:這裏是所有的js代碼

// Sets the require.js configuration for your application. 
require.config({ 

    // 3rd party script alias names (Easier to type "jquery" than "libs/jquery-1.8.3.min") 
    paths: { 
     // Core Libraries 
     jquery:  '../bower_components/jquery/jquery', 
     backbone: '../bower_components/backbone/backbone', 
     underscore: '../bower_components/underscore/underscore', 
     jquerymobile:'../bower_components/jquery-mobile/dist/jquery.mobile.min' 

    }, 

    // Sets the configuration for your third party scripts that are not AMD compatible 
    shim: { 

     "backbone": { 
      "deps": [ "underscore", "jquery" ], 
      "exports": "Backbone" //attaches "Backbone" to the window object 
     }, 
     "jquery.mobile": ['jquery'] 
    } // end Shim Configuration 
}); 

// Includes File Dependencies 
require([ "jquery", "backbone", "routers/mobileRouter" ], function($, Backbone, Mobile)  { 

    $(document).on("mobileinit", 
     // Set up the "mobileinit" handler before requiring jQuery Mobile's module 
     function() { 
      // Prevents all anchor click handling including the addition of active button state and alternate link bluring. 
      $.mobile.linkBindingEnabled = false; 

      // Disabling this will prevent jQuery Mobile from handling hash changes 
      $.mobile.hashListeningEnabled = false; 
     } 
    ); 

    require([ "jquerymobile" ], function() { 
     // Instantiates a new Backbone.js Mobile Router 
     this.router = new Mobile(); 
    }); 
}); 
+0

我想我知道問題是什麼。它只是他們爲[this]寫的東西(https://github.com/jquery/jquery-mobile/tree/master/demos/examples/backbone-require)演示:)它的主幹實現。 –

回答

1

又一個鍵/值添加到您的路徑:

paths: { 
    // Core Libraries 
    jquery:  '../bower_components/jquery/jquery', 
    backbone: '../bower_components/backbone/backbone', 
    underscore: '../bower_components/underscore/underscore', 
    jquerymobile:'../bower_components/jquery-mobile/dist/jquery.mobile.min', 
    jquerymobilerouter: '../bower_components/jquery-mobile/demos/examples/backbone-require/js/routers/mobileRouter.js' 
}, 

那麼你可以使用它像這樣:

require(["jquery", "backbone", "jquerymobilerouter"], function($, Backbone, MobileRouter) { 
}); 
+0

嗯,聽起來不對。它解決了路由器問題,但現在我有了幾個新的404。現在RequireJs嘗試加載諸如http:// localhost:9000/models/CategoryModel.js,http:// localhost:9000/collections/CategoriesCollection .js,http:// localhost:9000/views/CategoryView.js。有些東西是錯的:( –

+1

那麼,我提出的改變與那些現在失敗的模型,集合和視圖無關。你有沒有正確設置路徑?我沒有看到'''baseUrl'' '在require.config中的任意位置設置,這可能是原因。 –

+0

您的帖子的確是問題的答案。請參閱我上面的帖子對我的評論!Thnx! –