在延迟对象返回数据后,我正在尝试重新呈现DOM elemnet。我在控制台上调试它,似乎我的元素正在被创建,但是它从来没有出现在页面上。如果我添加静态内容,它会按预期工作。
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),
])
});
}))
])
]),发布于 2015-11-02 14:27:00
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
}
}另一种选择是为它创建一个component,这几乎会在相同的代码中产生,接受它的封装。第三种选择是将m.prop与mithrils一起使用。
发布于 2015-11-02 08:20:39
我不知道mithril,但我猜promise不能以这种方式使用。
从承诺的基本原则来看,用promise.then(...)包装整个m()表达式会更有意义。换句话说,在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),
]);
}))
])
]),
});或者,mithril有一个用于rendering before web service requests finish的机制,在这里可能是相关的。
https://stackoverflow.com/questions/33467939
复制相似问题