micromodel
Advanced tools
Comparing version 0.1.3 to 0.2.0
@@ -1,532 +0,527 @@ | ||
// Generated by CoffeeScript 1.3.3 | ||
// Generated by CoffeeScript 1.4.0 | ||
(function() { | ||
var MicroModel, global, underscoreMethods, _, | ||
var attributeRe, exports, requireBackboneEvents, requireEventEmitter, _, | ||
__hasProp = {}.hasOwnProperty, | ||
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }, | ||
__slice = [].slice; | ||
global = this; | ||
if ((typeof module !== "undefined" && module !== null ? module.exports : void 0) != null) { | ||
exports = module.exports; | ||
_ = require('underscore'); | ||
requireEventEmitter = function() { | ||
return require('events').EventEmitter; | ||
}; | ||
requireBackboneEvents = function() { | ||
return require('backbone').Events; | ||
}; | ||
} else { | ||
exports = window; | ||
_ = window._ || require('underscore'); | ||
requireEventEmitter = function() { | ||
return window.EventEmitter || require('events').EventEmitter; | ||
}; | ||
requireBackboneEvents = function() { | ||
var _ref; | ||
return ((_ref = global.Backbone) != null ? _ref.Events : void 0) || require('backbone').Events; | ||
}; | ||
} | ||
MicroModel = typeof exports !== "undefined" && exports !== null ? exports : global.MicroModel = {}; | ||
attributeRe = /^_/; | ||
_ = global._ || require('underscore'); | ||
underscoreMethods = ['forEach', 'each', 'map', 'reduce', 'reduceRight', 'find', 'detect', 'filter', 'select', 'reject', 'every', 'all', 'some', 'any', 'include', 'contains', 'invoke', 'max', 'min', 'sortBy', 'sortedIndex', 'toArray', 'size', 'first', 'initial', 'rest', 'last', 'without', 'indexOf', 'shuffle', 'lastIndexOf', 'isEmpty', 'groupBy']; | ||
MicroModel.withUnderscoreCollection = function(klass) { | ||
exports.Model = function(klass) { | ||
var proto; | ||
if (klass == null) { | ||
klass = exports.BaseClass(); | ||
} | ||
proto = klass.prototype; | ||
return _.each(underscoreMethods, function(method) { | ||
return proto[method] = function() { | ||
return _[method].apply(_, [this.models].concat(_.toArray(arguments))); | ||
}; | ||
}); | ||
proto.isModel = true; | ||
proto.initialize = function(attrs, options) { | ||
if (this.defaults) { | ||
this.set(this.defaults, options); | ||
} | ||
if (attrs) { | ||
this.set(attrs, options); | ||
} | ||
return this; | ||
}; | ||
proto.isEqual = function(other) { | ||
var name, otherSize, size, value; | ||
if (this === other) { | ||
return true; | ||
} | ||
if (!(other && this.id === other.id && other.isModel)) { | ||
return false; | ||
} | ||
size = 0; | ||
for (name in this) { | ||
if (!__hasProp.call(this, name)) continue; | ||
value = this[name]; | ||
if (!(!attributeRe.test(name))) { | ||
continue; | ||
} | ||
size += 1; | ||
if (!_.isEqual(value, other[name])) { | ||
return false; | ||
} | ||
} | ||
otherSize = 0; | ||
for (name in other) { | ||
if (!__hasProp.call(other, name)) continue; | ||
if (!attributeRe.test(name)) { | ||
otherSize += 1; | ||
} | ||
} | ||
return size === otherSize; | ||
}; | ||
proto.set = function(attrs, options) { | ||
var changes, name, newValue, oldValue; | ||
if (attrs == null) { | ||
return {}; | ||
} | ||
changes = {}; | ||
for (name in attrs) { | ||
if (!__hasProp.call(attrs, name)) continue; | ||
newValue = attrs[name]; | ||
if (!(!attributeRe.test(name))) { | ||
continue; | ||
} | ||
oldValue = this[name]; | ||
if (!_.isEqual(oldValue, newValue)) { | ||
changes[name] = oldValue; | ||
} | ||
this[name] = newValue; | ||
} | ||
return changes; | ||
}; | ||
proto.clone = function() { | ||
return new this.constructor(this.attributes()); | ||
}; | ||
proto.clear = function(options) { | ||
var changes, name; | ||
changes = attributes(); | ||
for (name in this) { | ||
if (!__hasProp.call(this, name)) continue; | ||
delete this[name]; | ||
} | ||
return changes; | ||
}; | ||
proto.validate = function() { | ||
var errors, msg, name, validator, _ref, _ref1; | ||
if (!this.validations) { | ||
return null; | ||
} | ||
errors = {}; | ||
_ref = this.validations; | ||
for (name in _ref) { | ||
if (!__hasProp.call(_ref, name)) continue; | ||
validator = _ref[name]; | ||
if (msg = validator(this[name])) { | ||
((_ref1 = errors[name]) != null ? _ref1 : errors[name] = []).push(msg); | ||
} | ||
} | ||
if (_.isEmpty(errors)) { | ||
return null; | ||
} else { | ||
return errors; | ||
} | ||
}; | ||
proto.isValid = function() { | ||
return !(this.validate() != null); | ||
}; | ||
proto.attributes = function() { | ||
var attrs, name, value; | ||
attrs = {}; | ||
for (name in this) { | ||
if (!__hasProp.call(this, name)) continue; | ||
value = this[name]; | ||
if (!attributeRe.test(name)) { | ||
attrs[name] = value; | ||
} | ||
} | ||
return attrs; | ||
}; | ||
proto.toJSON = function() { | ||
return this.attributes(); | ||
}; | ||
proto.inspect = function() { | ||
return JSON.stringify(this.toJSON()); | ||
}; | ||
proto.toString = function() { | ||
return this.inspect(); | ||
}; | ||
return klass; | ||
}; | ||
MicroModel.withUnderscoreEqual = function(klass) { | ||
exports.Model.Events = function(klass, type) { | ||
var proto; | ||
exports.Events(klass, type); | ||
proto = klass.prototype; | ||
return proto.isEqual = proto.eql; | ||
}; | ||
MicroModel.withEventEmitter = function(klass) { | ||
var EventEmitter, initializeWithoutEventEmitter, methods, proto; | ||
proto = klass.prototype; | ||
EventEmitter = global.EventEmitter || require('events').EventEmitter; | ||
initializeWithoutEventEmitter = proto.initialize; | ||
methods = { | ||
initialize: function() { | ||
EventEmitter.apply(this); | ||
return initializeWithoutEventEmitter.apply(this, arguments); | ||
proto.setWithoutEvents = proto.set; | ||
proto.set = function(attrs, options) { | ||
var changes; | ||
changes = this.setWithoutEvents(attrs, options); | ||
this._emitChanges(changes, options); | ||
return changes; | ||
}; | ||
proto.clearWithoutEvents = proto.clear; | ||
proto.clear = function(options) { | ||
var changes; | ||
changes = this.clearWithoutEvents(options); | ||
this._emitChanges(changes, options); | ||
return changes; | ||
}; | ||
proto._emitChanges = function(changes, options) { | ||
var name, oldValue; | ||
if (!(_.isEmpty(changes) && !(options != null ? options.silent : void 0))) { | ||
for (name in changes) { | ||
oldValue = changes[name]; | ||
this.trigger("change:" + name, this, oldValue); | ||
} | ||
return this.trigger('change', this, changes); | ||
} | ||
}; | ||
proto.isEventEmitter = true; | ||
_(proto).extend(EventEmitter.prototype); | ||
return _(proto).extend(methods); | ||
return klass; | ||
}; | ||
MicroModel.withBackboneEvents = function(klass) { | ||
var Events, methods, proto; | ||
exports.Collection = function(klass) { | ||
var method, proto, underscoreMethods, underscoreMethodsReturningCollection, _fn, _fn1, _i, _j, _len, _len1; | ||
if (klass == null) { | ||
klass = exports.BaseClass(); | ||
} | ||
proto = klass.prototype; | ||
Events = global.Backbone.Events || require('backbone').Events; | ||
methods = { | ||
addListener: function() { | ||
return this.on.apply(this, arguments); | ||
}, | ||
removeListener: function() { | ||
return this.off.apply(this, arguments); | ||
}, | ||
emit: function() { | ||
return this.trigger.apply(this, arguments); | ||
} | ||
proto.isCollection = true; | ||
proto.initialize = function() { | ||
var args, _ref; | ||
args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; | ||
_ref = [[], 0, {}], this.models = _ref[0], this.length = _ref[1], this.ids = _ref[2]; | ||
this.add.apply(this, args); | ||
return this; | ||
}; | ||
proto.isEventEmitter = true; | ||
_(proto).extend(Events); | ||
return _(proto).extend(methods); | ||
}; | ||
MicroModel.withModel = function(klass) { | ||
var attributes, initializeWithoutModel, methods, proto; | ||
proto = klass.prototype; | ||
attributes = function(obj) { | ||
var attrs, name, value; | ||
attrs = {}; | ||
for (name in obj) { | ||
if (!__hasProp.call(obj, name)) continue; | ||
value = obj[name]; | ||
if (!/^_/.test(name)) { | ||
attrs[name] = value; | ||
} | ||
proto.add = function() { | ||
var args, models, options, _ref; | ||
args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; | ||
_ref = _.isArray(args[0]) ? args : [args, {}], models = _ref[0], options = _ref[1]; | ||
if (options == null) { | ||
options = {}; | ||
} | ||
return attrs; | ||
return this._add(models, options); | ||
}; | ||
initializeWithoutModel = proto.initialize; | ||
methods = { | ||
initialize: function(attrs, options) { | ||
var _ref; | ||
if (initializeWithoutModel != null) { | ||
initializeWithoutModel.call(this, attrs, options); | ||
proto._add = function(models, options) { | ||
var added, model, _i, _len; | ||
added = []; | ||
for (_i = 0, _len = models.length; _i < _len; _i++) { | ||
model = models[_i]; | ||
if (!model.isModel) { | ||
klass = this.model || (function() { | ||
throw new Error("no Model for Collection (" + this + ")!"); | ||
}).call(this); | ||
model = new klass(model); | ||
} | ||
_ref = [_.uniqueId('c'), {}], this._cid = _ref[0], this._changed = _ref[1]; | ||
if (this.defaults) { | ||
this.set(this.defaults, options); | ||
if (model.id == null) { | ||
throw new Error("no id for Model (" + model + ")!"); | ||
} | ||
if (attrs) { | ||
return this.set(attrs, options); | ||
if (model.id in this.ids) { | ||
continue; | ||
} | ||
}, | ||
eql: function(other, strict) { | ||
var name, otherSize, size, value; | ||
if (strict == null) { | ||
strict = false; | ||
this.ids[model.id] = this.models.length; | ||
this.models.push(model); | ||
added.push(model); | ||
} | ||
this.length = this.models.length; | ||
return added; | ||
}; | ||
proto["delete"] = function() { | ||
var args, models, options, _ref; | ||
args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; | ||
_ref = _.isArray(args[0]) ? args : [args, {}], models = _ref[0], options = _ref[1]; | ||
if (options == null) { | ||
options = {}; | ||
} | ||
return this._delete(models, options); | ||
}; | ||
proto.del = function() { | ||
var args; | ||
args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; | ||
return this["delete"].apply(this, args); | ||
}; | ||
proto._delete = function(models, options) { | ||
var deleted, deletedIndexes, index, model, oldModels, _i, _j, _k, _len, _len1, _len2, _ref, _ref1, _ref2; | ||
_ref = [[], {}], deleted = _ref[0], deletedIndexes = _ref[1]; | ||
for (_i = 0, _len = models.length; _i < _len; _i++) { | ||
model = models[_i]; | ||
if (!(model.id in this.ids)) { | ||
continue; | ||
} | ||
if (this === other) { | ||
return true; | ||
} | ||
if (!(other && this.id === other.id && _.isObject(other))) { | ||
return false; | ||
} | ||
if (strict) { | ||
if (!(this._cid === other._cid && this.constructor === other.constructor)) { | ||
return false; | ||
deleted.push(model); | ||
deletedIndexes[this.ids[model.id]] = true; | ||
} | ||
if (!_.isEmpty(deletedIndexes)) { | ||
oldModels = this.models; | ||
_ref1 = [[], {}], this.models = _ref1[0], this.ids = _ref1[1]; | ||
for (index = _j = 0, _len1 = oldModels.length; _j < _len1; index = ++_j) { | ||
model = oldModels[index]; | ||
if (!(index in deletedIndexes)) { | ||
this.models.push(model); | ||
} | ||
} | ||
size = 0; | ||
for (name in this) { | ||
if (!__hasProp.call(this, name)) continue; | ||
value = this[name]; | ||
if (!(!/^_/.test(name))) { | ||
continue; | ||
} | ||
size += 1; | ||
if (!_.isEqual(value, other[name])) { | ||
return false; | ||
} | ||
_ref2 = this.models; | ||
for (index = _k = 0, _len2 = _ref2.length; _k < _len2; index = ++_k) { | ||
model = _ref2[index]; | ||
this.ids[model.id] = index; | ||
} | ||
otherSize = 0; | ||
for (name in other) { | ||
if (!__hasProp.call(other, name)) continue; | ||
if (!/^_/.test(name)) { | ||
otherSize += 1; | ||
} | ||
} | ||
return size === otherSize; | ||
}, | ||
equal: function(other) { | ||
return this.eql(other, true); | ||
}, | ||
set: function(obj, options) { | ||
var attrs, currentValue, name, newValue, permit, permited, type, value, _ref; | ||
if (options == null) { | ||
options = {}; | ||
} | ||
if (!obj) { | ||
return; | ||
} | ||
attrs = attributes(obj); | ||
if (permit = options.permit) { | ||
permited = {}; | ||
for (name in attrs) { | ||
value = attrs[name]; | ||
if (__indexOf.call(permit, name) >= 0) { | ||
permited[name] = value; | ||
} | ||
} | ||
attrs = permited; | ||
} | ||
if (this.schema) { | ||
_ref = this.schema; | ||
for (name in _ref) { | ||
type = _ref[name]; | ||
attrs[name] = MicroModel.cast(attrs[name], type); | ||
} | ||
} | ||
if (this.validate(attrs) && options.validate !== false) { | ||
} | ||
this.length = this.models.length; | ||
return deleted; | ||
}; | ||
proto.get = function(id) { | ||
var index; | ||
if ((index = this.ids[id]) != null) { | ||
return this.models[index]; | ||
} | ||
}; | ||
proto.has = function(id) { | ||
return id in this.ids; | ||
}; | ||
proto.at = function(index) { | ||
return this.models[index]; | ||
}; | ||
proto.clear = function(options) { | ||
var deleted, _ref; | ||
if (options == null) { | ||
options = {}; | ||
} | ||
deleted = this.models; | ||
_ref = [[], 0, {}], this.models = _ref[0], this.length = _ref[1], this.ids = _ref[2]; | ||
return deleted; | ||
}; | ||
proto.inspect = function() { | ||
return JSON.stringify(this.toJSON()); | ||
}; | ||
proto.toString = function() { | ||
return this.inspect(); | ||
}; | ||
proto.toJSON = function() { | ||
return this.models; | ||
}; | ||
proto.isEqual = function(other) { | ||
var index, model, _i, _len, _ref; | ||
if (this === other) { | ||
return true; | ||
} | ||
if (!(other && other.length === this.length && other.isCollection)) { | ||
return false; | ||
} | ||
_ref = this.models; | ||
for (index = _i = 0, _len = _ref.length; _i < _len; index = ++_i) { | ||
model = _ref[index]; | ||
if (!model.isEqual(other.at(index))) { | ||
return false; | ||
} | ||
this._changed = {}; | ||
if (!options.silent) { | ||
for (name in attrs) { | ||
newValue = attrs[name]; | ||
currentValue = this[name]; | ||
if (!_.isEqual(currentValue, newValue)) { | ||
this._changed[name] = currentValue; | ||
this[name] = newValue; | ||
} | ||
} | ||
} else { | ||
for (name in attrs) { | ||
newValue = attrs[name]; | ||
this[name] = newValue; | ||
} | ||
} | ||
if (this.isEventEmitter && !options.silent && !_.isEmpty(this._changed)) { | ||
for (name in this._changed) { | ||
this.emit("change:" + name, this); | ||
} | ||
this.emit('change', this); | ||
} | ||
return true; | ||
}, | ||
clone: function() { | ||
return new this.constructor(this.attributes()); | ||
}, | ||
clear: function() { | ||
var name; | ||
for (name in this) { | ||
if (!__hasProp.call(this, name)) continue; | ||
delete this[name]; | ||
} | ||
this._changed = {}; | ||
return this; | ||
}, | ||
validate: function(attrs) { | ||
var errors, msg, name, value, _base, _ref; | ||
if (attrs == null) { | ||
attrs = {}; | ||
} | ||
if (!this.validations) { | ||
return null; | ||
} | ||
errors = {}; | ||
for (name in attrs) { | ||
value = attrs[name]; | ||
if (msg = typeof (_base = this.validations)[name] === "function" ? _base[name](value) : void 0) { | ||
((_ref = errors[name]) != null ? _ref : errors[name] = []).push(msg); | ||
} | ||
} | ||
if (_.isEmpty(errors)) { | ||
return null; | ||
} else { | ||
return errors; | ||
} | ||
}, | ||
isValid: function() { | ||
return this.validate(this); | ||
}, | ||
attributes: function() { | ||
return attributes(this); | ||
}, | ||
inspect: function() { | ||
return JSON.stringify(this.attributes()); | ||
}, | ||
toString: function() { | ||
return this.inspect(); | ||
}, | ||
toJSON: function() { | ||
return this.attributes(); | ||
} | ||
return true; | ||
}; | ||
proto.isModel = true; | ||
_(proto).extend(methods); | ||
return MicroModel.withUnderscoreEqual(klass); | ||
underscoreMethodsReturningCollection = ['forEach', 'each', 'map', 'filter', 'select', 'reject', 'every', 'all', 'some', 'any', 'sortBy', 'toArray', 'rest', 'without', 'shuffle']; | ||
_fn = function(method) { | ||
return proto[method] = function() { | ||
var list, _ref; | ||
list = (_ref = _[method]).apply.apply(_ref, [_, this.models].concat(__slice.call(arguments))); | ||
return new this.constructor(list); | ||
}; | ||
}; | ||
for (_i = 0, _len = underscoreMethodsReturningCollection.length; _i < _len; _i++) { | ||
method = underscoreMethodsReturningCollection[_i]; | ||
_fn(method); | ||
} | ||
underscoreMethods = ['reduce', 'reduceRight', 'find', 'detect', 'include', 'contains', 'invoke', 'max', 'min', 'sortedIndex', 'size', 'first', 'initial', 'last', 'indexOf', 'lastIndexOf', 'isEmpty', 'groupBy', 'countBy']; | ||
_fn1 = function(method) { | ||
return proto[method] = function() { | ||
var _ref; | ||
return (_ref = _[method]).apply.apply(_ref, [_, this.models].concat(__slice.call(arguments))); | ||
}; | ||
}; | ||
for (_j = 0, _len1 = underscoreMethods.length; _j < _len1; _j++) { | ||
method = underscoreMethods[_j]; | ||
_fn1(method); | ||
} | ||
return klass; | ||
}; | ||
MicroModel.cast = function(value, type) { | ||
var tmp; | ||
if (_.isFunction(type)) { | ||
return type(value); | ||
} else if (type === String) { | ||
return v.toString(); | ||
} else if (type === Number) { | ||
if (_.isNumber(v)) { | ||
return v; | ||
} else if (_.isString(v)) { | ||
tmp = parseInt(v); | ||
if (_.isNumber(tmp)) { | ||
return tmp; | ||
} | ||
exports.Collection.Sorted = function(klass) { | ||
var proto; | ||
proto = klass.prototype; | ||
proto.isSortedCollection = true; | ||
if (proto.hasEvents) { | ||
throw new Error("Sorted mixin should be applied before Events!"); | ||
} | ||
proto.sort = function(options) { | ||
return this._sort(options); | ||
}; | ||
proto._sort = function(options) { | ||
var changed, index, model, _i, _len, _ref; | ||
if ((options != null ? options.comparator : void 0) != null) { | ||
this.comparator = options.comparator; | ||
} | ||
} else if (type === Boolean) { | ||
if (_.isBoolean(v)) { | ||
return v; | ||
} else if (_.isString(v)) { | ||
return v === 'true'; | ||
if (!this.comparator) { | ||
return false; | ||
} | ||
} else if (type === Date) { | ||
if (_.isDate(v)) { | ||
return v; | ||
} else if (_.isString(v)) { | ||
tmp = new Date(v); | ||
if (_.isDate(tmp)) { | ||
return tmp; | ||
if (this.comparator.length === 1) { | ||
this.models = _(this.models).sortBy(this.comparator); | ||
} else { | ||
this.models.sort(this.comparator); | ||
} | ||
changed = false; | ||
_ref = this.models; | ||
for (index = _i = 0, _len = _ref.length; _i < _len; index = ++_i) { | ||
model = _ref[index]; | ||
if (this.ids[model.id] !== index) { | ||
changed = true; | ||
} | ||
this.ids[model.id] = index; | ||
} | ||
} else { | ||
throw "can't cast to unknown type (" + type + ")!"; | ||
} | ||
return changed; | ||
}; | ||
proto._addWithoutSort = proto._add; | ||
proto._add = function(models, options) { | ||
var added; | ||
added = this._addWithoutSort(models, options); | ||
if (added.length > 0) { | ||
this._sort(options); | ||
} else if ((options != null ? options.comparator : void 0) != null) { | ||
this.comparator = options.comparator; | ||
} | ||
return added; | ||
}; | ||
return klass; | ||
}; | ||
MicroModel.withCollection = function(klass) { | ||
var initializeWithoutCollection, methods, proto, _proxyModelEvent; | ||
exports.Collection.Events = function(klass, type) { | ||
var proto; | ||
exports.Events(klass, type); | ||
proto = klass.prototype; | ||
_proxyModelEvent = function(model) { | ||
return this.emit('model:change', model, this); | ||
proto.initializeWithoutEvents = proto.initialize; | ||
proto.initialize = function() { | ||
var args, | ||
_this = this; | ||
args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; | ||
this._forwardModelChangeEvent = function(model, changes) { | ||
return _this.trigger('model:change', model, changes, _this); | ||
}; | ||
return this.initializeWithoutEvents.apply(this, args); | ||
}; | ||
initializeWithoutCollection = proto.initialize; | ||
methods = { | ||
initialize: function(models, options) { | ||
var _ref; | ||
if (options == null) { | ||
options = {}; | ||
if (proto.sort != null) { | ||
proto.sortWithoutEvents = proto.sort; | ||
proto.sort = function(options) { | ||
var changed; | ||
changed = this.sortWithoutEvents(options); | ||
if (changed && !(options != null ? options.silent : void 0)) { | ||
this.trigger('change', this); | ||
} | ||
if (initializeWithoutCollection != null) { | ||
initializeWithoutCollection.call(this, models, options); | ||
return changed; | ||
}; | ||
} | ||
proto._addWithoutEvents = proto._add; | ||
proto._add = function(models, options) { | ||
var added; | ||
added = this._addWithoutEvents(models, options); | ||
this._emitAddChanges(added, options); | ||
return added; | ||
}; | ||
proto._deleteWithoutEvents = proto._delete; | ||
proto._delete = function(models, options) { | ||
var deleted; | ||
deleted = this._deleteWithoutEvents(models, options); | ||
this._emitDeleteChanges(deleted, options); | ||
return deleted; | ||
}; | ||
proto.clearWithoutEvents = proto._delete; | ||
proto.clear = function(options) { | ||
var deleted; | ||
deleted = this.clearWithoutEvents(options); | ||
this._emitDeleteChanges(deleted, options); | ||
return deleted; | ||
}; | ||
proto._emitAddChanges = function(added, options) { | ||
var model, _i, _j, _len, _len1; | ||
for (_i = 0, _len = added.length; _i < _len; _i++) { | ||
model = added[_i]; | ||
if (model.hasEvents) { | ||
model.on('change', this._forwardModelChangeEvent); | ||
} | ||
this._proxyModelEvent = _proxyModelEvent.bind(this); | ||
_ref = [[], 0, {}, {}], this.models = _ref[0], this.length = _ref[1], this.ids = _ref[2], this.cids = _ref[3]; | ||
this.comparator = options.comparator; | ||
if (models) { | ||
return this.add(models, options); | ||
} | ||
if ((added.length > 0) && !(options != null ? options.silent : void 0)) { | ||
for (_j = 0, _len1 = added.length; _j < _len1; _j++) { | ||
model = added[_j]; | ||
this.trigger('add', model, this); | ||
} | ||
}, | ||
sort: function(options) { | ||
if (options.comparator) { | ||
this.comparator = options.comparator; | ||
return this.trigger('change', this); | ||
} | ||
}; | ||
proto._emitDeleteChanges = function(deleted, options) { | ||
var model, _i, _j, _len, _len1; | ||
for (_i = 0, _len = deleted.length; _i < _len; _i++) { | ||
model = deleted[_i]; | ||
if (model.hasEvents) { | ||
model.off('change', this._forwardModelChangeEvent); | ||
} | ||
if (!this.comparator) { | ||
throw "no comparator!"; | ||
} | ||
if ((deleted.length > 0) && !(options != null ? options.silent : void 0)) { | ||
for (_j = 0, _len1 = deleted.length; _j < _len1; _j++) { | ||
model = deleted[_j]; | ||
this.trigger('delete', model, this); | ||
} | ||
if (this.comparator.length === 1) { | ||
this.models = _(this.models).sortBy(this.comparator); | ||
} else { | ||
this.models.sort(this.comparator); | ||
} | ||
if (this.isEventEmitter && options.silent !== true) { | ||
this.emit('change', this); | ||
} | ||
return this; | ||
}, | ||
add: function() { | ||
var added, args, lastArgument, model, models, options, tmp, _i, _j, _k, _l, _len, _len1, _len2, _len3; | ||
args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; | ||
if (_.isArray(args[0])) { | ||
models = args[0], options = args[1]; | ||
} else { | ||
lastArgument = args[args.length - 1]; | ||
options = !lastArgument.isModel ? args.pop() : {}; | ||
models = args; | ||
} | ||
if (options == null) { | ||
options = {}; | ||
} | ||
if (!(models.length > 0)) { | ||
return; | ||
} | ||
tmp = models; | ||
models = []; | ||
for (_i = 0, _len = tmp.length; _i < _len; _i++) { | ||
model = tmp[_i]; | ||
if (!model.isModel) { | ||
klass = this.model || (function() { | ||
throw "no Model class for Collection (" + this + ")!"; | ||
}).call(this); | ||
model = new klass(model); | ||
} | ||
models.push(model); | ||
} | ||
added = []; | ||
for (_j = 0, _len1 = models.length; _j < _len1; _j++) { | ||
model = models[_j]; | ||
if ((model.id in this.ids) || (model._cid in this.cids)) { | ||
continue; | ||
} | ||
this.ids[model.id] = model; | ||
this.cids[model._cid] = model; | ||
this.models.push(model); | ||
added.push(model); | ||
} | ||
this.length = this.models.length; | ||
if (this.isEventEmitter && model.isEventEmitter) { | ||
for (_k = 0, _len2 = added.length; _k < _len2; _k++) { | ||
model = added[_k]; | ||
model.addListener('change', this._proxyModelEvent); | ||
} | ||
} | ||
if (this.comparator) { | ||
this.sort({ | ||
silent: true | ||
}); | ||
} | ||
if (this.isEventEmitter && !options.silent && added.length > 0) { | ||
for (_l = 0, _len3 = added.length; _l < _len3; _l++) { | ||
model = added[_l]; | ||
this.emit('add', model, this); | ||
} | ||
this.emit('change', this); | ||
} | ||
return this; | ||
}, | ||
"delete": function() { | ||
var args, deleted, index, lastArgument, model, models, options, _i, _j, _k, _len, _len1, _len2; | ||
args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; | ||
if (_.isArray(args[0])) { | ||
models = args[0], options = args[1]; | ||
} else { | ||
lastArgument = args[args.length - 1]; | ||
options = !lastArgument.isModel ? args.pop() : {}; | ||
models = args; | ||
} | ||
if (options == null) { | ||
options = {}; | ||
} | ||
if (!(models.length > 0)) { | ||
return; | ||
} | ||
deleted = []; | ||
for (_i = 0, _len = models.length; _i < _len; _i++) { | ||
model = models[_i]; | ||
if (!((model.id in this.ids) || (model._cid in this.cids))) { | ||
continue; | ||
} | ||
index = this.models.indexOf(model); | ||
delete this.ids[model.id]; | ||
delete this.cids[model._cid]; | ||
this.models.splice(index, 1); | ||
deleted.push(model); | ||
} | ||
this.length = this.models.length; | ||
if (this.isEventEmitter && model.isEventEmitter) { | ||
for (_j = 0, _len1 = deleted.length; _j < _len1; _j++) { | ||
model = deleted[_j]; | ||
model.removeListener('change', this._proxyModelEvent); | ||
} | ||
} | ||
if (this.isEventEmitter && !options.silent && deleted.length > 0) { | ||
for (_k = 0, _len2 = deleted.length; _k < _len2; _k++) { | ||
model = deleted[_k]; | ||
this.emit('delete', model, this); | ||
} | ||
this.emit('change', this); | ||
} | ||
return this; | ||
}, | ||
get: function(id) { | ||
return this.ids[id] || this.cids[id]; | ||
}, | ||
has: function(id) { | ||
return (id in this.ids) || (id in this.cids); | ||
}, | ||
at: function(index) { | ||
return this.models[index]; | ||
}, | ||
clear: function(options) { | ||
var deleted, model, _i, _j, _len, _len1, _ref; | ||
if (options == null) { | ||
options = {}; | ||
} | ||
deleted = this.models; | ||
_ref = [[], 0, {}, {}], this.models = _ref[0], this.length = _ref[1], this.ids = _ref[2], this.cids = _ref[3]; | ||
if (this.isEventEmitter && model.isEventEmitter) { | ||
for (_i = 0, _len = deleted.length; _i < _len; _i++) { | ||
model = deleted[_i]; | ||
model.removeListener('change', this._proxyModelEvent); | ||
} | ||
} | ||
if (this.isEventEmitter && !options.silent && deleted.length > 0) { | ||
for (_j = 0, _len1 = deleted.length; _j < _len1; _j++) { | ||
model = deleted[_j]; | ||
this.emit('delete', model, this); | ||
} | ||
this.emit('change', this); | ||
} | ||
return this; | ||
}, | ||
reset: function() { | ||
var args; | ||
args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; | ||
this.clear(); | ||
return this.add.apply(this, args); | ||
}, | ||
inspect: function() { | ||
return JSON.stringify(this.models); | ||
}, | ||
toString: function() { | ||
return this.inspect(); | ||
}, | ||
toJSON: function() { | ||
return this.models; | ||
}, | ||
eql: function(other, strict) { | ||
var index, model, _i, _len, _ref; | ||
if (strict == null) { | ||
strict = false; | ||
} | ||
if (this === other) { | ||
return true; | ||
} | ||
if (!(other && other.length === this.length && other.models)) { | ||
return false; | ||
} | ||
if (strict) { | ||
if (!(other && this.constructor === other.constructor)) { | ||
return false; | ||
} | ||
} | ||
_ref = this.models; | ||
for (index = _i = 0, _len = _ref.length; _i < _len; index = ++_i) { | ||
model = _ref[index]; | ||
if (!model.eql(other.models[index], strict)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
}, | ||
equal: function(other) { | ||
return this.eql(other, true); | ||
return this.trigger('change', this); | ||
} | ||
}; | ||
proto.isCollection = true; | ||
_(proto).extend(methods); | ||
MicroModel.withUnderscoreCollection(klass); | ||
return MicroModel.withUnderscoreEqual(klass); | ||
return klass; | ||
}; | ||
MicroModel.Class = function() { | ||
var args, klass, methods, mixin, mixins, name, proto, _i, _len; | ||
args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; | ||
name = _(args[0]).isString() ? args.shift() : null; | ||
methods = !_(args[args.length - 1]).isFunction() ? args.pop() : {}; | ||
mixins = args; | ||
klass = function() { | ||
var _ref; | ||
return (_ref = this.initialize) != null ? _ref.apply(this, arguments) : void 0; | ||
}; | ||
exports.Events = function(klass, type) { | ||
var EventEmitter, initializeWithoutEventEmitter, proto; | ||
if (type == null) { | ||
type = 'EventEmitter'; | ||
} | ||
proto = klass.prototype; | ||
if (name) { | ||
klass.name = name; | ||
proto["is" + name] = true; | ||
proto.hasEvents = true; | ||
if (type === 'EventEmitter') { | ||
EventEmitter = requireEventEmitter(); | ||
_(proto).extend(EventEmitter.prototype); | ||
initializeWithoutEventEmitter = proto.initialize; | ||
proto.initialize = function() { | ||
EventEmitter.apply(this); | ||
return initializeWithoutEventEmitter.apply(this, arguments); | ||
}; | ||
proto.on = function() { | ||
return this.addListener.apply(this, arguments); | ||
}; | ||
proto.off = function() { | ||
return this.removeListener.apply(this, arguments); | ||
}; | ||
proto.trigger = function() { | ||
return this.emit.apply(this, arguments); | ||
}; | ||
} else if (type === 'Backbone.Events') { | ||
_(proto).extend(requireBackboneEvents()); | ||
} else { | ||
throw new Error("unknown type " + type); | ||
} | ||
for (_i = 0, _len = mixins.length; _i < _len; _i++) { | ||
mixin = mixins[_i]; | ||
mixin(klass); | ||
} | ||
_(proto).extend(methods); | ||
return klass; | ||
}; | ||
MicroModel.Model = MicroModel.Class('Model', MicroModel.withModel); | ||
exports.BaseClass = function() { | ||
return function() { | ||
var _ref; | ||
if ((_ref = this.initialize) != null) { | ||
_ref.apply(this, arguments); | ||
} | ||
return this; | ||
}; | ||
}; | ||
MicroModel.Collection = MicroModel.Class('Collection', MicroModel.withCollection); | ||
exports.BaseModel = exports.Model(); | ||
exports.BaseCollection = exports.Collection(); | ||
exports.FullModel = exports.Model.Events(exports.Model()); | ||
exports.FullCollection = exports.Collection.Events(exports.Collection.Sorted(exports.Collection())); | ||
}).call(this); |
{ | ||
"name" : "micromodel", | ||
"main" : "./micromodel", | ||
"version" : "0.1.3", | ||
"version" : "0.2.0", | ||
"homepage" : "http://alexeypetrushin.github.com/micromodel", | ||
@@ -14,3 +14,3 @@ "dependencies" : { | ||
"url" : "http://petrush.in" | ||
} , | ||
}, | ||
"license" : "MIT", | ||
@@ -17,0 +17,0 @@ "scripts" : { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
55412
16
540