在我的应用程序中,每个 KiiUser 都有一个数据哈希表。但是我在使用 KiiObject 保存数据并下次获取时遇到了一些问题。我想在用户注册时创建一个具有特定 URI 的 KiiObject,这样每次他登录时我都可以导入它
KiiObject.objectWithURI(<URI>)
或者,如何使用 objectWithJSON()。如果可以避免,我不想使用 kiiQuery,因为每个用户只有一个对象。
谁能给我一个代码示例,如何在用户注册时创建此 Kiiobject 以及如何在每次登录时对其进行编辑;或者每次他编辑并保存时。这是我当前的一些代码,但我并不总是得到保存的数据(对象上未定义的保存的哈希表“数据”)
function performRegistration() {
try {
var user = KiiUser.userWithUsername(<username>, <password>);
user.register({
success: function(Auser) {
var bucket = Auser.bucketWithName("data");
data = bucket.createObject();
data.set("data", {});
data.saveAllFields({
success: function(theObject) {
console.log("Object saved!");
theObject.refresh({success:function(obj){
data=obj;
[....
...Some Code here to edit the...
...data object and save it....
...]
Kii.logger("User registered: " + Auser);
}});
},
failure: function(theObject, errorString) {
console.log("Error saving object: " + errorString);
}
});
},
failure: function(theUser, anErrorString) {
alert("Unable to register: " + anErrorString);
Kii.logger("Unable to register user: " + anErrorString);
}
});
} catch (e) {
alert("Unable to register: " + e.message);
Kii.logger("Unable to register user: " + e.message);
}
}
// the user clicked the 'sign in' button
function performLogin() {
KiiUser.authenticate(<username>, <password>, {
success: function(Auser) {
user = Auser;
var bucket = user.bucketWithName("data"); // a KiiBucket
var query = KiiQuery.queryWithClause();
var queryCallbacks = {
success: function(queryPerformed, r, nextQuery) {
console.log(r.constructor.name,r);
r[0].refresh({success:function(obj){
data=obj;
if(data.get("data")==undefined){
data.set("data",{});
}
data.save({success:function(Sobj){
data=Sobj;
[...
...some data object manipilation then save it
...]
}});
}});
},
failure: function(queryPerformed, anErrorString) {
}
};
bucket.executeQuery(query, queryCallbacks);
},
// callback for failed registration
failure: function(theUser, anErrorString) {
alert("Unable to register: " + anErrorString);
Kii.logger("Unable to register user: " + anErrorString);
}
});
};
Kii.initializeWithSite(<...>, <...>, KiiSite.US);
[...
other code
...]