I am getting a little deeper into my first functional app and need to better understand what it going on in my controller.
Here I have a controller that handles the action when a user clicks on an 'Option'. Looking at the this
object raises a few questions:
this
? I would expect it to be an instance of my Option model, but it is missing some properties (like "identity: 'model: Option'"). this
is an instance of my Option model, why is the 'model' property undefined? Why doesn't it just know that?this.content
? It looks like some stuff is inside content
(id
and isSuppressed
) and some is not (this.isSelected
) - why is that?Disclaimer: Though there aren't any presenting problems so far, there certainly could be errors in my ember app architecture.
Screenshot debugging controller:
Option Model & Controller
App.Option = Ember.Object.extend({
identity: 'model: Option',
id: '',
cost: '',
isSelected: false,
isSuppressed: false
});
App.OptionController = Ember.Controller.extend({
actions: {
toggleOption: function() {
this.set('isSelected', !this.get('isSelected'));
var id = this.get('content.id');
this.send('deselect', this.get('content.id'));
}
}
});
App.OptionsController = Ember.ArrayController.extend({
actions: {
deselect: function(exception) {
var opts = this.rejectBy('id', exception)
opts.setEach('isSuppressed', true);
}
}
});
What are the prime tasks that are performed by controllers in Ember. js? Decorating the model which is returned by the route is a very essential task that needs to be performed in Ember.
What is a Controller? A Controller is routable object which receives a single property from the Route – model – which is the return value of the Route's model() method. The model is passed from the Route to the Controller by default using the setupController() function.
In Ember Data, models are objects that represent the underlying data that your application presents to the user. Note that Ember Data models are a different concept than the model method on Routes, although they share the same name.
It depends where this
is, if your in the controller it's the controller. If your controller is an ObjectController
/ArrayController
it will proxy get/set calls down to the underlying model. content
/model
are the same thing in the context of the controller.
The properties rarely live directly on the instance, usually they are hidden to discourage accessing the properties without using the getters/setters.
In your code above there is a good chance your OptionController
should be extending ObjectController
. Unless the controller isn't backed by a model. If you use Ember.Controller.extend
then it won't proxy getters/setters down to the model, it will store, retrieve properties from the controller itself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With