2015-11-03 32 views
-1

我建立一個應用程序反應並將流量/ McFly的,並希望有獨立店,但我的McFly的動作傳遞給每一個商店我McFly的創建 - 儘管我使用單獨的文件導入該McFly的實例FLUX與多個獨立店/調度

/stores/msg/mcfly.js

var McFly   = require('mcfly'); 
, MsgDispatcher = new McFly() 
; 
module.exports = MsgDispatcher; 

/stores/user/mcfly.js

var McFly  = require('mcfly') 
, UserMcFly = new McFly() 
; 
module.exports = UserMcFly; 

所以這應該是不同的情況下,對吧? 但他們的調度員似乎是一樣的。
?因爲「流量」調度員始終是單身?

當我創建不同的商店/ ActionCreator-雙不同McFly的「實例」 每一個動作仍然可以通過每家商店去。
我知道很多人建議將只有一個全局狀態/存儲,但恕我直言這種做法並不適合於每一個項目,我討厭這種行爲。

TL; DR:
是否有可能建立完全獨立的存儲/調度員
或者是打算這樣?爲什麼?
缺點:糟糕的表現,真正的大StateObject,檢查更新,如果它時並不需要,獨立SubApps不可能DataModels的?spezification,...

如何創建獨立可重複使用的獨立的子應用程序如果不能有獨立的存儲/調度程序?

最好的問候, 史蒂夫

+0

誤解商店減速機 - 概念。問題目了沒有意義......應該刪除 – DoubleU23

回答

1

爲什麼一個問題的行動在所有商店購買?您可以在每個商店中使用開關來捕捉您對該商店感興趣的操作。有時你真的想在多個商店裏聽同樣的動作。

+0

的問題是,該「shoulUpdate」的方法(在混合料攪拌)解僱了,因爲店裏的發光即使商店國家並沒有改變改變事件。這wouln'd是一個問題,如果我的「shouldComponentUpdate」方法的nextState不等於實際狀態......(這是我的第一個反應/流量工程) 我逮住那的ChangeEvent通過測試,如果狀態改變。但那只是一個(壞的?)解決方法 – DoubleU23

+1

也許它是mixin然後。商店不應該僅僅因爲一個動作被觸發而觸發更新。嘗試偵聽在componentDidMount中設置的商店的備用方法。 –

0

大THX @Janaka史蒂文斯
我添加的組件的的onChange回調到店,如果需要手動啓動它:

Thread.react.js

import React  from 'react'; 
import MsgStore  from '../stores/msg/MsgStore'; 
export default React.createClass({ 
     getInitialState: function() { 
      // returns the _msg object located in the MsgStore closure 
      return MsgStore.getState() 
     } 
    , onChange: function() { 
      // i don't think that's the right way but it works 
      this.setState(MsgStore.getState()); 
     } 
    // better than the mcFly mixin for multiple stores 
    // so you have more control to which store your component listens 
    , componentDidMount: function() { 
      MsgStore.on('change', this.onChange); 
     } 
     [ ... ] 
    ) 
; 

MsgStore

import React   from 'react'; 
import {MsgMcFly}  from './mcfly'; 
import {MsgReducers} from './MsgReducers'; 
import combineReducers from 'mcfly-combinereducers'; 
let combinedReducers = combineReducers(MsgReducers) 
// get _msgs per API | from DB 
, _msgs    = [ 
     {id: 0, txt: 'test0', user: 'steve23'} 
    , {id: 1, txt: 'test1', user: 'steve23'} 
    , {id: 2, txt: 'test2', user: 'steve23'} 
    ] 
; 
export default MsgMcFly.createStore({ 
     getMsgs: function(){ return _msgs; } 
    , getState: function(){ return {'msgs': _msgs} } 
    , createMsgObject: function(msg) { 
      return { 
       id:   _msgs.length // dev 
      , txt:  msg.txt 
      , timestamp: msg.timestamp || new Date() 
      , user:  msg.user || 'steve' // dev 
      } 
     } 
    }, function(action) { 

      // prevent the Store to fire for 'USER_' actions 
      if(action.type.substr(0, 3) !== 'MSG') 
       return false; 

      combinedReducers(MsgStore.getMsgs(), action); 

      MsgStore.emitChange(); 
      return true; 
     } 
    ) 
;