Ember access model inside a controller

ember-dataember.js

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'});
});

Best Solution

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

Related Question