Is this the right approach for using models in Webix Jet to query and store data returned from the server? In this model, I want to ask the server for “player data” by giving it an id.
// models/player.js
define([],function(){
// Object where the data will be stored
var data={}
// Clear out the current data without making a new Object
/*private*/ function clear() {
for (var property in data) {
delete data[property];
}
}
// Copy data into the data Object
/*private*/ function copyFrom(otherData) {
clear();
for (var property in otherData) {
data[property] = otherData[property];
}
}
// Load data given an id from the server and copy it
// to the data Object when ready.
// Returns a promise
/*public*/ function load(id) {
return webix.ajax().post(
"/admin/action/getPlayer",
{ userId:id },
function(text,data){
copyFrom(data.json().player);
}
);
};
// Return the data Object which will contain the last loaded data
// and the load function used to request data from the server
return {
data:data,
load:load
}
});