Coder Social home page Coder Social logo

fluxxx's Introduction

fluxx

isomorphic flux

簡介

啟發自

試著用最簡短的實作解決 react isomorphic 的基本問題

  • How to render after all stores are populated?
  • Singleton issue
  • Checksum mismatch

解法

  • resolver 由 fluxxx 提供 ready 來檢查
  • singleton - 每個 request 都有獨立的 new Fluxxx() store action component 提供方法取得該 flux 的 instance。
  • checksum - dehydrate / rehydrate

usage

Action

Fluxxx.action({
  all: function() {
    var flux = this.flux;
    // return promise
    return fetch('/api/list.json').then(function(res) {
      return res.json();
    }).then(function(data) {
      flux.getActions().initial(data);
      return data;
    });
  },
  initial: function(data) {
    this.flux.getActions().initial.dispatch(data);
  }
});

Store

Fluxxx.store('list', function() {
  var flux = this.flux;
  var list = [1, 2, 3];
  return {
    getList: function() {
      return list;
    },
    dehydrate: function() {
      return [list];
    },
    rehydrate: function(state) {
      list = state[0];
    },
    dispatchToken: flux.listenTo('list', function(on) {
      on(actions.pop, 'onPop');
      on(actions.push, 'onPush');      
      on(actions.initial, 'onInitial');
    }
  }
});

Component

var List = React.createClass({
	mixins: [Fluxxx.mixin(React)],
	getInitialState: function() {
		this.list = this.flux().getStore('list');
		return {items: this.list.getItems()};
	}
	render: function() {
	  //...
	}
});

Create context

var flux = new Fluxxx();
flux.rehydrate(window.__dehydrated);
React.render(<App flux={flux} />, document.getElementById('container'));

Create context in server side

var flux = new Fluxxx();
var app = React.createElement(App, {flux: flux});
React.renderToString(app);
flux.ready(function() {
  var html = React.renderToString(app);
  res.render('index', {html: html, dehydratedStr: JSON.stringify(flux.dehydrate())});
});

fluxxx's People

Contributors

mlwmlw avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.