【问题标题】:EmberJS Binding Content Between ControllersEmberJS 在控制器之间绑定内容
【发布时间】:2012-11-02 00:56:54
【问题描述】:

我目前正在使用两个数据模型,其中 Foo 具有 Bars 类型的“toMany”属性。我现在正在尝试创建两个选择框,当第一个填充了 Foo 时,它会优化第二个列表,仅列出与该 foo 关联的条。

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>​

【问题讨论】:

    标签: javascript model-view-controller binding ember.js


    【解决方案1】:

    圣牛。经过很多天几乎疯了,事实证明这完全是最新的 ember-data 中的一个错误。在夹具中,所有 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/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-13
      • 2013-02-01
      • 1970-01-01
      • 2014-05-22
      • 2022-11-02
      • 2013-01-28
      相关资源
      最近更新 更多