vendredi 24 avril 2015

controllers send action context in ember.js

I'm trying to triger an action from a controller to another

Controller A is trying to trigger action on controller B:

A{
  needs: ['B'],
  actions: {
    foo: function(bar) {
      this.get('controllers.B').send('foo', bar);
    },
  }
}

B{
  actions: {
    foo: function(bar) {
      console.log("foo bar");
    },
  }
}

Tha tis working as intended, th eproblem comes when I have more controllers referenced:

A{
  needs: ['B'],
  actions: {
    foo: function(bar) {
      this.get('controllers.B').send('foo', bar);
    },
  }
}

B{
  needs: ['B'],
  name: null,
  init: function () {
    this.get('controllers.C').addObserver('name', this, function (sender) {
      this.set('name', sender.get('name'));
    });
  },
  actions: {
    foo: function(bar) {
      console.log("foo bar: " + name);
    },
  }
}

C{
  name: "hello from C",      
}

Now when I call B.foo(bar) I get :

foo bar: hello from C

When I call A.foo(bar), I get:

foo bar: null

when I put a breakpoint in the function, in the forst case, name is defined, in the second case (with C). name is null

I guess I have context issue but can't find out...




Aucun commentaire:

Enregistrer un commentaire