vendredi 7 août 2020

Share service variable between controllers

I have a variable in a service that I want two share between two controllers. As I know, Ember services are singleton.

I created a service file: app/services/data-manipulation.js:

import Ember from "ember";

export default Ember.Service.extend({
  init() {
     console.log('initService');
  },
  shouldManipulate: false
});

In first controller that is called one screen before the second controller:

dataManipulation: Ember.inject.service(),
init() {
   this.updateManipulate();
},
updateManipulate: function() {
   this.set("dataManipulation.shouldManipulate", true);
   var currentValue = this.get("dataManipulation.shouldManipulate");
   console.log(currentValue); // log true as expected
}

In second controller:

dataManipulation: Ember.inject.service(),
init() {
  // it inits the service again so 'initService' is logged again.
  var currentValue = this.get("dataManipulation.shouldManipulate");
  console.log(currentValue); // log undefined
}

What is the problem and how can I make it works?




Aucun commentaire:

Enregistrer un commentaire