2013-08-19 157 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! –