Search code examples
javascriptember.jspromisecomputed-properties

Ember.JS - Cannot filterBy or filter based on computed properties


I'm trying to filter the result of my hasMany-Array async-get with filterBy and condition "isRoot" == true. The "isRoot" property is a computed property and it seems that the filterBy function of ember doesn't wait for the promise to resolve. Here my code:

Model for Directory

App.Directory = DS.Model.extend(App.ModelEventHandler, {
   name: DS.attr('string', {defaultValue: ''}),
   users: DS.hasMany('user', {async: true}),
   isRootOfShare: DS.attr('boolean', {defaultValue: false}),
   directories: DS.hasMany('directory', {async: true, inverse: 'directory'}),
   directory: DS.belongsTo('directory', {async: true, inverse: 'directories'}),
   shares: DS.hasMany('share', {async: true}),
   files: DS.hasMany('file', {async: true}),
   isRoot: function () {
       var directoryPromise = this.get('directory');
       return directoryPromise.then(function (directory) {
           var isRoot = directory === null;
           return isRoot;
       }.bind(this));
}.property('directory')}

Model for User

App.User = DS.Model.extend(App.ModelEventHandler, {
   // Attributes begin
   email: DS.attr('string'),
   isCurrentUser: DS.attr('boolean', {defaultValue: false}),
   // Relationships
   directories: DS.hasMany('directory', {async: true}),
   shares: DS.hasMany('share', {async: true}) }

The statement I'm using to filter the directories:

user.get('directories').then(function (directories) {
        //TODO: Fix isRoot
        var filteredDirectories = directories.filterBy('isRoot', true);
        return filteredDirectories;
    });

Someone here who knows a solution for my problem? Thx in advance!

UPDATE

I made a JSBIN with shows my current problem. Here the link JSBIN link to my example


Solution

  • I came up with a solution myself. I'm using Ember.computed.equal instead of a computed property. Here a part of the code

    isRoot: Ember.computed.equal('directory.content', null),
    

    Working jsbin example http://jsbin.com/kiwujohefa/1/edit?js,output .

    Does somebody know whats the technical difference between a computed property and ember.computed.equal? I thought under the hood they work the same just that ember.computed.equal provides a specific functionality e.g. here equal?