2011-08-06 5 views
0

함수를 호출하려고하는데 정의되지 않은 함수라는 오류가 발생합니다.Backbone.View에서 함수를 호출 할 수 없습니다.

window.PackageView = Backbone.View.extend({ 

    tagName: "div", 

    className: "package-template", 

    events:{ 

     "click #display-name"  : "getNodeId",   
    }, 

    initialize: function() { 
     _.bindAll(this,'render', 'getNodeId', 'getAction');      
     this.template = _.template($('#package-template').html()); 
     $(this.el).html(this.template); //Load the compiled HTML into the Backbone "el" 
     nodeInstance.bind('reset', this.render, this); 
    }, 

    render: function() { 
     //.. no need to worry about this. 
    }, 


    getNodeId: function(){ 
     objectJSON = jAccess.getJSON(); // I get the JSON successfully 
     nodeIdArray = [] 
     _.each(objectJSON, function(action){ 
      _.each(action.Nodes, function(action){ 
       nodeIdArray.push(action.Id); 
        this.getAction(); // Here is the problem. I get an error 
      });  
     });  
    }, 

    getAction: function(){ 

     actionsArray = []; 
     objectJSON = jAccess.getJSON(); 
     _.each(objectJSON, function(action){ 
      _.each(action.Nodes, function(action){ 
       if(action.Id == 5) {  

       _.each(action.Actions, function(action){ 
        actionsArray.push(action.Name); 
       }); 
      } 
      }); 
     }); 
     console.log(actionsArray); 
    } 


}); 

어디서 잘못 될지 모르겠습니다. 도와주세요.

getNodeId: function(){ 
    var self = this; 
    objectJSON = jAccess.getJSON(); 
    nodeIdArray = [] 
    _.each(objectJSON, function(action){ 
     _.each(action.Nodes, function(action){ 
      nodeIdArray.push(action.Id); 
       self.getAction(); 
     });  
    });  
}, 

I을 :

나는, 당신이 할 수있을 것입니다 간단한 수정으로 평가 기능 ('this.getAction()')

+0

이것은 아마도 범위 문제 일 수 있습니다. getNodeId()의 시작 부분과 this.getAction() 이전의 행에서 루트 객체 console.log (this)의 값은 무엇입니까? 일치하지 않으면 범위 지정 문제가됩니다. – Gazler

+0

잘 모르겠습니다. 나는 확인해야 할 것이다. – JABD

+0

'this.getAction() '앞에 DOMWindow가 있습니다. – JABD

답변

0

아닌 '정의되지 않은'라는 오류를 얻을 수 밑줄 바인드 함수를 사용하는보다 우아한 해결책이 있다고 생각하면 수표를 받겠습니다.

EDIT 이 경우에 적용하지 않고, 편안 자원에서 컬렉션에 각각의 방법을 사용할 때 더 훌륭한 솔루션 만 가능하다. 예를 들어 메시지 모음이있는 경우 컨텍스트를 유지하려면 다음을 수행 할 수 있습니다.

addAllMessages: function(messages) { 
     messages.each(this.addMessage, this); 
    }, 
+0

죄송합니다! 그것은 일을했다. 감사! – JABD

+1

@ Eric : 백본 문서를 읽을 필요가 있습니다. 코딩 할 때 항상 열어 두십시오. http://documentcloud.github.com/backbone/#FAQ- 여기에서이 문제를 해결합니다. – PhD

+0

@ Nupul. 그래 니가 맞아. – JABD

관련 문제