2012-11-13 78 views
8

我目前正在使用兩個數據模型,其中Foo具有類型爲Bars的「toMany」屬性。我現在試圖創建兩個選擇框,其中當第一個填充Foo的時候,它會優化第二個列表,只有與該foo關聯的酒吧。EmberJS控制器之間的綁定內容

的jsfiddle這裏:下面http://jsfiddle.net/drew/6jLCy/

代碼,但它肯定是不行的。它儘可能爲第一個設置SelectBox值,但不會使用相應的欄值標題填充第二個值。

App = Em.Application.create(); 

App.store = DS.Store.create({ 
    revision: 7, 
    adapter: DS.fixtureAdapter 
}); 

/************************** 
* Models 
**************************/ 

App.Foo = DS.Model.extend({ 
    bars: DS.hasMany('App.Bar'), 
    title: DS.attr('string') 
}); 
App.Bar = DS.Model.extend({ 
    foos: DS.hasMany('App.Foo'), 
    title: DS.attr('string') 
}); 


/************************** 
* Fixtures 
**************************/ 

App.Foo.FIXTURES = [ 
    { 
     id: 0, 
     title: 'Footitle 1', 
     bars: [0,1] 
    }, 
    { 
     id: 1, 
     title: 'Footitle 2', 
     bars: [0,1,2] 
    } 
]; 

App.Bar.FIXTURES = [ 
    { 
     id: 0, 
     title: 'Bartitle 1', 

    },{ 
     id: 1, 
     title: 'Bartitle 2' 
    },{ 
     id: 2, 
     title: 'Bartitle 3' 
    } 
]; 


/************************** 
* Views 
**************************/ 

App.SetFooField = Em.Select.extend({ 
    contentBinding: 'App.fooController', 
    valueBinding: 'content.selected', 
    optionLabelPath: 'content.title' 
}); 

App.SetBarField = Em.Select.extend({ 
    contentBinding: 'App.barController', 
    valueBinding: 'content.selected', 
    optionLabelPath: 'content.title' 
}); 

/************************** 
* Controllers 
**************************/ 

App.fooController = Em.ArrayController.create({ 
    content: App.store.findAll(App.Foo) 
}); 

App.barController = Em.ArrayController.create({ 
    contentBinding: 'App.fooController.selected.bars' 
});​ 

標記爲HTML:

<script type="text/x-handlebars"> 

    {{view App.SetFooField}} 
    {{view App.SetBarField}} 

</script>​ 

回答

1

聖牛。在經歷了幾天的瘋狂之後,事實證明這完全是最新的餘燼數據中的一個錯誤。在燈具中,所有的ID都需要是字符串。只是。平原。堅果。

/************************** 
* Fixtures 
**************************/ 

App.Foo.FIXTURES = [ 
    { 
     id: '0', 
     title: 'Footitle 1', 
     bars: ['0','1'] 
    }, 
    { 
     id: '1', 
     title: 'Footitle 2', 
     bars: ['0','1','2'] 
    } 
]; 

App.Bar.FIXTURES = [ 
    { 
     id: '0', 
     title: 'Bartitle 1', 

    },{ 
     id: '1', 
     title: 'Bartitle 2' 
    },{ 
     id: '2', 
     title: 'Bartitle 3' 
    } 
]; 

failed to get embedded's object property using ember.js with ember-data

巨大的感謝@dgeb回答這個問題。

jsfiddle相應更新。

http://jsfiddle.net/drew/6jLCy/

相關問題