jeudi 10 août 2017

Logic for when 'this' is valid from component to service

I'm flummoxed. I have this working, but I have no idea why. Here's the setup... The service, stripped...

export default Ember.Service.extend({
    editableCat: null,
    saveCatChanges() {
        var editCat = editableCat;
this.get('editstate').saveJournalChanges();
...

The caller, which is a component activity... import Ember from 'ember';

export default Ember.Component.extend({
    editstate: Ember.inject.service('edit-state'),
    actions: {
        createCat() {
            WORKS this.get('editstate').saveCatChanges();
            FAILS Ember.get(this.get('editstate'), 'saveCatChanges')

        }
    }            
});

The "FAILS" version has no definition for "this" when it gets to the service. Really? So there's no "saveCatChanges" defined and it exceptions out. But the same code in the service, called with the WORKS variant, has a "this" and all is well.

Why? Huh? And the only reason I tracked this down was sheer frustration in trying to document it, so, huh, what?

Please help me understand. Ember is confusing me with when I need foo.bar vs Ember.get(this, 'foo').bar vs Ember.get(this,Ember.get(this, 'foo'),'bar), but this was especially unexpected.




Aucun commentaire:

Enregistrer un commentaire