javascript - Controller 之间的 EmberJS 绑定(bind)内容

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

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

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

最佳答案

圣牛。经过多日的发疯,事实证明这完全是最新的 ember-data 中的一个错误。在 fixtures 中,所有 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/

关于javascript - Controller 之间的 EmberJS 绑定(bind)内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13363413/

相关文章:

javascript - 函数无法计算页面元素的分数和更改值

jquery - 在 Jqgrid 编辑表单上显示成功消息

javascript - Chrome 应用程序/webview 中的 FB.login

javascript如何创建引用

ruby-on-rails - 如何在 Rails 中零碎地设置属性

java - 隐藏部分类公共(public)方法/接口(interface)的最简单方法

wpf - 将 ObservableCollection 绑定(bind)到 WPF ListBox

c# - 绑定(bind)到 CheckBox 并以 MVVM 方式执行命令

c# - 将命令绑定(bind)到 ListView 项目 MVVM 正确的方式

javascript - 在调整浏览器大小之前,平铺图像的脚本不会初始化