doub1ejack
doub1ejack

Reputation: 11171

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

Upvotes: 6

Views: 310

Answers (1)

Kingpin2k
Kingpin2k

Reputation: 47367

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.

Upvotes: 3

Related Questions