Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember.js: questions concerning controllers, 'this', 'content' and model structure

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:

  1. What exactly is this? I would expect it to be an instance of my Option model, but it is missing some properties (like "identity: 'model: Option'").
  2. If this is an instance of my Option model, why is the 'model' property undefined? Why doesn't it just know that?
  3. What is 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: inspecting ember.js controller & data

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);
        }
    }
});
like image 213
doub1ejack Avatar asked Dec 02 '13 17:12

doub1ejack


People also ask

What is the prime task performed by controllers in Ember JS?

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 are controllers 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.

WHAT IS models in Ember JS?

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.


1 Answers

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.

like image 72
Kingpin2k Avatar answered Oct 15 '22 01:10

Kingpin2k