2015-07-04 11 views
1

我正在开发带有goatslacker/alt的flux前端程序。我有问题触发商店更新与行动。听众不像我预期的那样运作。无法用alt动作触发alt存储(Flux)

我正在处理的代码现在非常复杂。我会尝试将我的问题简化为以下代码。我期待它最终记录“Hello World!msg:Some message”。显然,听众hello()甚至没有运行。

这是主要的JavaScript(ES6)文件来运行:

import alt from './alt'; 

class DummyActionsProto { 

    sayHello(msg) { 
    console.log("sayHello", msg); 
    return {msg} 
    } 

} 
var DummyActions = alt.createActions(DummyActionsProto); 

class DummyStoreProto { 

    constructor() { 
    this.bindListeners({ 
     hello: DummyActions.sayHello, 
    }); 

    this.state = { 
     items: [], 
    }; 
    } 

    hello(msg) { 
    console.log("Hello World! msg: "+msg); 
    } 

} 
var DummyStore = alt.createStore(DummyStoreProto, 'DummyStore'); 

// trigger the action 
DummyActions.sayHello("Some message"); 

普通alt.js代码,它包括:

import Alt from 'alt'; 
module.exports = new Alt(); 

什么是我的问题吗?

回答

2

总之,如果您在操作方法中添加this.dispatch(),商店只能采取行动。因此,不要使用该方法返回任何内容,您应该运行this.dispatch()(带或不带参数)。监听器将使用参数this.dispatch()运行。

的更正:

import alt from './alt'; 

class DummyActionsProto { 

    sayHello(msg) { 
    console.log("sayHello", msg); 
    this.dispatch(msg); // **instead of return, you should do this** 
    } 

} 
var DummyActions = alt.createActions(DummyActionsProto); 

class DummyStoreProto { 

    constructor() { 
    this.bindListeners({ 
     hello: DummyActions.sayHello, 
    }); 

    this.state = { 
     items: [], 
    }; 
    } 

    hello(msg) { 
    console.log("Hello World! msg: "+msg); 
    } 

} 
var DummyStore = alt.createStore(DummyStoreProto, 'DummyStore'); 

// trigger the action 
DummyActions.sayHello("Some message"); 
相关问题