【发布时间】:2012-06-27 01:38:33
【问题描述】:
我正在使用 Rails。我刚刚安装了backbone-rails gem 并按照here 的说明进行操作。
当我尝试执行 Backbone.history.start() 时,我收到此错误:
Uncaught TypeError: Cannot call method 'start' of undefined
根据this other SO question 的说法,我知道在您创建至少一条路线之后才能调用Backbone.history.start()。不过,这似乎不是我的问题,因为我确实有多个路由,在此 CoffeeScript 文件中定义:
class Lunchhub.Routers.RestaurantLocationsRouter extends Backbone.Router
initialize: (options) ->
@restaurantLocations = new Lunchhub.Collections.RestaurantLocationsCollection()
@restaurantLocations.reset options.restaurantLocations
routes:
"new" : "newRestaurantLocation"
"index" : "index"
":id/edit" : "edit"
":id" : "show"
".*" : "index"
为什么会出现这个错误?
编辑:这是我的代码:
<div id="restaurant_locations"></div>
<script type="text/javascript">
$(function() {
window.router = new Lunchhub.Routers.RestaurantLocationsRouter({restaurantLocations: <%= @restaurant_locations.to_json.html_safe -%>});
Backbone.history.start();
});
</script>
【问题讨论】:
-
在调用
Backbone.history.start()之前,您是否正在创建任何路由的实例? -
比较jsfiddle.net/ambiguous/a5SLF和jsfiddle.net/ambiguous/a5SLF/1的行为,第二个有
new R,而第一个没有。 -
我想我是。我进行了编辑以包含我的代码。
标签: ruby-on-rails-3 backbone.js