2016-07-07 66 views
0

新到骨幹,並有一些麻煩。試圖以通用的,無代碼的方式提出這個問題,因爲我負責維護的應用程序長達數千行......希望我可以清楚。如何設置屬於父對象的兄弟模型的實例的屬性?

我有一個方法myMethod(),屬於一個模型App.Person。

我有一個包含App.Person的幾個實例的集合App.PersonList。

我有一個實例(myPersonList)App.PersonList,我在一個對象(myDonationForm)中創建,該對象是一個對象App.DonationForm的實例(並且我們在我的舒適區之外進一步漫遊:App.DonationForm擴展了一個名爲Controller的對象,該對象擴展了一個名爲Base的對象,它似乎是一個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

基本上,因爲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

我將使用一個簡化代碼片段更新我的問題。 – Sapphireblue

0

事件聚合器可能在這種情況下工作。

安裝擴展骨幹事件

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

在App.Errors初始化,listenTo事件

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

在App.Person的myMethod的,觸發事件

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

要回答你的一般問題更普遍,

  • 根據您的層次結構,Person對象根本不知道有關Errors對象的任何內容。兒童對象不(也不應該)知道他們父母的任何事情。然而,對象確實(大概)知道發生了「錯誤」,並且有人可能想知道這個錯誤。
  • 由於Person對象不知道更多,它只能觸發自定義事件。
  • 某些其他對象需要偵聽此自定義事件。根據您的層次結構,訪問對象的唯一其他對象是PersonList集合。
  • PersonList集合現在處於相同的情況。它從自定義事件中瞭解到一個錯誤已經發生,但它無法做任何事情,也不知道它的父母。所以它只能觸發另一個自定義事件。
  • DonationForm對象必須收聽PersonList集合中的自定義事件。當它收到事件時,它可以將它傳遞給Errors對象。

Voila。

相關問題