我正在尝试在 Ember 应用程序中设置批量操作复选框(例如“删除所选项目”)。这个想法是,如果选择了任何一个复选框,则显示或隐藏大规模操作下拉列表。我还没有放入下拉列表,因为我不知道如何观察数组中的所有元素。我怎样才能:
- 设置数组 Controller ,以便它观察所有客户端对象
- 获取选中项发生变化时的数量
- 另外:我是否符合处理客户端 itemController 的惯例?
app/templates/clients.hbs
<section id="clients">
<h4>my clients</h4>
<ul>
{{#each itemController="clients/client"}}
<li>
{{input type="checkbox" name="markedForDeletion" checked=markedForDeletion}}
{{#link-to 'clients.show' this}}
{{clientName}}
{{/link-to}}
</li>
{{/each}}
</ul>
</section>
{{#link-to 'clients.new'}}Add client{{/link-to}}
{{outlet}}
router.js
import Ember from 'ember';
import config from './config/environment';
var Router = Ember.Router.extend({
location: config.locationType
});
export default Router.map(function() {
this.resource('clients', function() {
this.route('show', {path: '/:client_id'});
this.route('new');
});
});
app/routes/clients.js
import Ember from 'ember';
export default Ember.Route.extend({
model: function() {
return this.store.find('client');
}
});
app/models/client.js
import DS from 'ember-data';
export default DS.Model.extend({
clientName: DS.attr('string'),
clientEmail: DS.attr('string')
});
app/controllers/clients.js
import Ember from 'ember';
export default Ember.ArrayController.extend({
checkBoxesChanged: function() {
// This fires only once, when the /clients/ route is activated
console.log('markedForDeletion in array changed');
}.observes('@each.clients')
});
app/controllers/clients/client.js
import Ember from 'ember';
export default Ember.ObjectController.extend({
markedForDeletion: true,
markedForDeletionChanged: function(){
// This fires correctly
console.log('markedForDeletion object changed');
}.observes('markedForDeletion')
});
编辑:提出类似问题here ,但恐怕这些答案对我帮助不大。
最佳答案
对此有几种解决方案。由于您的解决方案只需要检查额外的属性,因此我认为 ObjectProxy 将满足您的需求。但是,如果您需要更多功能,那么组件会更合适。
注意:在我们深入研究解决方案之前,请务必注意 ArrayController、ObjectController 和 ItemController 均已被弃用。
由于我们不会使用 itemController,因此您可以删除 app/controllers/clients/client.js
app/templates/clients.hbs
<section id="clients">
<h4>my clients</h4>
<ul>
{{#each clientsWithMarker as |client|}}
<li>
{{input type="checkbox" name="markedForDeletion" checked=client.markedForDeletion}}
{{#link-to 'clients.show' client}}
{{client.clientName}}
{{/link-to}}
</li>
{{/each}}
</ul>
</section>
{{#link-to 'clients.new'}}Add client{{/link-to}}
{{outlet}}
app/controllers/clients.js
import Ember from 'ember';
export default Ember.Controller.extend({
clientsWithMarker: Ember.computed.map('model', function(client) {
return Ember.ObjectProxy.create({
content: client,
checked: false
});
}),
// This computed property returns an array of ObjectProxies that
// are checked. It is recalculated automatically
checkedClients: Ember.computed.filterBy('clientsWithMarker', 'checked', true),
checkBoxesChanged: function() {
// This fires only once, when the /clients/ route is activated
console.log('markedForDeletion in array changed');
}.observes('clientsWithMarker.@each.checked')
});
这应该可以工作,但我还没有实际测试过这个特定的代码。
关于javascript - 观察 Ember.ArrayController 中的任何 Ember.ObjectController 何时发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29636032/