JSPatch 动态更新,案例分析

本文贴出项目中热修复的代码片段:

require('UIView, JPObject, HtmlAllViewController,DataManager,EMClient,EaseMessageViewController')
defineClass("GHAcceptOrderViewController", {
    tableView_willSelectRowAtIndexPath: function(tableView, indexPath) {
        var row = indexPath.row();
        var dataArr = self.sourceArr().toJS();
        var vo = dataArr[row];
        if (vo.type() == 1 && vo.status() == 1) {
            return null;
        }
        return indexPath;
    },
})
defineClass("HomePageViewController", {
    clickIndex: function(index) {
        var data = self.valueForKey("homeVO");
        var dataArr = data.banners().toJS();

        if (dataArr&&dataArr.length>index) {
            var vo = dataArr[index];
            if (vo.type()) {
                if (vo.type() == 2) {
                    
                    self.recharge();
                }
            }
            if(vo.linkUrl()&&vo.linkUrl().length()>6){

                var ctrl = HtmlAllViewController.alloc().init();
                var token = DataManager.getInstance().user().token();
                var header = vo.linkUrl().toJS() + "?token=" + token.toJS();

                ctrl.setValue_forKey(header,"mUrl");
                ctrl.setValue_forKey(vo.name(),"mTitle");
                  self.navigationController().pushViewController_animated(ctrl,true);
            }
        }
    },
    onFloatImage: function(tap) {
        var vo = DataManager.getInstance().user();
        self.view().setUserInteractionEnabled(false);

        var weakSelf = __weak(self);
        var success = block(function(){

            weakSelf.view().setUserInteractionEnabled(true);

              var ctrl = EaseMessageViewController.alloc().initWithConversationChatter_conversationType("pinjian001",0);
              var dict = require("NSMutableDictionary").alloc().init();
            dict.setObject_forKey("我", "senderName");
            if(vo.avatar()){
                dict.setObject_forKey(vo.avatar(), "senderAvatar");
            }else{
                dict.setObject_forKey("order_expert_desc_icon_bgImage.png", "senderAvatar");
            }
            dict.setObject_forKey("品简客服", "receiverName");
            dict.setObject_forKey("doctor_defalt_icon.png", "receiverAvatar");

            ctrl.setValue_forKey("咨询医生","title");
            ctrl.setValue_forKey(dict,"pinjian");

            weakSelf.navigationController().pushViewController_animated(ctrl,true);
        });

        var failure = block("EMError *", function(aError){
          weakSelf.view().setUserInteractionEnabled(true);
        });
        
        EMClient.sharedClient().asyncLoginWithUsername_password_success_failure(vo.hxUsername(),vo.hxPassword(),success,failure);

    },
    readMessage:function(notification){
        self.tabBarItem().setBadgeValue(null);
        self.showMessage(false);
    },
})

需要特别说明的是:

1.oc里的属性访问在 js 这一定要通过.() 访问;

2.oc里的数据在 js 端调用,一定要通过 .toJS() 转换成 js数据类型;

3.oc里的block 在js 这端,如上方代码所示,特别注意下传参的格式;

4.oc里的block 在js端调用,必须要通过弱引用修饰,然后在block里用weakSelf,上方代码所示:

var weakSelf = __weak(self);

5.用到的头文件一定要require();

建议:先本地setupDevelopment 调试,调试成功后,多次检查无误后,再发布全局。

另外:关于jspatch 断点调试:https://github.com/bang590/JSPatch/wiki/JS-%E6%96%AD%E7%82%B9%E8%B0%83%E8%AF%95

更多了解:

http://www.jianshu.com/p/0cb81bf23d7a

http://blog.cnbang.net/works/2767/

http://www.cnblogs.com/dsxniubility/p/5080875.html

http://jspatch.com/

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容