0

在我的延迟对象返回数据后,我试图重新渲染我的 DOM 元素。我在控制台上调试它,似乎我的元素正在创建,但它从未显示到页面上。如果我添加静态内容,它会按预期工作。

        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),
                        ])
                    });
                }))
            ])
        ]),
4

2 回答 2

2

Roamer-1888 是完全正确的。这不能在视图中完成。你有几个选择来实现这一点:

首先是等待控制器中的结果:

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
  }
}

另一种选择是为此创建一个组件,这几乎会导致相同的代码接受它的封装。第三种选择是m.prop与为此延期的秘银一起使用。

于 2015-11-02T06:27:00.320 回答
1

我不知道秘银,但猜想不能以那种方式使用承诺。

m()从 Promise 的第一原则来看,用 . 包裹整个表达式会更有意义promise.then(...)。换句话说,在解析后构建整个findGroupsDeferred.promise,而不是尝试针对表的内部部分。

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),
                ]);
            }))
        ])
    ]),
});

或者,秘银有一种在 Web 服务请求完成之前呈现的机制,这可能与此处相关。

于 2015-11-02T00:20:38.630 回答