Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@gryzzly
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gryzzly/b09e3159b8b7bfaf8e94 to your computer and use it in GitHub Desktop.
Save gryzzly/b09e3159b8b7bfaf8e94 to your computer and use it in GitHub Desktop.

Example of a tedious task when using backbone MVC

You have three components each of which might have certain state. These components depend on one particular component that emits events. These three views have to all define the emitting component as a dependency and manually subscribe to the events in order to sync the state.

var View = require('lib/View'),
    source = require('source'); 
    
module.exports = View.extend({

  initialise: function () {
    source.on('start stop', syncState);
  }, 
  
  destroy: function () {
   source.off('start stop', syncState);
  }
});

function syncState () {
  // maybe toggle classes, call state handler
  this.setState('source-' + source.state);
}

We also have to manually set the event listeners and not to forget to remove them (this is improved by listenTo and stopListening).

How can this be solved by Flux architecture?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment