javascript - 如何使用 View 中的复选框过滤 Ember.js 中 ArrayController 的内容集合?

标签 javascript ember.js ember-data

我正在为 Ember 和 Ember-Data 使用最新的代码。我有 rails 作为我的后端提供 JSON,它工作正常。我希望能够在我的 View 中使用复选框来过滤 Ember 模型的“事件”属性。如果我选中复选框,我希望复选框只显示 active=true 的数据。我不想从数组中删除数据,只想隐藏数据。这是我目前拥有的,但它不起作用。

型号:

App.Org = DS.Model.extend({
    code: DS.attr('string', { defaultValue: 'N/A' }),
    name: DS.attr('string', { defaultValue: 'N/A' }),
    source: DS.attr('string', { defaultValue: 'N/A' }),
    status: DS.attr('string', { defaultValue: 'N/A' }),
    type: DS.attr('string', { defaultValue: 'N/A' }),
    note: DS.attr('string', { defaultValue: 'N/A' }),
    financial_Flag: DS.attr('string', { defaultValue: 'N/A' }),
    expense_Flag: DS.attr('string', { defaultValue: 'N/A' }),
    revenue_Flag: DS.attr('string', { defaultValue: 'N/A' }),
    created_At: DS.attr('string', { defaultValue: 'N/A' }),
    updated_At: DS.attr('string', { defaultValue: 'N/A' }),

    active: function() {
        var status = this.get('status');
        var active = (status === 0) ? false : true;
        console.log("status: " + status + " | active: " + active);
        return active;
    }.property('status')

}).reopenClass({
    collectionUrl: '/orgs',
    resourceUrl: '/orgs/%@',
    resourceName: 'org'
});

数组 Controller :

App.OrgsController = Em.ArrayController.extend({

    isEmpty: function() {
        console.log("############ App.OrgsController.isEmpty called");
        return this.get('length') === 0;
    }.property('length'),

    toggleActive: function(){
        console.log("############ App.OrgsController.isActive called");
        return this.filterProperty('active', value).forEach(this.removeObject, this);
    }.property('@each.active'),

    init: function() {
        this.set('content', App.store.findAll(App.Org));
    },

    refreshOrgs: function() {
        this.set('content', App.store.findAll(App.Org));
    },

    getInactiveOrgs: function(){
        this.set('content', App.store.find(App.Org, {status: "0"}));
    }
});

在我看来我有:

<label>
    isActive
    {{view Ember.Checkbox checkedBinding="App.OrgsController.toggleActive" disabledBinding="App.OrgsController.isEmpty" }}
</label>

最佳答案

这可以通过创建一个计算属性来实现,该属性根据绑定(bind)到复选框的 bool 值返回整个集合或仅返回项目的子集。例如,

App.Post = DS.Model.extend({
    text: DS.attr('string'),
    active: DS.attr('boolean')
});

App.IndexRoute = Ember.Route.extend({
    model: function() {
        return App.Post.find();
    }
});

App.IndexController = Ember.ArrayController.extend({
    hideInactive: false,

    filteredContent: function() {
        var content = this.get('content');

        if (!content || !this.get('hideInactive'))
            return content;

        return content.filter(function(item) {
            return item.get('active');
        });
    }.property('content.isLoaded', 'hideInactive')
});

您的模板应如下所示:

<script type="text/x-handlebars" data-template-name="index">
    <p>{{view Ember.Checkbox checkedBinding="hideInactive"}} Hide Inactive Posts</p>
    <br/>

    {{#each filteredContent}}
        <p>{{text}}</p>
    {{/each}}
</script>

如果 active 被定义为计算属性,这也将起作用,如您的示例所示。

关于javascript - 如何使用 View 中的复选框过滤 Ember.js 中 ArrayController 的内容集合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12520785/

相关文章:

javascript - 在状态 `becameInvalid` 中尝试处理事件 'root.loaded.saved'

javascript - 仅显示段落的前两行

rest - 如何使用 RESTAdapter 处理单一资源

javascript - EmberJS 嵌套组件事件

javascript - Ember Js 中的自定义 session 。如何设置/访问 session 信息

javascript - Ember 如何在手动刷新页面时保留查询参数?

ember.js - 关于 ember 数据模型的弃用警告

javascript使用变量中的键创建一个对象

javascript - 使用箭头键移动对象的 Html 和 java 脚本?

javascript - React 中回调函数的返回值