effel
effel

Reputation: 260

Ember.js: How to query for records using multiple arguments?

I have day records which contain task records through a hasmany-relationship.

I want to query for only the completed tasks of a specific day, so they have to match two arguments. What's the correct way to achieve this and why is none of my approaches working?

App.DayIndexController = Ember.ObjectController.extend({
    ...
   results: function(){

        var date = this.get('model.id');

        return ...

    }.property(),
    ....
});

What I tried among others:

When I query with 1 parameter it returns the correct records.

// returns completed tasks
return this.store.find('task', {isCompleted: true}); 

When I query with 2 parameters, it ignores the second one.

// returns ONLY completed
return this.store.find('task', {isCompleted: true, dayName: date})  tasks

Returns an object, but I need an array to loop over:

return this.store.find('task', {dayName: date}).then(function(tasks){
    return tasks.filterBy('isCompleted', true);
});

Returns an object as well:

return this.store.find('task', {dayName: date}).then(function(tasks){
    return tasks.find('task', {isCompleted: true});
});

Model:

App.Task = DS.Model.extend({
    dayName: DS.attr('string'),
    title: DS.attr('string'),
    isCompleted: DS.attr('boolean'),
    quarters: DS.hasMany('quarter', {async: true})
});

I'm using Ember Data 1.0.0-beta with LSAdapter.

Upvotes: 2

Views: 2653

Answers (2)

effel
effel

Reputation: 260

Thank you all for the help! I finally got it working with a computed property.

App.DayIndexController = Ember.ObjectController.extend({

    items: function() {
        return this.get('model.tasks');
    }.property(),

    actions: {
        filterCompleted: function(){
            var tasks = this.get('model.tasks').filterBy('isCompleted');
            this.set('items', tasks);
        },

        filterAll: function(){
            this.set('items', this.get('model.tasks'));
        },

        filterActive: function(){
            var tasks = this.get('model.tasks').rejectBy('isCompleted', true);
            this.set('items', tasks);
        }
    }
});

Template:

{{#each task in items}}
    {{task.title}}
{{/each}}

<button {{action "filterAll"}}>All</button>
<button {{action "filterCompleted"}}>Completed</button>
<button {{action "filterActive"}}>Active</button>

Upvotes: 0

Kingpin2k
Kingpin2k

Reputation: 47367

it might look like an object, but really it's a collection. You can iterate it using forEach or a simple for loop, then objectAt.

this.store.find('task', {dayName: date}).then(function(tasks){
  tasks.forEach(function(task){
    console.log(task.get('name'));
  });
});


this.store.find('task', {dayName: date}).then(function(tasks){
  for(var i =0,len=tasks.get('length');i<len;i++){
     var task = tasks.objectAt(i);
  }
});

As to it ignoring multiple query items, it is programmed to handle multiple query items, but whether or not it works, I'm not positive. Toss a debugger here: https://github.com/rpflorence/ember-localstorage-adapter/blob/master/localstorage_adapter.js#L188

Upvotes: 2

Related Questions