我在一个项目中使用Mithril JS,但我不太清楚如何连接Ajax生命周期。比如说,如果我的Ajax请求需要一段时间,我想显示一个加载图标。这很基础,但我似乎不知道该怎么做。
我希望将加载图标放在与Ajax请求内容相同的容器中。
以下是我的设置:
var Thing = function (data) {
var p = m.prop;
this.title = p(data.title);
this.timestamp = p(moment.unix(data.timestamp));
}
Thing.list = function(options) {
m.request({method: "GET", url: "/things.json", type: Thing, background: true});
};
MyApp.components.thingsList = {
controller: function ThingListController() {
this.things = m.prop([]);
Thing.list().then(this.things).then(m.redraw);
},
view: function thingListView(ctrl) {
return m('div#thing-tab', [
m('ul#things', [
ctrl.things().map(thingView)
])
]);
}
};
function thingView(thing) {
...some view stuff...
}
我已经按照自己想要的方式使它工作了,但是我无法弄清楚如何钩入ajax生命周期。再次强调,我只想在请求开始时显示一个旋转图标,然后用ajax请求的结果替换它。
非常感谢任何帮助!
谢谢,
complete
?m.request(args).then(complete, complete)...
- Eric Majerus