10

How can a model be accessed from within a controller ? Currently using the below code returns a "undefined is not a function" (go figure JS fail...).

models/plan.js

import DS from 'ember-data';

export default DS.Model.extend({
    name:       DS.attr('string'),
    period:     DS.attr('number'),
    price:      DS.attr('number'),
});

routes/checkout.js

import Ember from 'ember';

export default Ember.Route.extend({

    model: function(params) {
        return this.store.find('plan', params.plan_id);
    }
});

controllers/checkout.js

import Ember from 'ember';

export default Ember.Controller.extend({

    submitPayment: function(error, result)
    {
          var plan = this.get('model');
    }
}

router.js

Router.map(function() {
  this.route('checkout', {path: '/checkout/:plan_id'});
});
Bogdan Zurac
  • 6,348
  • 11
  • 48
  • 96
  • maybe 'this' is not the controller in 'submitPayment'. Is submitPayment an action? If so you should put it inside an actions object. Or checkout the usuals, is model being fetched in the model hook? – blessanm86 Jan 20 '15 at 15:49
  • submitPayment is just a function being called from within an action. Yes, the model is being stored, no problems there. – Bogdan Zurac Jan 20 '15 at 15:51
  • 1
    Instead of extending 'Ember.Controller', try 'Ember.ObjectController'. – blessanm86 Jan 20 '15 at 15:55
  • No difference. Altough if I user this.get('model') inside the action instead of the function from above, I get an object. It has only the id set however, instead of all the fields. Why is that ? Also, if I use this.get('model.price') I get the actual price. So how can I get the entire model inside a single variable ? – Bogdan Zurac Jan 20 '15 at 16:06
  • Im not that familiar with ember data. You might need to call this.get('model').toJSON() – blessanm86 Jan 20 '15 at 16:19
  • If you're not familiar, why are you commenting? No, you do **not** need to call `toJSON` on the model. –  Jan 20 '15 at 17:05
  • Nothing is obviously wrong here. What line is the error message occurring on? –  Jan 20 '15 at 17:05
  • I've figured it out eventually. plan = this.get('model') works for the action. It returns the model, and the properties can be accessed with plan.get('price'). Not ideal, but it gets the job done. Why it didn't work is because it was inside a function that was called as a callback from inside the action. So probably the scope of "this" wasn't carried out to the callback function as well. I moved the callback function as an inner function inside the action, then "this" scope worked. – Bogdan Zurac Jan 20 '15 at 17:28
  • Any idea though as to why you can't access a controller variable from a JS callback (ex. AJAX success callback) ? If I want to use this.set('variable', value); inside the AJAX success callback, I get the undefined is not a function error. Nvm, found this: http://stackoverflow.com/questions/20985916/setting-an-application-controller-variable-to-results-returned-from-ajax-call – Bogdan Zurac Jan 23 '15 at 19:32

1 Answers1

13

I've figured it out eventually. plan = this.get('model') works for the action. It returns the model, and the properties can be accessed with plan.get('price'). Not ideal, but it gets the job done. Why it didn't work is because it was inside a function that was called as a callback from inside the action. So probably the scope of "this" wasn't carried out to the callback function as well. I moved the callback function as an inner function inside the action, then "this" scope worked.

As for the scope problem, here's the solution setting an application controller variable to results returned from AJAX call

Community
  • 1
  • 1
Bogdan Zurac
  • 6,348
  • 11
  • 48
  • 96
  • I'm not able to use this.get() inside a controller. Ex: export default Ember.Controller.extend({ model: this.get('model'), message: this.model.message, .... .. – Vaibhav Sep 14 '16 at 11:48
  • 2
    Figured it out. It works like this in a controller: message: Ember.computed.alias('model.message'),... – Vaibhav Sep 14 '16 at 11:55