我的项目最初是在角,你还可以在GitHub上看到它。但我想换到米思里。
我正在调用data.json文件上的请求(从GitHub页面中提供),这只是按日期排序的事件列表(编译时排序)。整个文件的加载时间有点问题,而且只会变得更糟。
我最初的解决方案是尝试加载一个较小的初始数据子集(通过另一个文件),然后加载其余的数据,但我不知道如何使用Mithril来实现这一点。(此外,我最终需要在数据输出之前对其进行一些计算,如果这与此相关的话)。例如添加属性来标记年份和月份的边界。)
当前的相关代码只是坐在控制器中的this.eventlist = m.request({method: "GET", url: "data.json"});。任何建议,我可以如何在米思里(或任何更好的想法)的建议,将不胜感激。
下面是我到目前为止掌握的代码:
"use strict",
(function(){
var app = {};
app.controller = function() {
this.eventlist = m.request({method: "GET", url: "data.json"});
};
app.view = function(controller) {
return m("ul", [
controller.eventlist().map(function(item){
console.log(item);
return m("li", [
m("a", {href: item.url}, item.name),
m("div", item.description)
]);
})
]);
};
m.module(document.body, app);
})();发布于 2015-12-29 09:02:35
我重构了您的控制器,因此它有几个请求函数;init在启动时被调用,一旦解析,就调用rest。
app.controller = function () {
// changed from this. to var so it's accessible inside the then function
var eventlist = m.prop([]);
// load initial data
var init = function () {
m.request({method: "GET", url: "first-data.json"}).
// assign the result to the getter/setter
then(eventlist).
// send the request for the rest of the data
then(rest)
;
};
// load the rest of the data
var rest = function () {
m.request({method: "GET", url: "rest-of-data.json"}).
// concat the resulting array with the current one
then(function (result) {
eventlist(
eventlist().concat(result)
);
})
;
};
init();
// return the eventlist so the view can access it
return {
eventlist: eventlist
}
};https://stackoverflow.com/questions/34487148
复制相似问题