2012-03-23 3 views
0

을 backbone.js : "catch되지 않은 구문 에러 : 예기치 않은 토큰",간단한 이벤트는, 내가 오류 다음 얻을 index.html을에서이를 실행에 오류

events: { 
      "click #add-friend": "showPrompt", 
     }, 

를 참조 그것은 특별히을 의미한다 ":"여기 "click # add-friend": "showPrompt" 아래의 추가 컨텍스트. 어떤 조언을 주시면 감사하겠습니다.

(function ($) { 

    Friend = Backbone.Model.extend({ 
     // create a model to to hold friend attribute 
     name: null 
    }); 

    Friends = Backbone.Collection.extend({ 
     // this is our friends collection and holds out Friend models 
     initialize: function(models, options) { 
      this.bind("add", options.view.addFriendLi); 
      // listens for "add" and calls a view function if so 
     } 
    }); 

    AppView = Backbone.View.extend({ 
     el: $("body"), 
     initialize: function() { 
      this.friends = new Friends(null, {view: this}); 
     // creates a new friends collection when the view is initialized 
     // pass it a reference to the view to create a connection between the two 
     events: { 
      "click #add-friend": "showPrompt" 
     }, 
     showPrompt: function() { 
      var friend_name = prompt("Who is your friend?"); 
      var friend_model = new Friend({name:friend_name}); 
      // adds a new friend model to out Friend collection 
      this.friends.add(friend_model); 
     }, 
     addFriendLi: function (model) { 
      // the parameter passed is a reference to the model that was added 
      $("#friends_list").append("<li>" + model.get('name') + "</li>"); 
     } 
    }); 
    var appview = new AppView; 
})(jQuery); 

답변

2

초기화 방법 :

initialize: function() { 
    this.friends = new Friends(null, {view: this}); 
}, // add a "}," here 

events: { 
    "click #add-friend": "showPrompt" 
}, 
+0

쉼표 제거가 도움이되지 않았습니다. – zallarak

+0

수정 해 주셔서 감사합니다! – zallarak

1

속성 값 뒤에 쉼표를 제거

"click #add-friend": "showPrompt" // remove the comma 

당신은 또한 마지막에 닫는 } 누락 : 당신은 끝에 여분의 쉼표를 가지고

events: { 
    "click #add-friend": "showPrompt" // <-- comma removed! 
}, 
+0

이것은 사실이지만 오류의 원인이 아닙니다. – Pointy

2

"초기화"기능에 "}"이 없습니다. 이것이 없다면 토큰 "events"가 새로운 문장을 시작하고 있다고 생각합니다. 그 문맥에서 문법적으로 부정확 한 문자열 상수 다음의 콜론까지는 모든 것이 좋습니다.

아, 또한 "이벤트"속성에서 "초기화"속성의 값을 구분하기 위해 쉼표도 필요합니다.

+0

감사합니다. – zallarak

관련 문제