Ember.js: controller issues, 'this', 'content' and model structure

I dig a little deeper into my first functional application and should better understand what is going on in my controller.

Here I have a controller that processes the action when the user clicks "Option". Looking at the this object, several questions arise:

  • What is this ? I expect this to be an instance of my Option model, but it lacks some properties (for example, "identity: 'model: Option").
  • If this is an instance of my Option model, why is the 'model' property undefined? Why doesn't he know that?
  • What is this.content ? It looks like some things are inside content ( id and isSuppressed ) and some are not ( this.isSelected ) - why is this?

Disclaimer Although there has not been any presentation problems so far, there may be errors in my ember application architecture.

Screen Debugging Controller: inspecting ember.js controller & data

Optional model and 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); } } }); 
+6
source share
1 answer

It depends on where this is, if your controller is a controller. If your controller is an ObjectController / ArrayController , it will proxy receive / set calls to the base model. content / model is the same in the controller context.

Properties rarely live directly on the instance, usually they are hidden to prevent access to properties without using getters / setters.

In the above code, there is a good chance that your OptionController should expand on the ObjectController . If the controller is not supported by the model. If you use Ember.Controller.extend , then it will not use proxy servers / setters for the model, it will store, retrieve properties from the controller itself.

+3
source

Source: https://habr.com/ru/post/959185/


All Articles