本帖最后由 千里之外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/ |