5

我有以下两个模型:

App.Child = DS.Model.extend({
    name: DS.attr('string')
});

和:

App.Activity = DS.Model.extend({
    children: DS.hasMany('child',{async:true}),
    name: DS.attr('string')
});

对于hasMany关系,我想使用复选框在现有子项之间进行选择。

例如,我有这三个孩子:

App.Child.FIXTURES = [
  { id: 1, name: 'Brian' },
  { id: 2, name: 'Michael' },
  { id: 3, name: 'James' }
];

用户应该能够在创建或编辑活动时使用复选框来选择要添加到hasMany关系的子项。

我创建了一个 JSFiddle 来说明我的问题:http: //jsfiddle.net/Dd6Wh/。单击“创建新活动”以查看我要执行的操作。

基本上它与Ember.Select [ ... ] multiple="true"相同,但用于复选框。

Ember.js 这样的事情的正确方法是什么?

4

1 回答 1

14

您可以itemControllereach视图中使用帮助器来管理选择。在下面的代码中,我创建了一个名为ChildController

App.ChildController = Ember.ObjectController.extend({    
    selected: function() {
        var activity = this.get('content');
        var children = this.get('parentController.children');
        return children.contains(activity);
    }.property(),
    selectedChanged: function() {
        var activity = this.get('content');
        var children = this.get('parentController.children');
        if (this.get('selected')) {                                    
            children.pushObject(activity);            
        } else {                                    
            children.removeObject(activity);                                                    
        }        
    }.observes('selected')
});

使用 itemController,您可以公开一些属性和逻辑,而无需将其直接添加到您的模型中。在这种情况下,selected计算属性和selectedChanged观察者。

在您的模板中,您可以使用checkedBinding="selected". 因为itemController代理了每个模型,selecteditemcontroller的属性会被用到,并且{{name}}绑定,会查找模型的name属性:

<script type="text/x-handlebars" data-template-name="activities/new">
    <h1>Create a new activity</h1>

    {{#each childList itemController="child"}}
        <label>
            {{view Ember.Checkbox checkedBinding="selected"}}
            {{name}}
        </label><br />
    {{/each}}
    {{view Ember.TextField valueBinding="name"}}
    <button {{action create}}>Create</button>
</script>

编辑模板中的相同方法:

<script type="text/x-handlebars" data-template-name="activities/edit">
    <h1>Edit an activity</h1>

    {{#each childList itemController="child"}}
        <label>
            {{view Ember.Checkbox checkedBinding="selected"}}
            {{name}}
        </label><br />
    {{/each}}
    {{view Ember.TextField valueBinding="name"}}
    <button {{action update}}>Update</button>
</script>

这是这个工作http://jsfiddle.net/marciojunior/8EjRk/的小提琴

组件版本

模板

<script type="text/x-handlebars" data-template-name="components/checkbox-select">
    {{#each elements itemController="checkboxItem"}}
        <label>            
            {{view Ember.Checkbox checkedBinding="selected"}}
            {{label}}
        </label><br />
    {{/each}}    
</script>

Javascript

App.CheckboxSelectComponent = Ember.Component.extend({   
    /* The property to be used as label */
    labelPath: null,
    /* The model */
    model: null,
    /* The has many property from the model */
    propertyPath: null,
    /* All possible elements, to be selected */
    elements: null,
    elementsOfProperty: function() {
        return this.get('model.' + this.get('propertyPath'));
    }.property()
});

App.CheckboxItemController = Ember.ObjectController.extend({    
    selected: function() {        
        var activity = this.get('content');
        var children = this.get('parentController.elementsOfProperty');        
        return children.contains(activity);
    }.property(),
    label: function() {    
        return this.get('model.' + this.get('parentController.labelPath'));
    }.property(),
    selectedChanged: function() {
        var activity = this.get('content');
        var children = this.get('parentController.elementsOfProperty');
        if (this.get('selected')) {                                    
            children.pushObject(activity);            
        } else {                                    
            children.removeObject(activity);                                                    
        }        
    }.observes('selected')
});

更新了小提琴http://jsfiddle.net/mgLr8/14/

我希望它有帮助

于 2013-10-27T15:44:40.523 回答