Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access model from controller?

I can't seem to get the model from inside the controller, even though the controller seems to have a model property set. The following:

export default Ember.ObjectController.extend({
    init: function() {
        this._super();
        console.log(this.get('model'));
        console.log(this.model);
        console.log(this);
    }
}

prints out:

enter image description here

Any ideas?

like image 478
Felix Avatar asked Apr 21 '15 05:04

Felix


People also ask

How do models connect to view?

How to use the Model in View? Create “Index. cshtml” page in Home folder inside the View. To access the product model in view first, we need to import the Product model namespace then we can use the product model in view.

How pass data from controller model in MVC?

The other way of passing the data from Controller to View can be by passing an object of the model class to the View. Erase the code of ViewData and pass the object of model class in return view. Import the binding object of model class at the top of Index View and access the properties by @Model.

How can I check if a model is valid in controller?

You need to check whether the submitted data is valid or not in the controller. In other words, you need to check the model state. Use the ModelState. IsValid to check whether the submitted model object satisfies the requirement specified by all the data annotation attributes.

What is model view controller C#?

MVC (Model-View-Controller) is a pattern in software design commonly used to implement user interfaces, data, and controlling logic. It emphasizes a separation between the software's business logic and display. This "separation of concerns" provides for a better division of labor and improved maintenance.


2 Answers

So it turns out that when I examine the model by setting a break point it is empty. I assume the console shows content because it updates the model once the content arrives.

In init() the model is unreachable:

init: function() {
    this._super();
    console.log(this.get('model'));  // null
}

Same for any method .on('init'):

onInit: function() {
    console.log(this.get('model'));  // null
}.on('init'),

But the model is accessible to actions (I'm assuming because the model has been set up by the time the action is called):

someAction: function() {
    console.log(this.get('model'));  // model object as expected
}

So to answer my question, this.get('model') can be used to access the model from the controller, but just not in init() or .on('init') methods.

like image 166
Felix Avatar answered Sep 25 '22 05:09

Felix


The an Ember.ObjectController is a proxy for the model. So the model can be referenced using this as you found in your example. So then in a template {{this.aModelAttr}} or just {{aModelAttr}}. Like you question suggests it's a bit confusing.

The ObjectController is being deprecated as of Ember 1.11.0. So to simplify use Ember.Controller and in your controller you can reference the model by this.get('model')

Then in the template use {{model.aModelAttr}}

To give the model a domain specific ie: books or user name use

export default Ember.Controller.extend({
    domainSpecificName: Ember.computed.alias('model')
}

Then in your templates you can use {{domainSpecificName.aModelAttr}}

like image 43
kiwiupover Avatar answered Sep 24 '22 05:09

kiwiupover