2012-04-30 5 views
0

나는이 내 백본보기 중 하나에 대한 다음 코드를백본보기 이벤트는 기능을 수행하지 않는 것

class GD.Views.Result extends Backbone.View 
    template: JST['mobile/templates/result'] 
    tagName: 'tr' 
    className: 'result' 
    events: 
    'click' : 'showDetail' 
    'click #favourite' : 'addFavourite(Favourite)' 
    'click #like' : 'addFavourite(Like)' 
    'click #dislike' : 'addFavourite(Dislike)' 
    render: -> 
    $(@el).html(@template(model: @model)) 
    this 
    addFavourite: (type) -> 
    event.preventDefault() 
    attributes = 
     id: @model.id 
     type: type 
     cluster: @model.cluster 
    @collection.create attributes, 
    wait: true 
    success: @updateIcons 
    error: @handleError 
    showDetail: -> 
    ... 
    updateIcons: -> 
    ... 
    handleError: -> 
    ... 

그리고 내 콘솔에서이 오류 받고 있어요 :

Uncaught Error: Method "addFavourite(Favourite)" does not exist 

왜 이것이 AddFavourite 메서드가 아닌 showDetail 메서드에서 발생하는지 이해하지 못합니다. 어떤 이벤트에 대해서도 정의 된 인수가 필요한 메서드를 전달할 수 있습니까? 사전에 어떤 도움

많은 감사 (!)

답변

4

@Derick 설명 같이, events 요소의 값이 문자열이보기 개체에서, 그 이름 방법, 그래서 당신이 그런 식으로 매개 변수를 지정할 수없는 경우. 문자열로

  1. 공급 단지 메소드 이름, 다음 방법의 유형을 결정 : 당신이 중 하나 (예 JS에) 할 필요가있다. 예 :

    addFavourite : function (event) { 
    
        event.preventDefault(); 
    
        var type = event.target.id; 
    
    } 
    
  2. Suppy 함수 객체

    events : { 
    
        'click #favourite' : 'addFavourite', 
    
        'click #like' : 'addFavourite', 
    
        'click #dislike' : 'addFavourite' 
    
    } 
    

    (이 예에서, 함수식을 통해). 나는 당신의 예에서 볼 수있는 것과

    events : { 
    
        'click #favourite' : function (event) { 
    
        this.addFavourite(event, 'favourite'); 
    
        }, 
    
    
        'click #like' : function (event) { 
    
        this.addFavourite(event, 'like'); 
    
        }, 
    
    
        'click #dislike' : function (event) { 
    
        this.addFavourite(event, 'dislike'); 
    
        } 
    
    } 
    

    addFavourite : function (event, type) { 
    
        event.preventDefault(); 
    
    } 
    

, 나는 '이 경우 당신은 당신의 일반적인 addFavorite 메서드를 호출하고 그것을 유형, 예를 전달하는 기능을 제공 할 수 있습니다 옵션 # 1로 가십시오. 이것은, 예컨대 :

events : function() { 

    var events = {}; 

    var view = this; 

    [ 

    'favourite', 

    'like', 

    'dislike' 

    ].forEach(function (type) { 

    events[ "click #" + type ] = (function (type) { 

     return function (event) { 

     view.addFavourite(event, type); 

     }; 

    })(type); 

    }; 


    return events; 

}  
보다 역동적 만들 수있는 수

1

는 이벤트 구성 매개 변수를 지정하지 마십시오. 이벤트 설정은 조회로 사용되며, 메소드 서명이 아닌 이름으로 완료됩니다.

class GD.Views.Result extends Backbone.View 
    template: JST['mobile/templates/result'] 
    tagName: 'tr' 
    className: 'result' 
    events: 
    'click' : 'showDetail' 
    'click #favourite' : 'addFavourite' 
    'click #like' : 'addFavourite' 
    'click #dislike' : 'addFavourite' 
    render: -> 
    $(@el).html(@template(model: @model)) 
    this 
    addFavourite: (type) -> 
    event.preventDefault() 
    attributes = 
     id: @model.id 
     type: type 
     cluster: @model.cluster 
    @collection.create attributes, 
    wait: true 
    success: @updateIcons 
    error: @handleError 
    showDetail: -> 
    ... 
    updateIcons: -> 
    ... 
    handleError: -> 
    ... 
관련 문제