간단하지만 혼란스러운 문제가 있습니다. 이 오류가 발생합니다백본보기의 모델이 누락되었지만 이유를 이해할 수 없습니다.
<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({pushState: true});
});
</script>
: 나는 다음과 같은 코드 조각이 나는 그래도 {pushState: true}
부분을 가지고가는 경우에
Uncaught TypeError: Cannot call method 'toJSON' of undefined
을 그냥 인수없이 Backbone.history.start()
할, 그냥 잘 작동합니다.
오류 옆에 show_view.js: 19
이 표시됩니다.
ShowView.prototype.template = JST["backbone/templates/restaurant_locations/show"];
ShowView.prototype.render = function() {
$(this.el).html(this.template(this.model.toJSON())); // LINE 19
Uncaught TypeError: Cannot call method 'toJSON' of undefined
return this;
}
그래서 내가
this.model
가 정의되지 않은 추측 : 여기처럼
show_view.js
의 일부가 보이는거야. 여기에
show_view
커피 스크립트는 다음과 같습니다
Lunchhub.Views.RestaurantLocations ||= {}
class Lunchhub.Views.RestaurantLocations.ShowView extends Backbone.View
template: JST["backbone/templates/restaurant_locations/show"]
render: ->
$(@el).html(@template(@model.toJSON()))
return this
나는이 문제를 해결할 수있는 생각, @model
그것을 할 필요가 무엇을 할 수 있도록 할 수 있습니다. 그러나 나는 @model
이 어디서 온 것인지 알지 못합니다.
무엇을해야합니까?
편집 : 조금 더 있습니다. 아래의 show
함수에서 id
은 "restaurant_locations"로 설정되고 물론 restuarant_locations
의 ID를 가진 @restaurantLocations
의 구성원은 없습니다. id
세트가 restaurant_locations
으로 설정된 사실은 어느 정도 의미가 있습니다. 내가 치는 URL은 http://localhost:3000/restaurant_locations
입니다. 그러나 index
함수가 아닌 show
이 아니라면이 URL이 내가가는 URL 인 것 같습니다. 내가 거기에 몇 가지 문제를 참조
routes:
"new" : "newRestaurantLocation"
"index" : "index"
":id/edit" : "edit"
":id" : "show"
".*" : "index"
: 당신이 그렇게 저희가 귀하의 경로를 살펴 보자 라우팅 문제를 가지고있는 것처럼
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"
newRestaurantLocation: ->
@view = new Lunchhub.Views.RestaurantLocations.NewView(collection: @restaurantLocations)
$("#restaurant_locations").html(@view.render().el)
index: ->
@view = new Lunchhub.Views.RestaurantLocations.IndexView(restaurantLocations: @restaurantLocations)
$("#restaurant_locations").html(@view.render().el)
show: (id) ->
restaurant_location = @restaurantLocations.get(id)
@view = new Lunchhub.Views.RestaurantLocations.ShowView(model: restaurant_location)
$("#restaurant_locations").html(@view.render().el)
edit: (id) ->
restaurant_location = @restaurantLocations.get(id)
@view = new Lunchhub.Views.RestaurantLocations.EditView(model: restaurant_location)
$("#restaurant_locations").html(@view.render().el)
'showVideo.model'은 '새로운 ShowView ({model : myModelInstance})'에서 가져옵니다. 미안하지만 훨씬 간단한 시나리오에서 백본으로 조사를 시작해야한다는 것을 모르는 경우에는 꼭 말해야합니다. – fguillen
당신은 copypasta 했습니까? – jakee
필자가 언급 했어야 할 것은 : 저는 레일즈에 있고 백본 레일 (backbone-rails) 젬을 사용하고 있습니다.이 젬은 자동으로 많은 코드를 생성합니다. 나는 그것이 생성 된 코드를 디버깅하지 않고도 작동 할 것이라고 기대할 수 있지만, 그렇다고 밝혀지지는 않았다. –