2

I am using the latest code for both Ember and Ember-Data. I have rails as my backend supplying JSON, which is working fine. I want to be able to filter on the "active" property of the Ember model using a checkbox in my view. I want the checkbox to only show the data that is active=true if I check the checkbox. I do NOT want to delete the data from the array, only HIDE the data. This is what I have currently, but it is not working.

Model:

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

ArrayController:

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"}));
    }
});

In my view I have:

<label>
    isActive
    {{view Ember.Checkbox checkedBinding="App.OrgsController.toggleActive" disabledBinding="App.OrgsController.isEmpty" }}
</label>
thegrinner
  • 9,633
  • 5
  • 39
  • 64
Jason Cochran
  • 316
  • 3
  • 10
  • 1
    Not sure if it helps, but since you're using ember-data, maybe you want to use [`App.store.filter`](https://github.com/emberjs/data#filtering-loaded-records) instead of the controller's filterProperty`? – dechov Sep 20 '12 at 21:53
  • Hmmm...I'll give that a shot. Thanks! – Jason Cochran Sep 21 '12 at 19:30

1 Answers1

7

This can be achieved by creating a computed property that returns either the whole collection or just a subset of the items based on a boolean bound to the checkbox. For example,

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

Your template should look something like this:

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

This will also work if active is defined to be a computed property, as in your example.

ahmacleod
  • 4,180
  • 17
  • 42