2016-11-30 41 views
0

使用電極配置庫,https://github.com/electrode-io/electrode-confippet - 有什麼辦法可以在反應組件中使用配置設置客戶端?有什麼方法可以使用電極配置變量客戶端?

我目前正在創建一個配置存儲並從服務器索引文件傳遞它,但是有沒有更好的方法?

//index-view.js 
import { config } from 'electrode-confippet'; 
const auth0Config = config.$('settings.auth0'); 

function createReduxStore(req, match) { // eslint-disable-line 
    const initialState = { 
    user: null, 
    checkBox: { checked: false }, 
    number: { value: 999 }, 
    config: { auth: auth0Config }, 
    }; 

    const store = createStore(rootReducer, initialState); 
    return Promise.resolve(store); 
} 

回答

1

是的!我們目前正在這樣做:

// src/server/views/index-view.js 
// … 
module.exports = (req) => { 
    const app = (req.server && req.server.app) || req.app; 

    if (!app.routesEngine) { 
     app.routesEngine = new ReduxRouterEngine({ 
      routes, 
      createReduxStore, 
      stringifyPreloadedState 
     }); 
    } 

    return app.routesEngine.render(req); 
}; 

這會在每次請求Express時提供。

如果您需要在初始化期間設置您的應用需要的配置參數,還可以在express-server.js中創建存儲(並在index-view.js中重新創建它以避免交叉連接污染)。

// express-server.js 
// … 
const loadConfigs = (userConfig) => { 
    if (_.get(userConfig, 'plugins.electrodeStaticPaths.enable')) { 
     // eslint-disable-next-line no-param-reassign 
     userConfig.plugins.electrodeStaticPaths.enable = false; 
    } 

    return Confippet.util.merge(Confippet.config, userConfig); 
}; 

// fetch appContext FIRST because components may need it to load, eg: language 
const createReduxStore = (config) => store.dispatch({ 
     type: 'ADD_INITIAL_STATE', 
     payload: { appContext: config.$('storeConfig') }, 
    }) 
    // eslint-disable-next-line no-console 
    .catch(error => console.error('DISPATCH ERROR:', error)) 
    // return the now-hydrated store 
    .then(() => store); 

module.exports = function electrodeServer(userConfig, callback) { 
    const promise = Promise.resolve(userConfig) 
     .then(loadConfigs) 
     .then(createReduxStore) 
     // … 

    return callback ? promise.nodeify(callback) : promise; 
}; 

然後電極將使其可以作爲類似window.__PRELOADED__

相關問題