首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有多个GET请求的Mithril.js

具有多个GET请求的Mithril.js
EN

Stack Overflow用户
提问于 2015-12-28 02:54:21
回答 1查看 673关注 0票数 0

我的项目最初是在角,你还可以在GitHub上看到它。但我想换到米思里。

我正在调用data.json文件上的请求(从GitHub页面中提供),这只是按日期排序的事件列表(编译时排序)。整个文件的加载时间有点问题,而且只会变得更糟。

我最初的解决方案是尝试加载一个较小的初始数据子集(通过另一个文件),然后加载其余的数据,但我不知道如何使用Mithril来实现这一点。(此外,我最终需要在数据输出之前对其进行一些计算,如果这与此相关的话)。例如添加属性来标记年份和月份的边界。)

当前的相关代码只是坐在控制器中的this.eventlist = m.request({method: "GET", url: "data.json"});。任何建议,我可以如何在米思里(或任何更好的想法)的建议,将不胜感激。

下面是我到目前为止掌握的代码:

代码语言:javascript
复制
"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);

})();
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-29 09:02:35

我重构了您的控制器,因此它有几个请求函数;init在启动时被调用,一旦解析,就调用rest

代码语言:javascript
复制
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
    }
};
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34487148

复制
相关文章

相似问题

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