2
votes

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

1
Can't you just do directories.filter(function(dir){ return dir === null }) (or extract the dir === null part out) - Benjamin Gruenbaum
If you can get it up on jsbin for us to play with - it would make it easier to figure it out - Kalman
Hi Benjamin! Directory is only a root-directory if the parent directory (the relationship "directory" in directory-model) its attached to is null. Your example is good idea but to get the expected outcome i would have to do something like that: directories.filter(function(dir){return dir.get('directory').then(function(directory){return directory == null});}); with still leaves me with a promise - mnie
Updated the question with a jsbin! Thx in advance for looking over it. - mnie

1 Answers

2
votes

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?