Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Flux: A simple architecture model to build Clie...

Flux: A simple architecture model to build Client-side apps!

Flux is the application architecture that Facebook uses for building client-side web applications. It complements React's composable view components by utilizing a unidirectional data flow. It's more of a pattern rather than a formal framework, and you can start using Flux immediately without a lot of new code.

Pedro Nauck

May 14, 2015
Tweet

More Decks by Pedro Nauck

Other Decks in Programming

Transcript

  1. by

  2. “ The dispatcher is the central hub that manages all

    data flow in a Flux application. It is essentially a registry of callbacks into the stores and has no real intelligence of its own — it is a simple mechanism for distributing the actions to the stores. Dispatcher http://facebook.github.io/flux
  3. ✓ Central hub of your app ✓ Only one dispatcher

    per application ✓ Manage data based on named actions ✓ Simple way to apply on your app ✓ Register callback to the stores Dispatcher
  4. Store All actions are registered on the dispatcher, receives some

    payload, then are sent to a specific store. Dispatcher Callbacks Store Action Action
  5. The dispatcher exposes a method that allows us to trigger

    a dispatch to the stores, and to include a payload of data, which we call an action. Actions http://facebook.github.io/flux
  6. Actions var AppDispatcher = require(‘flux').Dispatcher; var Action = { createItem:

    function(text) { AppDispatcher.dispatch({ type: 'ADD_ITEM', data: { text: text } }) } }; creating your action
  7. Actions var AppDispatcher = require(‘flux').Dispatcher; var Action = { createItem:

    function(text) { AppDispatcher.dispatch({ type: 'ADD_ITEM', data: { text: text } }) } }; creating your action require dispatcher
  8. Actions var AppDispatcher = require(‘flux').Dispatcher; var Action = { createItem:

    function(text) { AppDispatcher.dispatch({ type: 'ADD_ITEM', data: { text: text } }) } }; creating your action
  9. Actions var AppDispatcher = require(‘flux').Dispatcher; var Action = { createItem:

    function(text) { AppDispatcher.dispatch({ type: 'ADD_ITEM', data: { text: text } }) } }; creating your action dispatch your action
  10. Action Dispatcher When the user handle some action, the view

    triggers an action to the dispatcher View View triggers
  11. Stores contain the application state and logic. Their role is

    somewhat similar to a model in a traditional MVC, but they manage the state of many objects — they do not represent a single record of data like ORM models do. Nor are they the same as Backbone's collections. More than simply managing a collection of ORM-style objects Stores “ http://facebook.github.io/flux
  12. ✓ Just a simple object-literal ✓ Contain the application state

    and logic ✓ Public interface: getters, no setter ✓ Setup: register with the dispatcher ✓ Emits a change event ✓ Receives data through a payload Stores
  13. Payload Store Store receives new data in the form a

    payload, that is just a object literal registered on the dispatcher. Dispatcher Payloads
  14. var _data = []; var Store = { getAll: function(){

    return { data: _data }; } }; Stores receiving data
  15. var _data = []; var Store = { getAll: function(){

    return { data: _data }; } }; Stores receiving data
  16. var _data = []; var Store = { getAll: function(){

    return { data: _data }; } }; Stores receiving data
  17. var _data = []; var Store = { getAll: function(){

    return { data: _data }; } }; Stores private data receiving data
  18. View A view is updated when the store emit some

    change event. Store change event Updating Views
  19. Stores based on events addChangeListener: function(callback) { this.on('change', callback); },

    removeChangeListener: function(callback) { this.removeListener('change', callback); }, emitChange: function() { this.emit('change'); }
  20. Stores based on events addChangeListener: function(callback) { this.on('change', callback); },

    removeChangeListener: function(callback) { this.removeListener('change', callback); }, emitChange: function() { this.emit('change'); }
  21. Stores based on events addChangeListener: function(callback) { this.on('change', callback); },

    removeChangeListener: function(callback) { this.removeListener('change', callback); }, emitChange: function() { this.emit('change'); }
  22. Stores based on events addChangeListener: function(callback) { this.on('change', callback); },

    removeChangeListener: function(callback) { this.removeListener('change', callback); }, emitChange: function() { this.emit('change'); }
  23. dispatcherIndex: AppDispatcher.register(function(payload) { switch(payload.type) { case 'ADD_ITEM': _data.push(payload.data); this.emitChange(); break;

    case 'REMOVE_ITEM': // logic to remove data break; } }) Stores registering on the dispatcher emit change to the view
  24. var ItemsAPI = require('./ItemsApi'); var AppDispatcher = require('flux').Dispatcher; var Action

    = { searchItems: function(query) { AppDispatcher.dispatch({ type: 'SEARCH_ITEMS' }); ItemsApi.search(query).done(function(data) { AppDispatcher.dispatch({ type: 'SEARCH_ITEMS_SUCCESS', data: data }); }); } }; Actions fetching data from remote
  25. var ItemsAPI = require('./ItemsApi'); var AppDispatcher = require('flux').Dispatcher; var Action

    = { searchItems: function(query) { AppDispatcher.dispatch({ type: 'SEARCH_ITEMS' }); ItemsApi.search(query).done(function(data) { AppDispatcher.dispatch({ type: 'SEARCH_ITEMS_SUCCESS', data: data }); }); } }; Actions fetching data from remote
  26. var ItemsAPI = require('./ItemsApi'); var AppDispatcher = require('flux').Dispatcher; var Action

    = { searchItems: function(query) { AppDispatcher.dispatch({ type: 'SEARCH_ITEMS' }); ItemsApi.search(query).done(function(data) { AppDispatcher.dispatch({ type: 'SEARCH_ITEMS_SUCCESS', data: data }); }); } }; Actions fetching data from remote
  27. var ItemsAPI = require('./ItemsApi'); var AppDispatcher = require('flux').Dispatcher; var Action

    = { searchItems: function(query) { AppDispatcher.dispatch({ type: 'SEARCH_ITEMS' }); ItemsApi.search(query).done(function(data) { AppDispatcher.dispatch({ type: 'SEARCH_ITEMS_SUCCESS', data: data }); }); } }; Actions fetching data from remote
  28. var ItemsAPI = require('./ItemsApi'); var AppDispatcher = require('flux').Dispatcher; var Action

    = { searchItems: function(query) { AppDispatcher.dispatch({ type: 'SEARCH_ITEMS' }); ItemsApi.search(query).done(function(data) { AppDispatcher.dispatch({ type: 'SEARCH_ITEMS_SUCCESS', data: data }); }); } }; Actions fetching data from remote
  29. var ItemsAPI = require('./ItemsApi'); var AppDispatcher = require('flux').Dispatcher; var Action

    = { searchItems: function(query) { AppDispatcher.dispatch({ type: 'SEARCH_ITEMS' }); ItemsApi.search(query).done(function(data) { AppDispatcher.dispatch({ type: 'SEARCH_ITEMS_SUCCESS', data: data }); }); } }; Actions fetching data from remote