本文贴出项目中热修复的代码片段:
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() == && vo.status() == ) {
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() == ) { self.recharge();
}
}
if(vo.linkUrl()&&vo.linkUrl().length()>){ 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",);
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/