|
代码如下,我定义的this.userid在loadUserData里面进行新用户判断处理的时候获取不到document.addEventListener("deviceready", function(){
this.userid = window.device.uuid;
});处理的赋值。一直是默认的值,这是什么原因呢?必须放到addEventListener函数处理的外部才能正常赋值取值,代码中注释掉的部分。
define(function(require){
var $ = require("jquery");
var justep = require("$UI/system/lib/justep");
require("cordova!cordova-plugin-device");
var Model = function(){
this.callParent();
this.userid = "user1";
this.loadUser = true;
};
Model.prototype.modelLoad = function(event){
if(justep.Browser.isX5App){
document.addEventListener("deviceready", function(){
this.userid = window.device.uuid;
});
}
//this.userid = window.device.uuid;
this.comp("userData").setFilter("filter1", "fID='" + this.userid + "'");
};
Model.prototype.getImg = function(imgUrl){
return require.toUrl("./img/" + imgUrl);
};
Model.prototype.loadUserData = function(){
if (this.loadUser) {
var userData = this.comp("userData");
userData.refreshData();
if (userData.getCount() == 0) {
alert(this.userid);
userData.newData({
"defaultValues" : [ {
"fID" : this.userid,
"fName" : "新用户"
} ]
});
}
this.loadUser = false ;
}
};
Model.prototype.myConActive = function(event){
this.loadUserData();
};
Model.prototype.cartConActive = function(event){
this.loadUserData();
};
Model.prototype.saveUserBtnClick = function(event){
this.comp("userData").saveData({
"onSuccess":function(){
justep.Util.hint("用户信息保存成功");
}
});
};
return Model;
}); |
|