2012-09-03 3 views
1

나는 modifyCollection 이벤트 dblclick과 관련된 기능입니다 jeditable를 사용하여 내 컬렉션을 편집 할 수 있습니다. 나는 다음과 같은 코드를 가지고있다.jeditable 함수 내부에서 컬렉션을 전달하는 방법은 무엇입니까?

initialize : function(options) { 
     view.__super__.initialize.apply(this, arguments); 
     this.collection = this.options.collection; 
     this.render(); 
    }, 

render : function() { 
     var template = _.template(tpl, { 
      collectionForTemplate : this.collection , 
      }); 
      this.el.html(template); 
      return this; 
    }, 

modifyCollection : function (event){ 
     $('#name').editable(function(value, settings) { 
      return (value); 
     } 
     , 
      { onblur: function(value) { 
       this.modelID=event.target.nameID; 
        this.collection = this.options.collection; 

       console.log("This Collection is: " + this.collection); //Shows : undefined 
          // 
          this.reset(value); 
        $(this).html(value); 
        return (value); 
      } 
     }); 

idee는 모델을 업데이트 한 다음 jeditable을 사용하여 컬렉션을 업데이트한다. in place 편집은 잘 작동하지만 문제는 컬렉션에 함수를 전달할 수 없다는 것입니다. 내 컬렉션의 모든 변경 사항을 로컬에 저장하고 나중에 서버로 보내려고합니다. 여기서 내가 뭘 잘못하고 있니?

+1

글쎄, 내가 말할 수있는 한가지는'onblur()'함수의'this'가'this' 콜렉션을 가리키고 있지 않다는 것입니다. 'modifyCollection()'함수 안에'var self = this;'를 추가하고'onblur()'에서'this.collection'을'self.collection'으로 변경하십시오. – jmk2142

+0

고마워요. 그것은 작동합니다! – Amateur

+0

@orangewarp : 그 의견은 의심 스러울 정도로 대답과 같습니다. –

답변

1

다른 사람들이이 스레드를 발견하면 공식적인 답변으로 댓글을 이동했습니다.

onblur() 함수 내의 this 함수가이 컬렉션을 가리키고 있지 않습니다. 과 같이 self.collectiononblur() 변화 this.collection에 다음 modifyCollection() 함수 내에서 var self = this;를 추가하십시오 :

modifyCollection : function (event) { 

    var self = this; // Added this line 
    // When working with functions within functions, we need 
    // to be careful of what this actually points to. 

    $('#name').editable(function(value, settings) { 
     return (value); 
    }, { 
    onblur: function(value) { 
     // Since modelID and collection are part of the larger Backbone object, 
     // we refer to it through the self var we initialized. 
     self.modelID = event.target.nameID; 
     self.collection = self.options.collection; 

     // Self, declared outside of the function refers to the collection 
     console.log("This Collection is: " + self.collection); 
      self.reset(value); 

      // NOTICE: here we use this instead of self... 
      $(this).html(value); // this correctly refers to the jQuery element $('#name') 
      return (value); 
     } 
    }); 
}); 

UPDATE - 자체에주의를 예감이

@muistooshort는 self 사실의 속성임을 잘 언급한다 따라서 코드에 var self = this;을 선언하지 않으면 윈도우 obj를 참조하게됩니다. self이 존재하지만 작동하지 않는 이유를 잘 모르는 경우 가중 될 수 있습니다.

일반적으로 이런 유형의 코딩은 self 대신 that 또는 _this을 사용하는 경향이 있습니다. 당신은 경고를 받았습니다. ;-)

+0

'self'를 사용하는 것은 단점이 있습니다. 'window' 객체는 ['self'] (https://developer.mozilla.org/en-US/docs/DOM/window.self) 멤버를 가지므로'self'라는 전역 변수가 있습니다. 문제는'var self = this;'를 잊었을 때 여전히 발생하지만'self '는 기대하고있는'self '가 아닐 것이다. 나는이 교훈을 힘든 방법으로 배운 후에'var _this = this'로 전환했습니다. –

+2

'window.self'는 프레임을 사용할 때만 중요하다고 생각합니다. 나는 실제로 그것을 사용하지 않았기 때문에'window'와'window.self'의 차이점이 고대로부터 남은 것이 될지 모르겠습니다. '자기'를 사용하고 싶지 않을 때'그'와'이'는 가장 흔한 이름 인 것 같습니다. "'self.x()'를 치는 것은 함수가 아닙니다. 항상 범위 내에 'self'가 있다는 것을 모르는 경우 오류가납니다. –

+0

'var self = this;'를 선언하지 않았다면 줄까지 공황 상태에 빠졌습니다. 나는 변화를 고려할 것이다. 나는 '자기'대신에 '그'를 사용하는 사람들을 보았고 그 이유를 모두 이해하기 시작했습니다. 그렇지만 질문에 '창'을 사용하는 것과 '자체'창과 다른 점은 무엇입니까? 그들은 내 콘솔에서 똑같은 것을 가리키는 것 같습니다. – jmk2142

관련 문제