vendredi 22 avril 2016

ember communication (many clients) with server simultaneously

This question may seem basic in ember terms, but I'm having difficulty implementing a solid solution.

Basically I have a toggle (ember-cli-toggle), which works with a model to determine a boolean state in a firebase database. When one user turns it on, I want every client's toggle switch to turn itself on, and visa-versa for off.

I've implemented a working solution below, however, an astute observer can see that this could create an infinite loop of toggling a switch from state to state. If several clients click a toggle switch at the same time, the / actively pushes updates to the server, which affects the model and the whole loop starts over again.

Does anyone know a better Ember way to go about this? I was considering looking more into Observers and adding properties to the controller for isPushed, etc, but I thought the community might be able to point me in the right direction instead.

Controller: device.js

import Ember from 'ember';

export default Ember.Controller.extend({
  model: null,

  actions: {
    toggleSwitch(isToggled, toggleName) {
      switch(toggleName) {
        case 'switchOne': {
          if (isToggled) {
            console.log("Switch one is on!");
            Ember.$.getJSON("http://IP/api/force/io/1/1", function(response) {
              console.log(response);
            });
          }
          else {
            console.log("Switch one is off!");
            Ember.$.getJSON("http://IP/api/force/io/1/0", function(response) {
              console.log(response);
            });          
          }
          break;
        }
        etc (other cases) . . . 
    }
  }
});

Handlebars: device.hbs



<div>
  Switch One
  <br />
  
    
  
    
     
</div>

Model: device.js

import DS from 'ember-data';

export default DS.Model.extend({
  force_mode: DS.attr('number'),
  io_one: DS.attr('number'),
  io_two: DS.attr('number'),
  io_three: DS.attr('number')
});




Aucun commentaire:

Enregistrer un commentaire