Mar*_*tin 5 javascript ember.js ember-data
我有以下两种型号:
App.Child = DS.Model.extend({
name: DS.attr('string')
});
Run Code Online (Sandbox Code Playgroud)
和:
App.Activity = DS.Model.extend({
children: DS.hasMany('child',{async:true}),
name: DS.attr('string')
});
Run Code Online (Sandbox Code Playgroud)
我想使用复选框在hasMany关系中在现有子节点之间进行选择.
例如,我有这三个孩子:
App.Child.FIXTURES = [
{ id: 1, name: 'Brian' },
{ id: 2, name: 'Michael' },
{ id: 3, name: 'James' }
];
Run Code Online (Sandbox Code Playgroud)
用户应该能够在创建或编辑活动时使用复选框,以选择要添加到hasMany关系的子项.
我创建了一个JSFiddle来说明我的问题:http://jsfiddle.net/Dd6Wh/.点击"创建新活动"以查看我正在尝试执行的操作.
基本上它与Ember.Select [...] multiple ="true"相同,但是对于复选框.
对于像Ember.js这样的事情,正确的方法是什么?
Mar*_*ior 14
您可以itemController在each视图助手中使用a 来管理选择.在下面的代码中我创建了一个名为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')
});
Run Code Online (Sandbox Code Playgroud)
使用itemController,您可以公开一些属性和逻辑,而无需将其直接添加到模型中.在那种情况下,selected计算属性和selectedChanged观察者.
在模板中,您可以使用绑定选择checkedBinding="selected".因为itemController代理每个模型,selected所以将使用itemcontroller 的属性,并且{{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>
Run Code Online (Sandbox Code Playgroud)
编辑模板中的相同方法:
<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>
Run Code Online (Sandbox Code Playgroud)
这是一个小工具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>
Run Code Online (Sandbox Code Playgroud)
使用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')
});
Run Code Online (Sandbox Code Playgroud)
更新小提琴http://jsfiddle.net/mgLr8/14/
我希望它有所帮助
| 归档时间: |
|
| 查看次数: |
2541 次 |
| 最近记录: |