首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Mithril - Rerender动态内容

Mithril - Rerender动态内容
EN

Stack Overflow用户
提问于 2015-11-02 06:26:20
回答 2查看 612关注 0票数 0

在延迟对象返回数据后,我正在尝试重新呈现DOM elemnet。我在控制台上调试它,似乎我的元素正在被创建,但是它从来没有出现在页面上。如果我添加静态内容,它会按预期工作。

代码语言:javascript
复制
        m('div', {class: 'table-responsive'}, [
            m('table', {class: 'table'}, [
                m("thead", [
                    m('tr', [
                        m('th', '#'),
                        m('th', 'Groups'),
                    ])
                ]),
                m('tbody', findGroupsDeferred.promise.then(function(data){ // findGroupsDeferred returns when the promise is complete with my data.
                    data.group.map(function(group) {
                        return m("tr", [
                            m("td", [
                                m("input[type=checkbox] checked", {
                                    value: group,
                                    onclick: function (event) {
                                        if (event.target.checked) {
                                            ctrl.addGroup(ctrl.groups(event.target.value))
                                        } else {
                                            ctrl.removeGroup(ctrl.groups(event.target.value))
                                        }
                                    }
                                })
                            ]),
                            m("td", group),
                        ])
                    });
                }))
            ])
        ]),
EN

回答 2

Stack Overflow用户

发布于 2015-11-02 14:27:00

Roamer-1888是非常正确的。这不能在视图中完成。要实现这一点,您有以下几种选择:

首先,在控制器中等待结果:

代码语言:javascript
复制
controller: function() {
  scope = {
    groups: []
  }
  findGroupsDeferred.promise.then(function(data) {
    scope.groups = data.group;
  }
  return scope;
},
view: function(scope) {
  return scope.groups.map(function(group) {
    return group.name // or what ever you want to do here
  }
}

另一种选择是为它创建一个component,这几乎会在相同的代码中产生,接受它的封装。第三种选择是将m.prop与mithrils一起使用。

票数 2
EN

Stack Overflow用户

发布于 2015-11-02 08:20:39

我不知道mithril,但我猜promise不能以这种方式使用。

从承诺的基本原则来看,用promise.then(...)包装整个m()表达式会更有意义。换句话说,在findGroupsDeferred.promise解析之后构建整个表,而不是尝试对准表的内部部分。

代码语言:javascript
复制
findGroupsDeferred.promise.then(function(data) { // findGroupsDeferred returns when the promise is complete with my data.
    m('div', {class: 'table-responsive'}, [
        m('table', {class: 'table'}, [
            m("thead", [
                m('tr', [
                    m('th', '#'),
                    m('th', 'Groups'),
                ])
            ]),
            m('tbody', data.group.map(function(group) {
                return m("tr", [
                    m("td", [
                        m("input[type=checkbox] checked", {
                            value: group,
                            onclick: function (event) {
                                if (event.target.checked) {
                                    ctrl.addGroup(ctrl.groups(event.target.value));
                                } else {
                                    ctrl.removeGroup(ctrl.groups(event.target.value));
                                }
                            }
                        })
                    ]),
                    m("td", group),
                ]);
            }))
        ])
    ]),
});

或者,mithril有一个用于rendering before web service requests finish的机制,在这里可能是相关的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33467939

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档