Skip to content

molecule 如何处理“全局”数据(状态)? #1

@kalxd

Description

@kalxd

淘宝“宝贝详情页”上,有个跟卖家聊天的按钮,点击之后可以跟卖家直接聊天。
现在考虑这样一个场景,如果点击聊天之后,卖家下面的“联系我”变成“正在联系”,然后弹出聊天窗口,关闭聊天窗口后恢复成“联系我”。

从redux方面来看,它像个巨大的观察者,不管是界面变化还是用户变化,它都会进行观察,如果需要对应的数据,只要订阅到需要的部分就行了,这个过程发生在组件中。

例如点击按钮:

// “详情页”
this.watch("change", prop => {
    if ("chatting" === prop.chat) {
        this.$chat.text("正在联系");
    }
    else {
       this.$chat.text("联系我");
    }
});

this.$chatBtn.click(_ => dispatch({ type: "chat", value: "chatting" }));
// 聊天按钮
this.watch("change", prop => {
    if ("chatting" === prop.chat) {
        this.$window.show();
    }
    else {
        this.$window.hide();
    }
});

this.$window.close(_ => dispatch({ type: "chat", value: "hide"}));

react-redux实际应用跟上面相差很多,不过差不多就是这个意思,把watch换成render就很接近了。虽然不怎么喜欢redux,但感觉也是有可取之处,那么molecule会怎样处理这种情况?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions