七夕阳光
七夕阳光
  • 发布:2017-02-19 20:23
  • 更新:2017-05-11 15:47
  • 阅读:1932

【报Bug】mui fire 传递数组时出错

分类:MUI
$.fire = function(webview, eventType, data) {  
        if (webview) {  
            if (data !== '') {  
                data = data || {};  
                if ($.isPlainObject(data)) {  
                    data = JSON.stringify(data || {}).replace(/\'/g, "\\u0027").replace(/\\/g, "\\u005c");  
                }  
            }  
            webview.evalJS("typeof mui!=='undefined'&&mui.receive('" + eventType + "','" + data + "')");  
        }  
    };

上述是mui fire方法的源码,当data为对象、字符串时可以正常传递。但当data为array时,isPlainObject方法会返回false,导致未能经过JSON.stringify编码而直接通过字符串拼接传递给webview。

举例:
var data=[{id:1,name:"haha"},{id:2,name:"haha2"}];
mui.fire(webview,"myevent",data);
最终传递给webview的代码就是:
typeof mui!=='undefined'&&mui.receive('myevent','[object Object],[object Object]')
从而导致无法还原data的原数据。

所以mui 的fire方法应该修改为下面这样:

$.fire = function(webview, eventType, data) {  
        if (webview) {  
            if (data !== '') {  
                data = data || {};  
                if ($.isPlainObject(data) || $.isArray(data)) {  
                    data = JSON.stringify(data || {}).replace(/\'/g, "\\u0027").replace(/\\/g, "\\u005c");  
                }  
            }  
            webview.evalJS("typeof mui!=='undefined'&&mui.receive('" + eventType + "','" + data + "')");  
        }  
    };
2017-02-19 20:23 负责人:无 分享
已邀请:
z***@ulimei.com

z***@ulimei.com

原来已经有人发现这个问题了,顶一个!
我现在的临时解决方案是,把要传递的数据放到localStorage里,然后事件触发的时候去取。

七夕阳光

七夕阳光 (作者)

Mui v3.6.0还没解决这个问题呢

DCloud_UNI_CHB

DCloud_UNI_CHB

github上最新源码已合并pull Request,参考:#303

该问题目前已经被锁定, 无法添加新回复