2017-09-08 16 views
0

所以我試圖模仿組件如何做一種方式綁定,但使他們就像計算屬性一樣工作。你會如何做這樣的事情:通過類似計算的屬性綁定到Ember.Object.create

Parent = Ember.Object.extend({ 
    color: 'red', 
    init: function() { 
    this.set('child', Child.create({ parent: this, color: Ember.computed.alias('parent.color') })); 
    } 
}); 
Child = Ember.Object.extend({}); 

parent = Parent.create(); 

// returns AliasedProperty {altKey: "parent.color", _dependentKeys: Array(1)}... 
// I want 'red' instead 
parent.get('child.color'); 

所以我知道bindings are depreciated, 但我找不到灰燼1.10.1另一種方式。另外,they are asynchronous,我不想要。

我之所以這樣做是因爲在Ember.Object之間有數據下移動模式,所以我不想Child知道Parent中發生了什麼。有了這個功能,我可以用不同的綁定創建兩個Child實例。

因此,我想定義創建綁定/計算屬性。換句話說,將Parent的屬性傳遞給Child,但綁定在Create上。

任何人有想法嗎?

+0

恩,你的代碼有什麼問題?除了使用拼寫錯誤之外,比如在使用之前定義'Child',並且您可能意味着'Ember.computed.alias'而不是'Ember.compute'。你也應該在這個類上定義別名,但是在早期的ember版本中,它也可以在創建時使用,而我不知道它是否爲'1.10',所以這可能是有效的。另外我希望你能意識到Ember'1.10'是古老的! – Lux

+0

更新了問題。對於錯字感到抱歉。更新了問題。它'別名'是無效的創建.....不知道更新的版本。會喜歡創建一些解決方案。 – s12chung

+0

你是什麼意思「模仿如何組件做一種方式綁定」? – locks

回答

0

只是傳遞color: Ember.computed.alias('parent.color').extend

this.set('child', Child.create({ parent: this })); 

和預期的一樣,將工作:

Child = Ember.Object.extend({ 
    color: Ember.computed.alias('parent.color') 
}); 

調用.create當後來只設置父。

+0

不完全是我想要的,因爲這意味着父母知道孩子。我正在尋找像hbs組件如何工作的東西'{{child-component color = color}}'(孩子對父母一無所知) – s12chung