este-library-oldschool
Version:
Library for github.com/steida/este.git
272 lines (252 loc) • 6.33 kB
JavaScript
// Generated by github.com/steida/coffee2closure 900.1.18
/**
@fileoverview Base class for various sync/async storages. It defines common
API for models persistence. Since one of the most expensive operations in
an application is making remote calls, this class allows you to override
processQueue method, to enable batching all write calls to the server into
a single call.
*/
goog.provide('este.storage.Base');
goog.require('este.Collection');
goog.require('goog.result');
/**
@param {string} namespace
@param {string|number=} version
@constructor
*/
este.storage.Base = function(namespace, version) {
this.namespace = namespace != null ? namespace : '';
if (version != null) {
this.version = version.toString();
}
}
/**
@type {string}
*/
este.storage.Base.prototype.namespace = '';
/**
Use http://semver.org/
@type {string}
*/
este.storage.Base.prototype.version = '1';
/**
@type {Array.<Object>}
@protected
*/
este.storage.Base.prototype.queue = null;
/**
@param {este.Model} model
@param {este.Collection|string=} arg
@return {!goog.result.Result}
*/
este.storage.Base.prototype.add = function(model, arg) {
var url;
url = this.getUrl(model, arg);
if (!this.queue) {
return this.addInternal(model, url);
}
return this.enqueue('add', model, url);
};
/**
@param {este.Model} model
@param {este.Collection|string=} arg
@return {!goog.result.Result}
*/
este.storage.Base.prototype.load = function(model, arg) {
var url;
url = this.getUrl(model, arg);
return this.loadInternal(model, url);
};
/**
@param {este.Model} model
@param {este.Collection|string=} arg
@return {!goog.result.Result}
*/
este.storage.Base.prototype.save = function(model, arg) {
var url;
url = this.getUrl(model, arg);
if (!this.queue) {
return this.saveInternal(model, url);
}
return this.enqueue('save', model, url);
};
/**
@param {este.Model} model
@param {este.Collection|string=} arg
@return {!goog.result.Result}
*/
este.storage.Base.prototype.remove = function(model, arg) {
var url;
url = this.getUrl(model, arg);
if (!this.queue) {
return this.removeInternal(model, url);
}
return this.enqueue('remove', model, url);
};
/**
@param {este.Collection} collection
@param {Object=} params
@param {string=} url
@return {!goog.result.Result}
*/
este.storage.Base.prototype.query = function(collection, params, url) {
if (url == null) {
url = this.getUrl(null, collection);
}
return this.queryInternal(collection, url, params);
};
/**
Allows to perform basic CRUD operations within a Unit of Work. It is
important to note that no changes will be saved until saveChanges method
is called.
*/
este.storage.Base.prototype.openSession = function() {
if (this.queue) {
return;
}
return this.queue = [];
};
/**
Save all queued changes.
@return {!goog.result.Result}
*/
este.storage.Base.prototype.saveChanges = function() {
var queue;
queue = this.queue.slice(0);
this.queue = null;
return this.processQueue(queue);
};
/**
@param {este.Model.Event} e
@return {!goog.result.Result}
*/
este.storage.Base.prototype.saveChangesFromEvent = function(e) {
var added, i, j, len, len1, ref, ref1, removed, result, results;
if (e.type === 'update') {
e = e.origin;
}
results = [];
switch (e.type) {
case 'add':
ref = e.added;
for (i = 0, len = ref.length; i < len; i++) {
added = ref[i];
result = this.add(added);
if (result) {
results.push(result);
}
}
break;
case 'remove':
ref1 = e.removed;
for (j = 0, len1 = ref1.length; j < len1; j++) {
removed = ref1[j];
result = this.remove(removed);
if (result) {
results.push(result);
}
}
break;
case 'change':
result = this.save(e.model);
if (result) {
results.push(result);
}
}
if (results.length) {
return goog.result.combineOnSuccess.apply(this, results);
}
return goog.result.successfulResult(null);
};
/**
@param {este.Model} model
@param {string} url
@return {!goog.result.Result}
@protected
*/
este.storage.Base.prototype.addInternal = goog.abstractMethod;
/**
@param {este.Model} model
@param {string} url
@return {!goog.result.Result}
@protected
*/
este.storage.Base.prototype.loadInternal = goog.abstractMethod;
/**
@param {este.Model} model
@param {string} url
@return {!goog.result.Result}
@protected
*/
este.storage.Base.prototype.saveInternal = goog.abstractMethod;
/**
@param {este.Model} model
@param {string} url
@return {!goog.result.Result}
@protected
*/
este.storage.Base.prototype.removeInternal = goog.abstractMethod;
/**
@param {este.Collection} collection
@param {string} url
@param {Object=} params
@return {!goog.result.Result}
*/
este.storage.Base.prototype.queryInternal = goog.abstractMethod;
/**
@param {string} method
@param {este.Model} model
@param {string} url
@protected
*/
este.storage.Base.prototype.enqueue = function(method, model, url) {
this.queue.push({
method: method,
model: model,
url: url
});
return null;
};
/**
@param {Array.<Object>} queue
@return {!goog.result.Result}
@protected
*/
este.storage.Base.prototype.processQueue = function(queue) {
var command, i, len, result, results;
results = [];
for (i = 0, len = queue.length; i < len; i++) {
command = queue[i];
result = (function() {
switch (command.method) {
case 'add':
return this.addInternal(command.model, command.url);
case 'save':
return this.saveInternal(command.model, command.url);
case 'remove':
return this.removeInternal(command.model, command.url);
}
}).call(this);
results.push(result);
}
if (results.length) {
return goog.result.combineOnSuccess.apply(this, results);
}
return goog.result.successfulResult(null);
};
/**
@param {este.Model} model
@param {este.Collection|string=} arg
@return {string}
*/
este.storage.Base.prototype.getUrl = function(model, arg) {
var url;
if (!arg) {
url = model.getUrl();
} else if (arg instanceof este.Collection) {
url = arg.getUrl();
} else {
url = arg;
}
return url;
};