Access Model Using Ember Inside the Controller

How can I access the model from the controller? Currently, using the code below returns "undefined is not a function" (go to 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'}); }); 
+7
ember-data
source share
1 answer

I realized this in the end. plan = this.get('model') works for the action. It returns a model, and properties can be accessed using plan.get('price') . Not perfect, but he does his job. Why it didn't work, because it was inside a function that was called as a callback inside an action. Therefore, it is likely that the "this" scope was not executed for the callback function. I moved the callback function as an internal function inside the action, then executed the "this" area.

Regarding the scope problem, here the solution sets the application controller variable for the results returned from the AJAX call

+8
source share

All Articles