起步软件技术论坛
搜索
 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 2741|回复: 0

[分享] 派发事件的用法

[复制链接]

90

主题

703

帖子

1026

积分

金牌会员

Rank: 6Rank: 6

积分
1026
QQ
发表于 2016-10-14 15:44:22 | 显示全部楼层 |阅读模式
本帖最后由 千里之外2 于 2016-10-14 15:49 编辑

原因:在shell跳转的页面中,想在一个页面中调用其他页面中的js方法,比如添加到购物车的操作,这个时候,可以使用派发事件的方式解决!实质上就是在当前页面中调用其他页面的js代码!
解决方法:
参考外卖案例的写法!在/UI2/demo/taobao/main.js 中,onload事件中进行添加事件
// 添加事件
    Model.prototype.modelLoad = function(event) {
        justep.Shell.on("onRestoreContent", this.onRestoreContent, this);
        justep.Shell.on("onShoppingContent", this.onShoppingContent, this);
        justep.Shell.on("onHomeContent", this.onHomeContent, this);
    };
onUnLoad事件中 卸载事件
// 卸载事件
Model.prototype.modelUnLoad = function(event) {
justep.Shell.off("onRestoreContent", this.onRestoreContent);
justep.Shell.off("onShoppingContent", this.onShoppingContent);
justep.Shell.off("onHomeContent", this.onHomeContent);
};</pre>
<pre>
以及js中定义这个方法,如:
Model.prototype.onRestoreContent=。。。
Model.prototype.onShoppingContent=。。。
Model.prototype.onHomeContent =。。。
在其他shell打开的页面中通过如下调用,就可以实现另一个页面相应方法的触发执行!
justep.Shell.fireEvent("onRestoreContent", {id:'id的值'});</pre>
<pre>


在onRestoreContent 的方法中通过event.id 就可以获得这个值了!
注意。另一个页面必须是打开过的!因为必须要执行onload事件才能注册上这个方法!而且不能执行到onUnLoad,否则事件卸载掉后,就无法执行到了!
这就是事件的派发基本实现原理!

http://docs.wex5.com/wex5-ui-question-list-2034/
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|X3技术论坛|Justep Inc.    

GMT+8, 2025-6-19 12:42 , Processed in 0.110257 second(s), 23 queries .

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表