Backbone应用程序没有初始化生产

时间:2014-05-06 05:56:01

标签: ruby-on-rails backbone.js heroku requirejs

我在Rails(4.1.0)应用程序上使用RequireJS(requirejs-rails gem)的Backbone(1.1.0)在我的本地机器上正常工作。但是当我预编译资产并启动服务器时,由r.js编译/压缩的Backbone应用程序无法启动。不会在任何地方抛出任何错误。以下是一些片段:

requirejs.yml

paths:
    jquery: "jquery/jquery"
    underscore: "underscore/underscore"
    backbone: "backbone/backbone"
    "jquery-mobile": "jquery-mobile-bower/js/jquery.mobile-1.4.2.min"
    tpl: "requirejs-tpl/tpl"
    "backbone-query-parameters": "backbone-query-parameters/backbone.queryparams"
    serializeJSON: "jquery.serializeJSON/jquery.serializejson"
    "jqm-config": "jqm-config"
    fastclick: "fastclick/lib/fastclick"

shim:
    "backbone-query-parameters":
        deps: ['backbone']
    'jqm-config':
        deps: ["jquery-mobile"]
    'jquery-mobile':
        deps: ["jquery"]

modules:
  - name: 'application' # default

tpl:
    extension: '.html' # default

的application.js

define(["backbone", "./router", "jquery-mobile", "jqm-config", "fastclick"],
function(Backbone, Router) {

var BackboneApp = {

    start: function() {
        this.router = new Router();
        Backbone.history.start();
    }
};

return BackboneApp.start();
})

application.html.erb

<!DOCTYPE html>
<html>
<head>
  <title></title>
  <%= stylesheet_link_tag "application", :media => "all" %>
  <%= csrf_meta_tags %>
</head>
<body>
    hello
    <%= yield %>
<%= requirejs_include_tag "application" %>
</body>
</html>

这是我的服务器日志:

18:57 $ RAILS_ENV=production rails server
=> Booting WEBrick
=> Rails 4.1.0 application starting in production on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Notice: server is listening on all interfaces (0.0.0.0). Consider using 127.0.0.1 (--binding option)
=> Ctrl-C to shutdown server
[2014-05-06 19:04:01] INFO  WEBrick 1.3.1
[2014-05-06 19:04:01] INFO  ruby 2.1.1 (2014-02-24) [x86_64-darwin12.0]
[2014-05-06 19:04:01] INFO  WEBrick::HTTPServer#start: pid=60796 port=3000
Started GET "/" for 127.0.0.1 at 2014-05-06 19:04:03 -0500
Processing by MainController#index as HTML
  Rendered main/index.html.erb within layouts/application (1.4ms)
Completed 200 OK in 16ms (Views: 13.3ms | ActiveRecord: 0.0ms)

只是为了解决任何困惑,这里加载的是html:

<!DOCTYPE html>
<html>
<head>
  <title></title>
  <link href="/assets/application-34af7785e9260ce67d287c5a05fc3583.css" media="all" rel="stylesheet" />
  <meta content="authenticity_token" name="csrf-param" />
  <meta content="MjhW2IjblGRfvIsAq/hphJT/WlFckUsQ/jSv8skdysk=" name="csrf-token" />
</head>
<body>
  hello

   <script>var require = {"baseUrl":"/assets","paths":{"application":"/assets/application-4d1052c11b8765cc7707a5f8643e33bf"},"priority":["application"],"shim":{"backbone-query-parameters":{"deps":["backbone"]},"jqm-config":{"deps":["jquery-mobile"]},"backbone":{"deps":["underscore","jquery"],"exports":"Backbone"}}};</script>
   <script data-main="application-4d1052c11b8765cc7707a5f8643e33bf" src="/assets/require-e48b9057622ee54d9d9b4dd773d4f295.js"></script>

</body>
</html>

我猜这个错误在于如何在application.js中或在requirejs.yml中的配置中初始化我的应用程序,但我无法解决它。

1 个答案:

答案 0 :(得分:0)

解决了这个issue。在我的顶级javascript(application.js)中使用require来电而非define来电