Commit 1527f4a9 authored by Tristan Cavelier's avatar Tristan Cavelier

jslint pass storage.js

parent ac79eed3
var storage = function(spec, my) { /*jslint indent: 2, maxlen: 80, sloppy: true */
var that = {}; /*global command: true, jobManager: true, job: true */
spec = spec || {}; var storage = function (spec, my) {
my = my || {}; var that = {}, priv = {};
// Attributes // spec = spec || {};
var priv = {}; my = my || {};
priv.type = spec.type || ''; // Attributes //
priv.type = spec.type || '';
// Methods //
Object.defineProperty(that,"getType",{ // Methods //
configurable:false,enumerable:false,writable:false,value: Object.defineProperty(that, "getType", {
function() { configurable: false,
return priv.type; enumerable: false,
} writable: false,
value: function () {
return priv.type;
}
});
/**
* Execute the command on this storage.
* @method execute
* @param {object} command The command
*/
that.execute = function (command) {
that.success = command.success;
that.error = command.error;
that.retry = command.retry;
that.end = command.end;
if (that.validate(command)) {
command.executeOn(that);
}
};
/**
* Override this function to validate specifications.
* @method isValid
* @return {boolean} true if ok, else false.
*/
that.isValid = function () {
return true;
};
that.validate = function () {
var mess = that.validateState();
if (mess) {
that.error({
"status": 0,
"statusText": "Invalid Storage",
"error": "invalid_storage",
"message": mess,
"reason": mess
});
return false;
}
return true;
};
/**
* Returns a serialized version of this storage.
* @method serialized
* @return {object} The serialized storage.
*/
that.serialized = function () {
var o = that.specToStore() || {};
o.type = that.getType();
return o;
};
/**
* Returns an object containing spec to store on localStorage, in order to
* be restored later if something wrong happen.
* Override this method!
* @method specToStore
* @return {object} The spec to store
*/
that.specToStore = function () {
return {};
};
/**
* Validate the storage state. It returns a empty string all is ok.
* @method validateState
* @return {string} empty: ok, else error message.
*/
that.validateState = function () {
return '';
};
that.post = function () {
setTimeout(function () {
that.error({
"status": 0,
"statusText": "Not Implemented",
"error": "not_implemented",
"message": "\"Post\" command is not implemented",
"reason": "Command not implemented"
});
}); });
};
/**
* Execute the command on this storage. that.put = function () {
* @method execute setTimeout(function () {
* @param {object} command The command that.error({
*/ "status": 0,
that.execute = function(command) { "statusText": "Not Implemented",
that.success = command.success; "error": "not_implemented",
that.error = command.error; "message": "\"Put\" command is not implemented",
that.retry = command.retry; "reason": "Command not implemented"
that.end = command.end; });
if (that.validate(command)) { });
command.executeOn(that); };
}
}; that.putAttachment = function () {
setTimeout(function () {
/** that.error({
* Override this function to validate specifications. "status": 0,
* @method isValid "statusText": "Not Implemented",
* @return {boolean} true if ok, else false. "error": "not_implemented",
*/ "message": "\"PutAttachment\" command is not implemented",
that.isValid = function() { "reason": "Command not implemented"
return true; });
}; });
};
that.validate = function () {
var mess = that.validateState(); that.get = function () {
if (mess) { setTimeout(function () {
that.error({ that.error({
status:0,statusText:'Invalid Storage', "status": 0,
error:'invalid_storage', "statusText": "Not Implemented",
message:mess,reason:mess "error": "not_implemented",
}); "message": "\"Get\" command is not implemented",
return false; "reason": "Command not implemented"
} });
return true; });
}; };
/** that.allDocs = function () {
* Returns a serialized version of this storage. setTimeout(function () {
* @method serialized that.error({
* @return {object} The serialized storage. "status": 0,
*/ "statusText": "Not Implemented",
that.serialized = function() { "error": "not_implemented",
var o = that.specToStore() || {}; "message": "\"AllDocs\" command is not implemented",
o["type"] = that.getType(); "reason": "Command not implemented"
return o; });
}; });
};
/**
* Returns an object containing spec to store on localStorage, in order to that.remove = function () {
* be restored later if something wrong happen. setTimeout(function () {
* Override this method! that.error({
* @method specToStore "status": 0,
* @return {object} The spec to store "statusText": "Not Implemented",
*/ "error": "not_implemented",
that.specToStore = function () { "message": "\"Remove\" command is not implemented",
return {}; "reason": "Command not implemented"
}; });
});
/** };
* Validate the storage state. It returns a empty string all is ok.
* @method validateState that.success = function () {};
* @return {string} empty: ok, else error message. that.retry = function () {};
*/ that.error = function () {};
that.validateState = function() { that.end = function () {}; // terminate the current job.
return '';
}; priv.newCommand = function (method, spec) {
var o = spec || {};
that.post = function () { o.label = method;
setTimeout(function () { return command(o, my);
that.error({ };
"status": 0,
"statusText": "Not Implemented", priv.storage = my.storage;
"error": "not_implemented", delete my.storage;
"message": "\"Post\" command is not implemented",
"reason": "Command not implemented" that.addJob = function (method, storage_spec, doc, option, success, error) {
}); var command_opt = {
}); options: option,
}; callbacks: {success: success, error: error}
that.put = function () {
setTimeout(function () {
that.error({
"status": 0,
"statusText": "Not Implemented",
"error": "not_implemented",
"message": "\"Put\" command is not implemented",
"reason": "Command not implemented"
});
});
};
that.putAttachment = function () {
setTimeout(function () {
that.error({
"status": 0,
"statusText": "Not Implemented",
"error": "not_implemented",
"message": "\"PutAttachment\" command is not implemented",
"reason": "Command not implemented"
});
});
};
that.get = function () {
setTimeout(function () {
that.error({
"status": 0,
"statusText": "Not Implemented",
"error": "not_implemented",
"message": "\"Get\" command is not implemented",
"reason": "Command not implemented"
});
});
};
that.allDocs = function () {
setTimeout(function () {
that.error({
"status": 0,
"statusText": "Not Implemented",
"error": "not_implemented",
"message": "\"AllDocs\" command is not implemented",
"reason": "Command not implemented"
});
});
};
that.remove = function () {
setTimeout(function () {
that.error({
"status": 0,
"statusText": "Not Implemented",
"error": "not_implemented",
"message": "\"Remove\" command is not implemented",
"reason": "Command not implemented"
});
});
};
that.success = function() {};
that.retry = function() {};
that.error = function() {};
that.end = function() {}; // terminate the current job.
priv.newCommand = function (method, spec) {
var o = spec || {};
o.label = method;
return command (o, my);
};
priv.storage = my.storage;
delete my.storage;
that.addJob = function (method,storage_spec,doc,option,success,error) {
var command_opt = {
options: option,
callbacks:{success:success,error:error}
};
if (doc) {
if (method === 'get') {
command_opt.docid = doc;
} else {
command_opt.doc = doc;
}
}
jobManager.addJob (
job({
storage:priv.storage(storage_spec||{}),
command:priv.newCommand(method,command_opt)
}, my)
);
}; };
if (doc) {
return that; if (method === 'get') {
command_opt.docid = doc;
} else {
command_opt.doc = doc;
}
}
jobManager.addJob(job({
storage: priv.storage(storage_spec || {}),
command: priv.newCommand(method, command_opt)
}, my));
};
return that;
}; };
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment