【问题标题】:Why the view not redraw with m.request in Mithril?为什么不使用 Mithril 中的 m.request 重绘视图?
【发布时间】:2015-12-21 08:53:56
【问题描述】:

我有这个例子:

controller : function() {
    var responseFolder = m.prop("");
    var pathDirectory = m.prop("C://");` 

    function clickChangeFolder(folder) {
        pathDirectory(pathDirectory() + folder + "/");
        responseFolder(m.request({
            method : "GET",
            url : "my url",
            data : {root:pathDirectory()}
        }));
    }  
    return {
        responseFolder: m.request({
            method : "GET",  
            url : "http://localhost:8080/Mithril_directory/GetFolders",
            data : {root:pathDirectory()}
        }),
    }

view : function(ctrl) {
    return [
        m("ul" , ctrl.responseFolder().map(function(folder) {
            return [
                m("li.liFolder" , {
                    onclick : ctrl.clickChangeFolder.bind(null, folder.name)
                }, 
                folder.name), 
           ];
      })
 ]}

第一次请求没问题,但是当我点击文件夹时,第二次请求没问题,但是视图没有重绘,为什么?

【问题讨论】:

    标签: javascript ajax view mithril.js


    【解决方案1】:

    来自the mithril documentation

    m.request 的基本使用模式返回一个m.prop getter-setter, 当 AJAX 请求完成时填充。

    因此,您的代码发生的情况是,在控制器的返回对象上,ctrl.responseFolder 是一个 m.prop,与之前声明的 responseFolder 变量无关。

    为了让每次点击后视图重绘,需要将初始请求赋值给responseFolder,所以会变成getter-setter,然后返回给视图,视图会重新渲染每个新请求。

    var responseFolder = m.request({...});
    ...
    return {
        responseFolder: responseFolder,
        ...
    };
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-09
    • 2013-01-11
    • 1970-01-01
    • 1970-01-01
    • 2013-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多