项目中有个weex页面需要选择时间,在不清楚weex写时间选择器复不复杂的情况下,我觉得还是直接调用native的时间选择控件,选择完时间之后把时间传递到weex然后刷新页面。
但问题也随之而来:我怎样在选择完时间之后把时间传给weex页面并主动刷新呢?由于之前也涉及到native和weex页面传数据的问题,但都不是native端主动的,换句话说:之前都是先响应weex页面中的动作然后去调用native中的方法,native再回调给weex。很明显的是weex-->native-->weex的一个过程。那么可以用callback回调数据给weex页面:
var self = this;
var eventModule = require('@weex-module/event');
eventModule.xxx(function(ret) {
//do some work
});
但现在我需要的交互是native主动发送事件到weex,是native-->weex的过程(weex中需要有个全局事件来监听native中的动作)。思考了良久不知道如何解决,问题在于weex怎么响应我native端的动作的,这个貌似是用callback解决不了的问题啊(其实后来我才知道可以通过在page的created事件时去调用native module,并把js的callback注册给native,native存为变量,然后在需要调用时通过callback触发js的处理函数,这样也可以实现直接从native-->weex)。

不支持全局事件????不会吧!!!
继续找,在某条Issue的底部发现这么一段话:

前几天刚把sdk换成0.8的,ok了,看文档吧:http://alibaba.github.io/weex/doc/modules/globalevent.html
完美搞定:
iOS native端
-(void)refreshWeexPage:(NSNotification *)notif{
NSDictionary * dic = notif.userInfo;
//传一个dic到weex
[weexInstance fireGlobalEvent:@"geolocation" params:dic];
}
weex端
ready: function () {
var self = this;
var globalEvent = require('@weex-module/globalEvent');
globalEvent.addEventListener("geolocation",function(e){
//do work or refresh
});
},
google搜了半天只有只言片语,还是跑github去找issue才发现解决办法(百度根本没有任何信息,吐槽一下百度),所以就目前来说,在weex开发中遇到问题最靠谱的还是去提issue或查找别人的issue看看别人的问题怎么解决的。
ok,结束,记录一下,给遇到同样问题的童鞋。有不懂得可以留言。
网友评论
native端 [weakSelf.instance fireGlobalEvent:@"geolocation" params:nil];
weex端
<script>
var modal = weex.requireModule('modal');
var globalEvent = weex.requireModule('globalEvent');
module.exports = {
created:function(){
globalEvent.addEventListener('geolocation', function (e) {
console.log("get geolocation")
modal.toast({ message: 'get geolocation' });
});
}
}
</script>
NSDictionary * dic = notif.userInfo;
//传一个dic到weex
[weexInstance fireGlobalEvent:@"geolocation" params:dic];
}
这个方法什么时候调用?在哪里调用? weexInstance是哪里来的? 我这里weexInstance是空的,新手求指教
var globalEvent = require ('@weex-module/globalEvent');
globalEvent.addEventListener("geolocation",function(e){
……获取到native传过来的dic……});
NSDictionary * dic = notif.userInfo;
//传一个dic到weex
[weexInstance fireGlobalEvent:@"geolocation" params:dic];
}、
比如我登入成功了, 这个方法, 写在什么地方? 写在我登入成功页面行不行?我要传一些登入信息给weex