Accessing model from controller

In documentation it states that model becomes controller property, but when i am trying to access it from controller i am getting “Uncaught Type error: Undefined is not a function” on “this.get(‘content’)” in controller. Another thing i noticed in EmberInspector its that controller is : (generated cards controller), while i have controller defined, It’s a little confusing. Code:

App.CardsRoute = Ember.Route.extend({
    model: function() {
     return Ember.$.getJSON('/cards');
    },


   setupController: function(controller, model) {
    controller.set('content', model);
  }
});

App.CardsController = Ember.ArrayController.extend({
    data: this.get('content'),
...

});

You have to write it this (no pun intented) way:

App.CardsController = Ember.ArrayController.extend({
  data: Ember.computed.alias('content'),
...

Actually, it’s redundant in my eyes, because you could access “content” (or “model”) directly where you need the object.

Another side-note, if your only code in your setupController is the controller.set('content', model); line, you could delete the whole function setupController, because Ember does this for you.

2 Likes

Yes thats right… And 2 things… If you ever call the setupController hook, you should call

this._super(controller, model)

and also you should always set the model, not the content of a controller to be future read:

 controller.set('model', model);
3 Likes

Thank you for your replies. If i am understanding correctly the Ember.computed.alias in your reply is accessing whole model but not strictly data returned by model. I needed to access JSON data from model, i used “this.model” now in controller. From what i see in console log “this.model” is returning array of data arrays.

  App.CardsRoute = Ember.Route.extend({
    model: function() {
     return Ember.$.getJSON('/cards');
    }
});

This is what is server on path /cards returning :

[[1317888000000,372.5101],[1317888060000,372.4]]

I want to use that data in my ember HighStock (from HighCharts) implementation. It’s drawing chart with this manually entered data:

   App.CardsController = Ember.ArrayController.extend({
        series: [{
               name : 'test',
                    type: 'area',
                    data :[[1317888000000,372.5101],[1317888060000,372.4]],
...

But not drawing with this:

App.CardsController = Ember.ArrayController.extend({
    series: [{
                    name : 'test',
                    type: 'area',
                    data : this.model,
    ...

From what i see in console, this.model is returning not only array with arrays of data but other ember specific objects too, is that the problem? if yes then how to access only JSON returned data so i can use it in controller?

Anyone ? I am really getting tired of ember, it’s the second thing i’ve spent hours on. I can do the same using jquery in couple of minutes and everything works as intended. But when i try to implement the same into ember i can’t get it to work.

Sorry, I really don’t want to criticize because I had to go the same road last year, but Ember isn’t just an in-place replacement for jQuery. It’s an abstraction for whole applications, and their parts are: routes, models, controllers. Also, it presumes that you totally (or in parts) understand how JavaSript works.

Coming back to your problem: the “data: this.model” is wrong in your series property. Try this way for testing it:

series: function() {
    return [
        { name: 'test', type: 'area', data: this.get('model') }
    ];
}.property('model'),

This is defining a computed property series which is recalculated every time the controller’s property model changes. Thus the `property(‘model’) at the end.

I made the same experience of that steep learning curve, but I can assure you that at some point it pays off 100 times the effort.

3 Likes

Thank you so much! It works! I was frustrated with ember, was thinking about changing it maybe to Angular but i think i will try ember a little more because i really love it for everything else i’ve already did in it (even if i’ve spent a lot of time on something). This was the first time i couldn’t handle it myself, that’s why i asked for help. And thx for explanation it really makes sense now. Thank you once again.

1 Like

So, if I have actions on the route if I am using a setupController, will it automatically bubble up to the route from the template.