The clickButton action has no access to the transitionToRoute function because it can’t see the component. It only sees “this” as what is declared in the contents of this file or what is implicitly added in other ways.
Why can’t actions allowed to see the component? I’ve never understood this!
many binding things (observer for example) happen with get
and set… its not a bad thing to use it. and pure this is not in class scope when usung it inside action.
I do use it when I know it has binding on it. But if I’m just calling a function it seems silly to do this.get(‘myfunc’)() only from actions but not from within the class itself.
Bit confused. Actions by default have the context of the component they are defined in. The only exception is when the action is overridden using an action closure.
It does generally work, but in a certain scenario it becomes difficult to access the service.
For instance, if I have an event click handled in a component, then you call the service function, then in that function in the service the “this” no longer points to the service. Or something like that, when I come across it again I’ll post here the code. But I end up having to jump through silly hoops.
I’m in a component class function, with a jquery asynchronous call inside it
simplifying:
export default Ember.Component.extend({
catalogueHooks: Ember.inject.service(),
e.g
LoadPlan() {
var service = this.get(‘catalogueHooks’);
Ember.$.when( API ).then(function(plan) {
service.get(‘setPlanData’)(JSON.parse(plan.plandata));
});
}
Then inside that setPlanData function in the service, “this” does not exist it is undefined.
e.g
setPlanData(value) {
alert("context:"+ this); // undefined this
var list = this.get('storedData.plans');
list[this.get('currentPlanIndex')].plan = value;
this.set('storedData.plans', list);
return value;
},
I suspect that this is my poor knowledge of general javascript scoping coming into play, I have the vaguest notion of ‘enclosure’ but to me if you call a function of an object, the “this” should be the instance that owns the function.
I end up having to pass the service function a copy of he service itself. Which is nasty hack.