成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

Flux 應(yīng)用架構(gòu)

songjz / 3343人閱讀

摘要:應(yīng)用架構(gòu)是用來構(gòu)建客戶端應(yīng)用的一種應(yīng)用架構(gòu)體系。它是一種類似的架構(gòu),但是它更加簡單清晰,是一種單向數(shù)據(jù)流的架構(gòu)設(shè)計。將數(shù)據(jù)和動作類型傳遞給去分發(fā)數(shù)據(jù)流是一個包含所有動作類型的常量對象一個分發(fā)中心,它管理著應(yīng)用的所有數(shù)據(jù)流向。

Flux 應(yīng)用架構(gòu)

Flux是Facebook用來構(gòu)建客戶端Web應(yīng)用的一種應(yīng)用架構(gòu)體系。它是一種類似MVC的架構(gòu),但是它更加簡單、清晰,是一種單向數(shù)據(jù)流的架構(gòu)設(shè)計。

Note

請事先對React和ES6進行了解。

本文采用Facebook官方的Flux。

快速入門
$ git clone https://github.com/ipluser/react-flux-demo.git
$ cd react-flux-demo
$ npm start

瀏覽器將會自動打開一個新的網(wǎng)頁(若沒有,請訪問http://127.0.0.1:8080):

核心概念

Flux應(yīng)用主要分為四個主要的部門:Views, Actions, Dispatcher, Stores.

Name Description
Views 視圖層,React組件
Actions 行為動作層,視圖層觸發(fā)的動作,例如click event
Dispatcher 分發(fā)中心,注冊/接受動作,調(diào)用數(shù)據(jù)流向中的回調(diào)函數(shù)
Stores 數(shù)據(jù)層,管理應(yīng)用狀態(tài),廣播通知Views狀態(tài)發(fā)生改變

單向數(shù)據(jù)流是Flux應(yīng)用的核心。Dispatcher, Stores, Views是獨立的輸入和輸出節(jié)點,而Action是一個包含數(shù)據(jù)和動作類型的簡單對象。

Views

打開項目入口文件main.jsx:

// public/scripts/main.jsx
import React from "react";
import ReactDOM from "react-dom";
import TodoController from "./components/todoController.jsx";

ReactDOM.render(, document.body);

上面代碼中采用了ReactJS Controller View模式,一個"Controller View"是應(yīng)用中最頂層的組件,它管理著所有應(yīng)用狀態(tài),并以屬性方式傳遞給子組件。 接下來我們看看toToController.jsx:

// public/scripts/components/todoController.jsx
import React from "react";

import TodoAction from "../actions/todoAction.js";
import TodoStore from "../stores/todoStore.js";
import Todo from "./todo.jsx";

export default class TodoController extends React.Component {
  constructor(props) {
    super(props);
  }

  newItem() {
    TodoAction.addItem("new item");
  }

  render() {
    return ;
  }
}

正如你所看到的,TodoController僅僅給Todo子組件指定了newItem動作。Todo接收屬性和渲染組件:

// public/scripts/components/todo.jsx
import React from "react";

import "../../styles/components/todo.scss";

export default function Todo(props) {
  let list = props.items.map((item, index) => {
    return 
  • {item}
  • ; }); return (
      {list}
    ); }

    一旦點擊todo按鈕,TodoController將會觸發(fā)一個addItem動作。

    Actions

    TodoAction將數(shù)據(jù)和動作類型傳遞給Dispatcher去分發(fā)數(shù)據(jù)流:

    // public/scripts/actions/todoAction.js
    import AppDispatcher from "../dispatcher.js";
    import TodoConstant from "../constants/todoConstant.js";
    
    class TodoAction {
      addItem(text) {
        AppDispatcher.dispatch({
          actionType: TodoConstant.ADD_ITEM,
          text
        });
      }
    }
    
    export default new TodoAction();

    todoConstants.js是一個包含所有動作類型的常量對象:

    // public/scripts/constants/todoConstant.js
    export default {
      ADD_ITEM: "TODO_ADD_ITEM"
    };
    Dispatcher

    Dispatcher一個分發(fā)中心,它管理著應(yīng)用的所有數(shù)據(jù)流向。每一個Store在這里注冊,并提供一個回調(diào)函數(shù):

    // public/scripts/dispatcher.js
    import { Dispatcher } from "flux";
    
    import TodoStore from "./stores/todoStore";
    import TodoConstant from "./constants/todoConstant";
    
    const AppDispatcher = new Dispatcher();
    
    TodoStore.dispatchToken = AppDispatcher.register(payload => {
      switch (payload.actionType) {
        case TodoConstant.ADD_ITEM:
          TodoStore.addItem(payload.text);
          break;
        default:
      }
    });
    
    export default AppDispatcher;

    上面代碼中可以看到,當TodoAction提供給Dispatcher一個新動作時,TodoStore將會通過注冊時的回調(diào)函數(shù)接受動作的行為。

    Stores

    TodoStore包含狀態(tài)和業(yè)務(wù)邏輯。它的職責有點類似MVC中的model

    // public/scripts/stores/todoStore.js
    import EventEmitter from "events";
    
    class TodoStore extends EventEmitter {
      constructor() {
        super();
        this.items = [];
      }
    
      getAll() {
        return this.items;
      }
    
      addItem(text) {
        this.items.push(text);
        this.change();
      }
    
      change() {
        this.emit("change");
      }
    
      addListener(name, callback) {
        this.on(name, callback);
      }
    
      removeListener(name, callback) {
        this.removeListener(name, callback);
      }
    }
    
    export default new TodoStore();
    Views, again

    再回到TodoController中,我們初始化應(yīng)用的狀態(tài),同時監(jiān)聽Store的狀態(tài)改變事件:

    // public/scripts/components/todoController.jsx
    import React from "react";
    
    import TodoAction from "../actions/todoAction.js";
    import TodoStore from "../stores/todoStore.js";
    import Todo from "./todo.jsx";
    
    export default class TodoController extends React.Component {
      constructor(props) {
        super(props);
        this.state = { items: TodoStore.getAll() };
        this.onListChange = this.onListChange.bind(this);
      }
    
      componentDidMount() {
        TodoStore.addListener("change", this.onListChange);
      }
    
      componentWillUnmount() {
        TodoStore.removeListener("change", this.onListChange);
      }
    
      onListChange() {
        this.setState({
          items: TodoStore.getAll()
        });
      }
    
      newItem() {
        TodoAction.addItem("new item");
      }
    
      render() {
        return ;
      }
    }

    一旦TodoController接受到應(yīng)用狀態(tài)改變,將會觸發(fā)Todo重新渲染。

    參考

    Facebokk Flux

    Andrew - Controller-View

    ruanyifeng - Flux 架構(gòu)入門教程

    源代碼

    react-flux-demo

    文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

    轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/11751.html

    相關(guān)文章

    • Redux概念之一: Redux簡介

      摘要:應(yīng)用這說明并不是單指設(shè)計給用的,它是獨立的一個函數(shù)庫,可通用于各種應(yīng)用。在數(shù)據(jù)流的最后,要觸發(fā)最上層組件的,然后進行整體的重新渲染工作。單純在的對象上是沒有辦法使用,要靠額外的函數(shù)庫才能這樣作,這是一定要使用類似像這種函數(shù)庫的主要原因。 Redux的官網(wǎng)中用一句話來說明Redux是什么: Redux是針對JavaScript應(yīng)用的可預(yù)測狀態(tài)容器 這句話雖然簡短,其實是有幾個涵義的: ...

      cjie 評論0 收藏0
    • 我對 React Flux 架構(gòu)的理解

      摘要:當響應(yīng)時,通過已注冊的回調(diào)函數(shù),將提供的數(shù)據(jù)負載發(fā)送給應(yīng)用中的所有。對外只暴露,不允許提供禁止在任何地方直接操作。是單例作為中的事件分發(fā)中心,同時還要管理所有中的事件。 React Flux架構(gòu)簡介 個人現(xiàn)階段對Flux架構(gòu)的理解,求拍磚求star!原文鏈接:https://github.com/kuitos/kuitos.github.io/issues/27 React 簡介請戳 ...

      hankkin 評論0 收藏0
    • Flux 應(yīng)用架構(gòu)

      摘要:應(yīng)用架構(gòu)是用來構(gòu)建客戶端應(yīng)用的一種應(yīng)用架構(gòu)體系。它是一種類似的架構(gòu),但是它更加簡單清晰,是一種單向數(shù)據(jù)流的架構(gòu)設(shè)計。將數(shù)據(jù)和動作類型傳遞給去分發(fā)數(shù)據(jù)流是一個包含所有動作類型的常量對象一個分發(fā)中心,它管理著應(yīng)用的所有數(shù)據(jù)流向。 Flux 應(yīng)用架構(gòu) Flux是Facebook用來構(gòu)建客戶端Web應(yīng)用的一種應(yīng)用架構(gòu)體系。它是一種類似MVC的架構(gòu),但是它更加簡單、清晰,是一種單向數(shù)據(jù)流的架構(gòu)設(shè)...

      Jonathan Shieber 評論0 收藏0
    • Redux專題:考古

      摘要:光憑一個是無法實現(xiàn)血緣關(guān)系疏遠的組件之間的狀態(tài)同步的。就是為解決這個問題而生的。,處理動作的派發(fā),相當于架構(gòu)的。我們的主角是,它也是目前社區(qū)最受歡迎的狀態(tài)管理框架。專題一覽考古實用中間件時間旅行 本文是『horseshoe·Redux專題』系列文章之一,后續(xù)會有更多專題推出來我的 GitHub repo 閱讀完整的專題文章來我的 個人博客 獲得無與倫比的閱讀體驗 React的橫空出世給...

      toddmark 評論0 收藏0

    發(fā)表評論

    0條評論

    songjz

    |高級講師

    TA的文章

    閱讀更多
    最新活動
    閱讀需要支付1元查看
    <