mardi 3 octobre 2017

Ember Controller Data Accessed From Other Places

I have a websocket connection getting live price data in my application.js controller. I put it in here because I want the connection to initialize and the data to be accessed from any page and not reconnected every page load.

I have no idea how to access this data from other controllers or templates.

2 things I want to do: 1) Check for a value and update a dynamic price on every page in the navigation bar. 2) Access the websocket data in other controllers. I don't know if this would affect the message handler already defined in application.js. I guess the event data should be going into a variable.

application.js

headerPrice: 0,

setupWebsocket: function() {
  this._super();
  var socket = this.get('websockets').socketFor('ws://localhost:7000/');
  socket.on('open', this.myOpenHandler, this);
  socket.on('message', this.myMessageHandler, this);
  socket.on('close', function(event) {
    console.log('Websocket closed');
  }, this);
}.on('init'),

myOpenHandler: function(event) {
  console.log('Websocket Opened: ' + JSON.stringify(event));
},

myMessageHandler: function(event) {
  if (event.data == "foo") {
     this.headerPrice = event.data.price;
  };
}

}




Aucun commentaire:

Enregistrer un commentaire