2016-07-07 2 views
0

백본에 새롭게 등장하고 약간의 문제가 있습니다. 유지 보수가 필요하다는 신청 이후 일반적이고 코드가없는 방식으로 이것을 요청하려고 시도하는 것은 수천 줄에 걸친 것입니다 ... 제가 분명 할 수 있기를 바랍니다.부모 개체의 형제 인 모델 인스턴스의 특성을 설정하는 방법은 무엇입니까?

모델 App.Person에 속하는 myMethod() 메소드가 있습니다.

App.Person의 여러 인스턴스를 보유하고있는 컬렉션 App.PersonList가 있습니다.

개체 AppDonationForm의 인스턴스 인 개체 (myDonationForm) 내에서 인스턴스 (myPersonList) App.PersonList를 만들고 있습니다. 여기에 내 안락 영역 외부로 더 이동합니다. App.DonationForm 베이스라는 객체를 확장하는 Controller라는 객체를 확장합니다.이 객체는 base.js로 보이는 것으로 여기에 무슨 일이 일어나고 있는지 거의 알지 못하지만 즉각적인 필요는 없다고 생각합니다.)

또한 App.DonationForm에는 모델 App.Errors의 인스턴스 (myErrorMsg)가 있습니다. myMethod()에서 myErrors의 속성을 설정할 수는 있지만 myErrors를 참조하는 구문은 작동하지 않아 중첩 된 객체의 트리를 가로 지른 다음 다시 병렬 단계를 수행 할 수 없습니다.

나는 의미가 있기를 바랍니다. 그것을 시각화하려면 :

myDonationForm, inst of App.DonationForm, ext Controller 
|--myPersonList, inst of App.PersonList, ext Collection 
| |--myPerson[1], inst of App.Person, ext Model // I want to change from here 
| | +---myMethod() 
| |--myPerson[2], inst of App.Person, ext Model // or from here 
|  +---myMethod() 
+myErrorMsg, inst of App.Errors, ext Model  // an attribute of this. 

당신이 제공 할 수있는 포인터를 미리 주셔서 감사합니다.


(필자는 피어 리뷰 화면을 가지고 때까지 내가 실수로 우, 그것을 실현하지 않았다! Hoyen의 대답이 아니라 내 자신의 질문을 수정하려고)

App.SpecialDonationForm = App.DonationForm.extend({ 
    [...] 
    initialize: function(options){ 
     App.DonationForm.prototype.initialize.call(this, options); 
     [...] 
    }, 
    start: function(){ 
     App.DonationForm.prototype.start.call(this); 
     [...] 
     this.myPersonList = new App.PersonList(this.initialData); 
     this.myErrorMsg = new App.Errors(); 
     [...] 

답변

0

코드 스 니펫 (snippet)을 추가 편집 기본적으로 myErrorMsg는 Backbone 모델의 인스턴스이므로 Backbone의 모델 메소드를 사용하여 속성을 설정해야합니다. 따라서 다음과 같이 표시되어야합니다.

App.Person = Backbone.Model.extend(
{ 
    [...] 
    defaults: { 
     [...], 
     errorMsg: new App.Errors() 
    }, 
    [...] 
    myMethod: function(){ 
     var value = ""; // set it to what ever you like 
     this.get("errorMsg").set("message",value); // "message", is the attribute you want to update with the value 
     this.trigger('change:errorMsg'); // not sure if this is needed. but this will insure that this will trigger any event listeners on errorMsg 
    } 

}); 

App.SpecialDonationForm = App.DonationForm.extend({ 
    [...] 
    initialize: function(options){ 
     App.DonationForm.prototype.initialize.call(this, options); 
     [...] 
    }, 
    start: function(){ 
     App.DonationForm.prototype.start.call(this); 
     [...] 
     this.myPersonList = new App.PersonList(this.initialData); 
     this.myErrorMsg = new App.Errors(); 
     this.myPersonList.on('change:errorMsg',showError.bind(this)); 
     function showError(model,val,options){ 
      this.myErrorMsg.set(_.extend(this.myErrorMsg.defaults,model.toJSON()); 
     } 
     [...] 
+0

답장을 보내 주셔서 감사합니다. "Uncaught ReferenceError : myErrorMsg가 정의되지 않았습니다." – Sapphireblue

+0

App.Person 클래스에 myErrorMsg의 인스턴스가 없습니다. – Hoyen

+0

내 질문을 pared-down 코드 스 니펫으로 업데이트합니다. – Sapphireblue

0

이벤트 수집기가이 시나리오에서 작동 할 수 있습니다.

App.eventAgg.trigger('someEvent'); 
0

에 대답하기 위해 App.Person의 MyMethod라는에서는 이벤트

this.listenTo(App.eventAgg, 'someEvent', this.doSomeUpdate); 

listenTo App.Errors 동안

App.eventAgg = _.extend({object}, Backbone.Events); 

초기화 백본 이벤트를 확장 할 수

설정, 이벤트를 트리거 귀하의 일반적인 질문을 더욱 일반적으로,

  • Person 개체는 Errors 개체에 대해 전혀 알지 못합니다. 하위 객체는 부모에 대해 아무 것도 모릅니다.
  • Person 개체는 "오류"가 발생했으며 누군가가 그것에 대해 알고 싶어한다고 (아마도) 알고 있습니다.
  • Person 개체는 그 이상을 모르기 때문에 사용자 지정 이벤트를 트리거 할 수 있습니다.
  • 일부 다른 개체는이 사용자 지정 이벤트를 수신 대기해야합니다. 계층 구조에 따라 Person 개체에 액세스 할 수있는 유일한 개체는 PersonList 컬렉션입니다.
  • PersonList 컬렉션은 현재 동일한 상황입니다. 그것은 (사용자 지정 이벤트에서) 오류가 발생했음을 알았지 만 그것에 대해 아무 것도 할 수 없으며 부모에 대해 알지 못합니다. 그래서 그것이 할 수있는 일은 또 다른 맞춤 이벤트를 유발하는 것입니다.
  • DonationForm 개체는 PersonList 컬렉션의 사용자 지정 이벤트를 수신 대기해야합니다. 이벤트를 받으면 객체를 Errors 객체로 전달할 수 있습니다.

Voila.

관련 문제