A 页面预加载 B 页面,打开 B 页面时调用 B 页面的初始化方法,之后打开 B 页面 , B页面完成逻辑处理,调用 mui.back() (调用mui.back之前调用breforback), 将参数传给A页面的监听方法,此时正常,当再次点击按钮跳转B页面后,B页面再次返回A页面时,发现 breforback 方法调用2次,
再次跳转后,发现 breforback 方法调用3次。。。。。
A页面方法:
mui.init({
preloadPages:[
{
url:'../common/choosePerson.html',
id:'choosePerson.html'
}
]
});
mui.plusReady(function(){
document.getElementById("chooseBtn").addEventListener('tap',function(){
//初始化选人页面
var choosePersonPage = null;
if(!choosePersonPage){
choosePersonPage = plus.webview.getWebviewById("choosePerson.html");
}
mui.fire(choosePersonPage,'init',{
fromPage : "addMsg.html", //当前页面路径
chooseType : "multi", //single 单选、multi多选
callback : "fillPerson" //指定回调函数
});
mui.openWindow({
url:'../common/choosePerson.html',
id:'choosePerson.html'
});
});
document.getElementById("sendBtn").addEventListener('tap',function(){
var content = document.getElementById("content").value;
if(trimStr(content) == "") {
mui.toast("请输入留言");
return;
}
});
});
//填充被选的人(选人之后回调函数)
window.addEventListener('fillPerson', function(event){
console.log(event.detail.personName);
console.log(event.detail.personId);
});
B页面方法:
var fromPage,callback,personName,personId;
var chooseType = "single";
mui.init({
beforeback:function(){
console.log("}}}" + personName);
console.log("}}}" + personId);
var backPage = plus.webview.getWebviewById(fromPage);
if(chooseType == "single") {
mui.fire(backPage,callback,{
personName : personName,
personId : personId
});
}else {
var name = personName.join(";");
var id = personId.join(",");
mui.fire(backPage,callback,{
personName : name,
personId : id
});
}
return true;
}
});
function init() {
personName = "";
personId = "";
var showPeople = document.getElementById("show");
showPeople.style.display = "none";
var showDiv = document.getElementById("showDiv");
showDiv.innerHTML = "";
var searchUrl = serverAddress + "platformOrganizationService/getPersonsTree";
var toData = {
"currId": ""
};
mui.ajaxSettings.contentType = "application/json";
mui.ajax(searchUrl, {
data: JSON.stringify(toData),
dataType: "json",
type: 'post',
success: function(data) {
putData(data);
},
error: function(xhr, type, errorThrown) {
ajaxError(xhr, type, errorThrown);
}
});
//完成按钮点击事件
var finishBTN = document.getElementById('finishBTN');
finishBTN.addEventListener('tap', function() {
if(chooseType == "single") {
if(checkSingleChoose() == false) {
mui.toast("请选择人员!");
return;
}
}else{
if(checkMultiChoose() == false) {
mui.toast("请选择人员!");
return;
}
}
mui.back();
}, false);
}
function checkSingleChoose() {
var radios = document.querySelectorAll('.peopleRadio');
for(n in radios) {
if(radios[n].checked == true) {
personName = radios[n].value;
personId = radios[n].id;
return true;
}
}
return false;
}
function checkMultiChoose() {
personName = new Array();
personId = new Array();
var flag = false;
var radios = document.querySelectorAll('.peopleRadio');
for(n in radios) {
if(radios[n].checked == true) {
personName.push(radios[n].value);
personId.push(radios[n].id);
flag = true;
}
}
return flag;
}
function putData(data) {
if (data != null) {
var table = document.getElementById('outerUl');
table.innerHTML = "";
for(n in data) {
//解析标题栏
var data_title = data[n];
table.innerHTML += '<li class="mui-table-view-cell mui-collapse"><a style="text-align: left;color: red;font-family: arial;">' + data_title.name + '</a></li>';
//解析部门层
var depts = data_title.children;
for(m in depts) {
var data_dept = depts[m];
//解析人员层
var buffer = new StringBuffer();
var peo = data_dept.children;
for(i in peo) {
var data_people = peo[i];
var name = data_people.name;
var id = data_people.id;
if(chooseType == "single") {
buffer.append('<div class="mui-input-row mui-radio mui-right">');
}else {
buffer.append('<div class="mui-input-row mui-checkbox">');
}
buffer.append('<label>' + name + '</label>');
var inputType = "checkbox";
if(chooseType == "single") {
inputType = "radio";
}
buffer.append('<input id="' + id + '" name="people" type="' + inputType + '" class="peopleRadio" value="' + name + '" onclick="show(this);"/>');
buffer.append('</div>');
}
table.innerHTML +=
'<li class="mui-table-view-cell mui-collapse">' +
'<a class="mui-navigate-right">' + data_dept.name + '</a>' +
'<div class="mui-collapse-content">' +
'<form class="mui-input-group">' +
buffer.toString() +
'</form>' +
'</div>' +
'</li>';
}
}
}
}
function show(obj) {
var showPeople = document.getElementById("show");
var showDiv = document.getElementById("showDiv");
showPeople.style.display = "block";
var checked = obj.checked;
var id = obj.id;
var name = obj.value;
if(checked == true) {
showDiv.innerHTML +=
'<a id="show_' + id + '" class="mui-control-item" href="#">' +
'<font style="color: blue;">' + name + '</font>' +
'</a>';
}else {
var delElement = document.querySelector('#show_' + id);
delElement.outerHTML = "";
}
}
//StringBuffer方法实现
function StringBuffer() {
this.content = new Array;
}
StringBuffer.prototype.append = function(str) {
this.content.push(str);
}
StringBuffer.prototype.toString = function() {
return this.content.join("");
}
window.addEventListener('init', function(event){
init();
fromPage = event.detail.fromPage;
chooseType = event.detail.chooseType;
callback = event.detail.callback;
});
gavinxu (作者)
那 beforeback 方法怎么定义呢?原来是在init里定义的
2015-06-24 17:20
DCloud_UNI_FXY
回复 gavinxu:你只在页面第一次加载进来的时候,初始化就行了。不要每次打开就初始化一遍
2015-06-24 23:37
gavinxu (作者)
好的,谢谢
2015-06-26 13:16