ãã®è¨äºã¯Virtual DOMã¢ããã³ãã«ã¬ã³ãã¼ã®5æ¥ç®ã®è¨äºã§ãã
Virtual DOMã¨ããã°Reactã
ã§ãããReactã¯MVCã§è¨ãViewã®é¨åã ããæ
å½ããã®ã§ãã£ã¦ãMVCãã¬ã¼ã ã¯ã¼ã¯çãªãã®ã§ã¯ããã¾ããã
ãªã®ã§ãModelçãä»ã®é¨åã¯èªåã§ã©ãã«ãããå¿
è¦ãããã¾ãã
ããã§ãfacebookã®ä¸ã®äººã¯ãReactãä¸å¿ã«ããã¢ããªã±ã¼ã·ã§ã³æ§ç¯ãã¿ã¼ã³ã¨ãã¦Fluxã£ã¦ã®ãæå±ãã¦ã¾ãã
https://facebook.github.io/flux/
Fluxã£ã¦ã©ããª
Fluxããã£ããè¨ãã¨ã
- ãã¼ã¿ã¯1æ¹åã«æµãã
- ã¤ãã³ããActionã¨ãã¦å®ç¾©
- ãã¸ãã¯ããã¼ã¿ã¯Storeã«å ¨é¨å ¥ãã
ã¨ããã¢ã¼ããã¯ãã£ã
å³ã§è¨ãã¨ãMVCã§ã¯
âã¿ããã«ãªãã¨M,Céã®å¦çã®æµãã ãºãã£ãã®ãã
Fluxã ã¨
ã¨ãã風ã«ãã¼ã¿ã®æµããæ´çã§ãã¦å¬ããã§ãããã¨ãããã¨ã
ï¼å³ã¯fluxxorã®è§£èª¬ããæåï¼
Fluxã®è©³ãã解説ã¯ã§ããªãã®ã§çç¥
âã®ãã¼ã¸èªã¿ãªããsaneyukiããã®è¨äºããã ã大ä½ããããã
Fluxã®ããã©ãç¹
ãã¦ãFluxã«å¾ã£ã¦ã¢ããªæ¸ãã¨ã
- DispatcherãActionCreatorã§åãã³ã¼ãä½åº¦ãæ¸ãäºã«ãªã
- ãä½ã§ãActionã§å¦çãããã£ã¦ããªãã¨ãComponentããä»ã®Component触ã£ãããçã®DOMã触ãæã«é¢åãã£ã¦Componentå ã«ãã¸ãã¯æ¸ãã¡ãã£ãããã¦ç ´ç¶»ããã¡
- Storeã®æ¸ãæ¹ã¨ãã©ãã«ã§ãåºæ¥ã¦ãã¾ããçµ±ä¸æãªããªã£ã¦å°ã
ããããããFluxããã¬ã¼ã ã¯ã¼ã¯ãããªãã®ãæªãï¼
ã¨ããããã§Fluxxorãçã¾ããã
fluxxorã§ã©ã便å©ã«ãªãã
ããã§ã¯ãFluxxorã使ã£ãã³ã¼ããç´ ã®Fluxã§æ¸ããæã¨ã©ãããæãã«éãã®ãè¦ã¦ããã
Fluxxorãå ¬å¼ãã¼ã¸ã«TODOãªã¹ãã®ãã¥ã¼ããªã¢ã«ããã
Fluxxorã®TODOãã¥ã¼ããªã¢ã«
facebook/Fluxã®æ¹ã«ãTODOãªã¹ãã®ãã¥ã¼ããªã¢ã«ãããã©ãFluxxorã®æ¹ãç°¡åãªä½ãã«ãªã£ã¦ãã
以ä¸ã§ã¯ãFluxxorã®æ¹ã®ãã¥ã¼ããªã¢ã«ã«ãã£ã¦è§£èª¬ããã
Store
ç´ ã®Fluxã§ã¯Storeã®ä½ãæ¹ã¨ã決ãããã¦ããªããã©ãä¸å¿ãã¥ã¼ããªã¢ã«ã ã¨âã¿ããã«æ¸ãã¦ãã
var AppDispatcher = require('../dispatcher/AppDispatcher'); var EventEmitter = require('events').EventEmitter; var merge = require('react/lib/merge'); // ãã¼ã¿ç½®ãå ´ var _todos = []; var addTodo = function (text) { _todos.push({text: text, complete: false}); }; var TodoStore = merge(EventEmitter.prototype, { getState: function () { return { todos: _todos }; }, addChangeListener: function(callback) { this.on('change', callback); }, removeChangeListener: function(callback) { this.removeListener('change', callback); } dispatcherIndex: AppDispatcher.register(function(payload) { var action = payload.action; var text; switch(action.actionType) { case TodoConstants.ADD_TODO addTodo(action.text); this.emit('change'); break; // ...çç¥... } return true; }) };
merge(EventEmitter.prototype, {...})
ã¨ãAppDispatcher.register()
ã¨ããã£ã¦ã¦è¦ããããªãã
ãããFluxxorã ã¨ãããªãã
var TodoStore = Fluxxor.createStore({ initialize: function() { this.todos = []; this.bindActions( constants.ADD_TODO, this.onAddTodo, ); }, onAddTodo: function(payload) { this.todos.push({text: payload.text, complete: false}); this.emit("change"); }, getState: function() { return { todos: this.todos }; } });
Storeãã¯ã©ã¹ã«ãªã£ãã®ã§å¤§åå¿ã楽ã«ãªãã
ã¾ããthis.emit('change')
ãã¦ãæã¯ãå¾è¿°ããStoreWatchMixinã«ããComponentå´ã§ç£è¦ã§ããã
ActoinCreator
ç´ ã®Fluxã ã¨ããã§
var AppDispatcher = require('../dispatcher/AppDispatcher'); var TodoActions = { create: function(text) { AppDispatcher.handleViewAction({ actionType: TodoConstants.TODO_CREATE, text: text }); }, };
Fluxxorã ã¨ãããªã
var TodoActions = { addTodo: function(text) { this.dispatch(constants.ADD_TODO, {text: text}); }, };
this.dispatch()
ã¨ããã£ã¦ã¦ãããæ®éã«requireãã¦TodoActions.addTodo()
ã¨ããã¦ã使ããªãã
Fluxxorã§ã¯ãâã®ããã«ãã¦Storeã¨Actionãçµã³ã¤ããæä¸ä½ã®Componentã®propsã«æ¸¡ãå¿
è¦ãããã
var stores = { TodoStore = new TodoStore() }; var actions = TodoActions; var flux = new Fluxxor.Flux(stores, actions); React.render(<Application flux={flux} />, document.getElementById("app"));
Component (View)
Componentã¯Reactã§æ®éã«æ¸ããåºæ¬ç´ ã®Fluxã®æã¨å¤ãããªããã ãã©ã
Store/Actionã触ãããã«FluxMixin
ãmixinããã
var FluxMixin = Fluxxor.FluxMixin(React), StoreWatchMixin = Fluxxor.StoreWatchMixin; var Application = React.createClass({ mixins: [FluxMixin, StoreWatchMixin("TodoStore")], getInitialState: function() { return { newTodoText: "" }; }, getStateFromFlux: function() { var flux = this.getFlux(); return flux.store("TodoStore").getState(); }, // ...çç¥... });
Store/Actionãrequireããªãã¦ãã
- Storeã¯
this.getFlux().store('TodoStore')
- Actionã¯
this.getFlux().actions
ã¨ãã風ã«ã¢ã¯ã»ã¹ã§ããã
âã®ã³ã¼ãã§ã¯StoreWatchMixinã使ã£ã¦ãã
StoreWatchMixinã使ãã¨ãæå®ããStoreã§emit('change')
ãããæãåæã«getStateFromFlux()
ãã¦ãããã
Dispatcherãããªã
Fluxã«ã¯Dispatcher
ã£ã¦ããé¨åãããã
Dispatcherã¯ãActionCreatorã§çºè¡ãããActionãStoreã«éç¥ããå½¹å²ã
ã¢ããªã®å
容ã«ããããããããã¨ãåããªã®ã§ãèªåã§æ¸ããããªãã
Fluxxorã§ã¯Fluxxorããã£ã¦ãããã®ã§æ¸ããªãã¦ããã
Fluxxorã®æ³¨æç¹
ã¨ããããã«ä¾¿å©ãªFluxxorã ãã©ã使ã£ã¦ã¿ã¦ããã¤ã注æãããã¨ãã£ãã
Fluxxorã¨ããããFluxã®åé¡ããã ãã©
æ¢åã³ã¼ããã®ã¾ã¾ä½¿ããªã
StoreãActionãæ¸ãæ¹å¤ããã®ã§ãããã¾ã§ã«æ¸ããã³ã¼ãå ¨é¨ä¿®æ£ãããã¨ã«ãªã
Storeããã£ã¡ãFatã«ãªã
ã¯ã
æä¸å±¤ã®Componentã®æ±ã
ç´ ã®Fluxã ã¨ãComponentæ¯ã«å¿
è¦ãªStoreãActionãrequireãã¦ä½¿ãã
Fluxxorã§ã¯ã使ç¨ããå
¨ã¦ã®Store/ActionãFluxxor.Fluxã«ç»é²ããæä¸å±¤ã®Componentã«
React.render(<Application flux={flux} />, document.getElementById("app"));
ã¨ãã£ã¦æ¸¡ãã
ããããäºã§ãåå«Componentããthis.getFlux()
ãéãã¦Store/Actionã®å¼ã³åºããã¨ãã§ããã
ã¤ã¾ãããããComponentãç´°ããåãã¦ãããã¼ã¸å
¨ä½ã大ãããªãã°ãªãã»ã©ãæä¸å±¤ã®Componentã®ã³ã¼ããã©ãã©ãã§ãããªã£ã¦ãã¾ãã
StoreWatchMixinã§ç£è¦ã§ããã®ã¯changeã¤ãã³ãã®ã¿
ç´ ã®Fluxã ã¨ãStoreå
ã§é©å½ãªã¤ãã³ãçºè¡ãã¦ãã¤ãã³ãæ¯ã«addEventListenerãããã§ããã
Fluxxorã§StoreWatchMixinã§Storeãç£è¦ããã¨ããchangeã¤ãã³ã以å¤ã¯ç£è¦ãã¦ãããªãã
Fluxxorã§æ¸ãã¨ãã¯ãchangeã¤ãã³ã以å¤ä½¿ããªãè¨è¨ã«ããæ¹ãè¯ãããã
ææ³
Reactã¯ã«ãããªããä¸ç観ã ãã©ãç´ ã®Fluxã¯å¤§åèªç±ãªæãã§é ãæ··ä¹±ããã
Fluxxorã¯é¨åã®ä½ãæ¹ãããç¨åº¦æ±ºãã¦ãããã®ã§æ°ã楽ã«ãªãã
Fluxã®ãã¬ã¼ã ã¯ã¼ã¯ã«ã¯ Delorean ã£ã¦ãã競å製åãããã®ã§ãFluxã§æ¸ããããã©Fluxxorã¯å«ãªäººã¯è§¦ã£ã¦ã¿ã¦ãããããã
次ã¯mizchiããã®è¨äºã§ãã