简体   繁体   English

Ember.js有很多复选框列表

[英]Ember.js hasMany as list of checkboxes

I have the following two models: 我有以下两种型号:

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

And: 和:

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

I want to use checkboxes to choose between the existing children, for the hasMany relation. 我想使用复选框在hasMany关系中在现有子节点之间进行选择。

For example, I have these three children: 例如,我有这三个孩子:

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

The user should be able to use checkboxes, while creating or editing an activity, for choosing which children, to add to the hasMany relation. 用户应该能够在创建或编辑活动时使用复选框,以选择要添加到hasMany关系的子项。

I've created a JSFiddle to illustrate my question: http://jsfiddle.net/Dd6Wh/ . 我创建了一个JSFiddle来说明我的问题: http//jsfiddle.net/Dd6Wh/ Click 'Create a new activity' to see what I'm trying to do. 点击“创建新活动”以查看我正在尝试执行的操作。

Basically it's the same as Ember.Select [ ... ] multiple="true" , but for checkboxes. 基本上它与Ember.Select [...] multiple =“true”相同 ,但是对于复选框。

What's the correct approach for something like this with Ember.js? 对于像Ember.js这样的事情,正确的方法是什么?

You can use an itemController in your each view helper to manage the selection. 您可以在each视图助手中使用itemController来管理选择。 In the code below I created one called ChildController : 在下面的代码中,我创建了一个名为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')
});

With a itemController you can expose some properties and logics, without add it directlly to your models. 使用itemController,您可以公开一些属性和逻辑,而无需将其直接添加到模型中。 In that case the selected computed property and the selectedChanged observer. 在这种情况下, selected计算属性和selectedChanged观察者。

In your template, you can bind the selection using checkedBinding="selected" . 在模板中,您可以使用checkedBinding="selected"绑定选择。 Because the itemController proxy each model, the selected property of the itemcontroller will be used, and the {{name}} binding, will lookup the name property of the model: 因为itemController代理每个模型,所以将使用itemcontroller的selected属性,而{{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>

The same aproach in edit template: 编辑模板中的相同方法:

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

This is a fiddle with this working http://jsfiddle.net/marciojunior/8EjRk/ 这是一个小工具http://jsfiddle.net/marciojunior/8EjRk/

Component version 组件版本

Template 模板

<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 使用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')
});

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

I hope it helps 我希望它有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM