美文网首页weexWeex开发前端小栈
Weex开发中native如何主动发送事件到weex

Weex开发中native如何主动发送事件到weex

作者: voidxin | 来源:发表于2016-11-18 11:22 被阅读945次

项目中有个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)。

QQ20161118-0.png

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

QQ20161118-1.png
前几天刚把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,结束,记录一下,给遇到同样问题的童鞋。有不懂得可以留言。

相关文章

网友评论

  • Aeline丶:能不能帮我看下toast为什么不弹出啊,谢谢您了
    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>
  • leimeimei:-(void)refreshWeexPage:(NSNotification *)notif{
    NSDictionary * dic = notif.userInfo;
    //传一个dic到weex
    [weexInstance fireGlobalEvent:@"geolocation" params:dic];
    }

    这个方法什么时候调用?在哪里调用? weexInstance是哪里来的? 我这里weexInstance是空的,新手求指教
    voidxin:@leimeimei refreshWeexPage是在你原生页面中触发的,比如button的点击事件时,传个dic到weex页面,weex页面的created方法中实现监听:
    var globalEvent = require ('@weex-module/globalEvent');
    globalEvent.addEventListener("geolocation",function(e){
    ……获取到native传过来的dic……});
    leimeimei:@voidxin 我这个不是自定义的Module, 是component
    voidxin:@leimeimei WXModuleProtocol中的WXSDKInstance ,具体请看官方demo
  • fordG:-(void)refreshWeexPage:(NSNotification *)notif{
    NSDictionary * dic = notif.userInfo;
    //传一个dic到weex
    [weexInstance fireGlobalEvent:@"geolocation" params:dic];
    }、

    比如我登入成功了, 这个方法, 写在什么地方? 写在我登入成功页面行不行?我要传一些登入信息给weex

本文标题:Weex开发中native如何主动发送事件到weex

本文链接:https://www.haomeiwen.com/subject/jlijpttx.html