ironwing
Version:
Ironwing is a lightweight front-end data library for model like data representations
84 lines (67 loc) • 1.98 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', {
value: true
});
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
var _lodashUtilityUniqueId = require('lodash/utility/uniqueId');
var _lodashUtilityUniqueId2 = _interopRequireDefault(_lodashUtilityUniqueId);
var instances = [],
Storage = {};
Storage.store = function (item) {
var found = Storage.find(item.type, item.attr.id);
if (!found) {
item.__unique = (0, _lodashUtilityUniqueId2['default'])();
instances.push(item);
} else {
item.__unique = found.__unique;
found.attr = item.attr;
}
};
Storage.update = function (model, newAttr) {
instances.forEach(function (item) {
if (item.type === model.type && item.__unique === model.__unique) {
item.attr = newAttr;
}
});
};
/**
* Rertrieves a resource from the local cache
* @param {String} type The model's type
* @param {Number} id The models'id
* @return {Model}
*/
Storage.find = function (type, id) {
var found;
instances.forEach(function (item) {
if (item.type === type && parseInt(item.attr.id) === parseInt(id)) {
found = item;
}
});
return found;
};
/**
* Finds all resrources from the local cache
* @param {String} type The mode's type
* @return {Object}
*/
Storage.findAll = function (type) {
return instances.filter(function (model) {
return model.type === type;
});
};
Storage['delete'] = function (item) {
instances = instances.filter(function (model) {
return model.attr.id !== item.attr.id ? true : model.type !== item.type ? true : false;
});
};
Storage.getSize = function () {
return instances.length;
};
// Storage.search = function(type, what){
// var models = new Model(type);
// return models.filter(function(model){
// return model.attr[ Object.keys(what)[0] ] === what[ Object.keys(what)[0] ]
// });
// }
exports['default'] = Storage;
module.exports = exports['default'];