ReactとFluxでクライアントサイドの設計 このエントリをはてなブックマークに登録

2015年11月04日

ダニーダニー / ,


React使
ReactFlux

MVC


MVCModelView

React使


ViewViewViewView

ViewViewView

Flux


Flux
Flux使
Observer

Flux使


FluxFlux

調使FacebookFlux使

React


https://github.com/facebook/react

View使

Flux


https://github.com/facebook/flux

Dispatcher使使

EventEmitter3


https://github.com/primus/eventemitter3

StoreView使

Node.js events.EventEmitter使

EventEmitter2使EventEmitter3


http://f96q.github.io/flux

View


src/components/item_form.js
'use strict';

import React from 'react'
import ItemActions from '../actions/item_actions'

export default class ItemForm extends React.Component {
  create(event) {
    let text = this.refs.itemText.getDOMNode().value;
    if (text) {
      ItemActions.create(text);
      this.refs.itemText.getDOMNode().value = '';
    }
  }
  render() {
    return (
      <div className="input-group">
        <input type="text" className="form-control" ref="itemText"></input>
        <span className="input-group-btn">
          <button className="btn btn-primary" type="button" onClick={this.create.bind(this)}>+</button>
        </span>
      </div>
    );
  }
}

+onClickItemActionscreate

Action


src/actions/item_actions.js
'use strict';

import uuid from 'uuid'
import Dispatcher from '../dispatcher/dispatcher'

class ItemActions {
  constructor(dispatcher) {
    this.dispatcher = dispatcher;
  }
  dispatch(params) {
    this.dispatcher.dispatch(params);
  }
  create(body) {
    this.dispatch({
      type: 'create',
      item: {id: uuid.v4(), body: body}
    });
  }
}

export default new ItemActions(Dispatcher);

ItemActionscreatethis.dispatcher
APIAjaxthis.dispatcher使

Dispatcher


src/dispatcher/dispatcher.js
'use strict';

import {Dispatcher} from 'flux'

export default new Dispatcher();

DispatcherFlux使

Store


src/stores/item_store.js

'use strict';

import EventEmitter from 'eventemitter3'
import Dispatcher from '../dispatcher/dispatcher'

class ItemStore {
  constructor(dispatcher) {
    this.state = this.getInitialState();
    this.eventEmitter = new EventEmitter();
    this.dispatcher = dispatcher;
    this.changeEvent = 'change';
    this.dispatcher.register((action) => {
      this.invokeOnDispatch(action);
    });
  }
  getState() {
    return this.state;
  }
  getInitialState() {
    return {items: []};
  }
  addListener(listener) {
    this.eventEmitter.addListener(this.changeEvent, listener);
  }
  removeListener(listener) {
    this.eventEmitter.removeListener(this.changeEvent, listener);
  }
  emitChange() {
    this.eventEmitter.emit(this.changeEvent);
  }
  invokeOnDispatch(action) {
    switch (action.type) {
      case 'create':
        this.state.items.push(action.item);
        this.emitChange();
      break;
    }
  }
}

export default new ItemStore(Dispatcher);

ItemActions.createdispatch
invokeOnDispatchcreatethis.state.itemsitemstatethis.emitChange()

View


src/components/app.js
'use strict';

import React from 'react'
import ItemStore from '../stores/item_store'
import ItemForm from './item_form'
import Items from './items'

export default class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = ItemStore.getState();
  }
  componentDidMount() {
    ItemStore.addListener(this.onChange.bind(this));
  }
  componentWillUnmount() {
    ItemStore.removeListener(this.onChange.bind(this));
  }
  onChange() {
    this.setState(ItemStore.getState());
  }
  render() {
    return (
      <div className="container">
        <ItemForm />
        <Items items={this.state.items} />
      </div>
    );
  }
}

ItemStoreemitChangeItemStore.addListeneronChange

onChangethis.setStateStoreReactView




https://github.com/f96q/react-flux-sample


WebWebKPTBoardReactFlux

https://github.com/f96q/kptboard


DocBase便使


https://docbase.io

053c751a-9a40-43e2-ad6e-b75e78367451
  1. メモからはじめる情報共有 DocBase 無料トライアルを開始
  2. DocBase 資料をダウンロード

「いいね!」で応援よろしくお願いします!

このエントリーに対するコメント

コメントはまだありません。

日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)


トラックバック

we use!!Ruby on RailsAmazon Web Services

このページの先頭へ