1.首先从后台取数据。
function UsertypeSelect(userid){
$.ajax({
url: "staticOperationCtrl/GetSelectData",
data: {
"userid":userId
},
success: function(data) {
if (data != "") {
var obj = eval(data); //后台给返的数据
localStorage['UsertypeSelect']=JSON.stringify(obj);
//给localStorage里面添加一个key为UsertypeSelect的字段,并且其value值为obj
};
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
ShowQuestDataErrMsg();
}
});
}
localStorage['UsertypeSelect']=JSON.stringify(obj);
//给localStorage里面添加一个key为UsertypeSelect的字段,并且其value值为obj
2.第二次要用相同的数据时,首先从localStorage取数据,如果local没有再去后台取数据
if(localStorage.UsertypeSelect !='') {
var gettpl = document.getElementById('getUsertypeTpl').innerHTML;
laytpl(gettpl).render(JSON.parse(localStorage.UsertypeSelect), function(html) {
document.getElementById('usertypeSelectEdit').innerHTML = html;
});//这里是本次访问接口时要做的事
}else{
$.ajax({
url: "staticOperationCtrl/GetSelectData",
data: {
"kind":"role",
"roleid":JSON.parse(localStorage.roleData).id,//此处的值也是从localStorage取出的。
"userid":localStorage.userId
},
success: function(data) {
if (data != "") {
var obj = eval(data);
var gettpl = document.getElementById('getUsertypeTpl').innerHTML;
laytpl(gettpl).render(obj, function(html) {
document.getElementById('usertypeSelectEdit').innerHTML = html;
});
};
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
ShowQuestDataErrMsg();
}
});
}
}
JSON.parse(localStorage.UsertypeSelect), function(html) {
document.getElementById('usertypeSelectEdit').innerHTML = html;
});//这里是本次访问接口时要做的事
}else{
$.ajax({
url: "staticOperationCtrl/GetSelectData",
data: {
"kind":"role",
"roleid":JSON.parse(localStorage.roleData).id,//此处的值也是从localStorage取出的。
"userid":localStorage.userId
},
success: function(data) {
if (data != "") {
var obj = eval(data);
var gettpl = document.getElementById('getUsertypeTpl').innerHTML;
laytpl(gettpl).render(obj, function(html) {
document.getElementById('usertypeSelectEdit').innerHTML = html;
});
};
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
ShowQuestDataErrMsg();
}
});
}
}