我正在使用 Rails。我刚刚安装了backbone-rails
gem 并按照此处的说明进行操作。
当我尝试做时Backbone.history.start()
,我收到此错误:
Uncaught TypeError: Cannot call method 'start' of undefined
我知道,根据this other SO questionBackbone.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>