rjoxford
rjoxford

Reputation: 361

Set controller property to first model

I want to set a controller property to the first object of the models but can't get the syntax right.

My application route...

model: function(){
    return this.store.find('group');
}

And then in my application controller, I'm trying something like...

activeGroup: function(){
        return this.get('model.firstObject');
    },

I've got a function a little later to set activeGroup to whichever is chosen, but I want it to be set as the first (or any) class by default.

Thanks!

Upvotes: 0

Views: 98

Answers (1)

Grapho
Grapho

Reputation: 1654

Your model hook should look like this:

model: function() {
    return this.store.find('group').then(function(results) {
      return results.get('firstObject');
    });
}

JSBIN: http://emberjs.jsbin.com/kukeji/1/edit

..or if you still want to use the full model array elsewhere... just use in your Controller

theFirstModel: Ember.computed.alias('model.firstObject')

http://emberjs.jsbin.com/movuzo/1/edit?html,css,js,output

Upvotes: 2

Related Questions