0

我刚开始使用martyjs,我面临的问题是我无法从 marty 组件容器中渲染 React 组件。Store 似乎可以工作,但martyjsStore 更改后组件容器不会更改。我需要一些关于应该写什么console.log来使这段代码工作的指导。

这是我当前的代码:

var CardStore = Marty.createStore({
id: "CardStore",
handlers: {
    change: AppConstants.CARD_CHANGE,
    ratingReceive: AppConstants.RATING_RECEIVE
},
getInitialState: function () {
    return { pid: '', card:{pid:''}};
},

get: function(){
    console.log('GET', this.state);
    return this.state;
},

change: function (pid) {
    console.log("STORE FETCH- WORKED", pid);
    this.state.pid = pid;
    return this.fetch({
        id: pid,
        remotely: function () {
            return RatingHttpAPI.get(pid);
        }
    });
},

ratingReceive: function (rating) {
    .....
    this.hasChanged();
    console.log("STORE CHANGES - WORKED", rating.pid);
}

});

var RatingHttpAPI = Marty.createStateSource({
type: "http",
id: "RatingHttpAPI",

get: function (pid) {
    console.log("API GET", pid);
    return this.post({url: res.url.rating.list, body: Model.addAntiForgery({pid: pid})})
        .then(function (response) {
            console.log("API RECEIVE -WORKED", pid);
            CardActionCreators.ratingReceive(response.body);
        });
},


});


var CardActionCreators = Marty.createActionCreators({
id: "CardActionCreators",
change: function (pid) {
    this.dispatch(AppConstants.CARD_CHANGE, pid);
},
ratingReceive: function(rating){
    this.dispatch(AppConstants.RATING_RECEIVE, rating);
}
});


var Row2RightCol = React.createClass({
render: function () {
    var props = this.props;
    console.log("RENDER-FIRED ONCE ON LOAD",props);
    return (
        <div>
            <p>{props.pid}</p>
        </div>
    );
}
});
Marty.createContainer(Row2RightCol, {
listenTo: CardStore,
fetch() {
    console.log("CONTAINER FETCH-NOT FIRED");
    return {
        card: CardStore.for(this).get()
    }
}
});

$(document).ready(function () {
    React.render(<Row2RightCol/>, $("#card .row:eq(1) .col-right")[0]);
});

在此需要对此问题进行一些澄清。

4

2 回答 2

0

检查 Flux 比较 repo https://github.com/voronianski/flux-comparison它有 Marty.js 示例。

于 2015-04-17T18:59:22.813 回答
0

没有使用 Marty,但是Marty.createContainer()返回了一个新的 React 组件,它包装了你的Row2RightCol组件并添加了绑定。您应该保存返回值Marty.createContainer()并使用它而不是Row2RightCol直接使用。

于 2015-04-16T11:59:23.420 回答