Socket
Socket
Sign inDemoInstall

hamsa

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hamsa - npm Package Compare versions

Comparing version 0.11.1 to 0.12.0

coffeelint.json

2

bower.json
{
"name" : "hamsa",
"version" : "0.9.20",
"version" : "0.12.0",
"description" : "A dead simple, data-binding & observable model.",

@@ -5,0 +5,0 @@ "keywords" : ["object", "observe", "data-binding", "model"],

/**
* hamsa - A dead simple, data-binding & observable model.
* @version v0.11.1
* @version v0.12.0
* @link http://gethamsa.com

@@ -8,659 +8,2 @@ * @author Javi Jimenez Villar (http://soyjavi.com)

*/
/*
Basic Module
@namespace Hamsa
@class Module
@author Javier Jimenez Villar <javi.jimenez.villar@gmail.com> || @soyjavi
*/
(function() {
'use strict';
var DEFAULT_EVENTS, _cast, _constructorUpdate, _existObserver, _guid, _unobserve,
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; };
DEFAULT_EVENTS = ['add', 'update', 'delete'];
(function(exports) {
var Hamsa;
Hamsa = (function() {
/*
Set a array of fields used in the Class
@method fields
@param {string} Unknown arguments, each argument is the name of field.
*/
Hamsa.define = function(fields1) {
var field;
this.fields = fields1 != null ? fields1 : {};
this.events = [];
this.names = (function() {
var results;
results = [];
for (field in this.fields) {
results.push(field);
}
return results;
}).call(this);
this.observers = [];
this.records = {};
return this;
};
/*
Returns all instances of the Class
@method all
@return {array} Array of all repository instances.
*/
Hamsa.all = function() {
return this.find();
};
/*
Destroy all instances of the Class
@method destroyAll
@return {array} Empty array of all repository instances.
*/
Hamsa.destroyAll = function() {
var uid;
for (uid in this.records) {
delete this.records[uid];
}
return this.records;
};
/*
Returns instances of the defined Hamsa Class
@method find
@param {object} [OPTIONAL] Specifies selection criteria using query operators.
To return all instances, omit this parameter.
@return {array} Array of Hamsa instances
*/
Hamsa.find = function(query) {
var exists, field, record, records, ref, uid, value;
if (query == null) {
query = {};
}
records = [];
ref = this.records;
for (uid in ref) {
record = ref[uid];
exists = true;
for (field in query) {
value = query[field];
if (exists) {
if (_cast(record[field], this.fields[field]) !== value) {
exists = false;
}
}
}
if (exists) {
records.push(record);
}
}
return records;
};
/*
Returns one instance that satisfies the specified query criteria
@method findOne
@param {object} [OPTIONAL] Specifies selection criteria using query operators.
To return the first instance, omit this parameter.
@return {object} Hamsa instance.
*/
Hamsa.findOne = function(query) {
return this.find(query)[0];
};
/*
Modifies and returns a single instance
@method findAndModify
@param {object} Document parameter with the embedded document fields.
@return {object} Hamsa instance.
*/
Hamsa.findAndModify = function(document) {
var key, record, ref, value;
record = this.findOne(document.query);
if (record) {
ref = document.update;
for (key in ref) {
value = ref[key];
record[key] = value;
}
}
return record || new this(document.update);
};
/*
Observe changes in instance repository.
@method observe
@param {function} A function to execute each time the object is changed.
@return {array} Observers availables.
*/
Hamsa.observe = function(callback, events1) {
this.events = events1 != null ? events1 : DEFAULT_EVENTS;
Object.observe(this.records, (function(_this) {
return function(states) {
var constructor, event, j, len, ref, ref1, results, state;
if (_existObserver(_this.observers, callback)) {
results = [];
for (j = 0, len = states.length; j < len; j++) {
state = states[j];
constructor = ((ref = _this.records[state.name]) != null ? ref.constructor : void 0) || state.oldValue.constructor;
if (constructor === _this) {
event = {
type: state.type,
name: state.name
};
if ((ref1 = state.type) === 'add' || ref1 === 'update') {
event.object = _this.records[state.name];
} else {
event.oldValue = state.oldValue;
}
results.push(callback(event));
} else {
results.push(void 0);
}
}
return results;
}
};
})(this), this.events);
this.observers.push(callback);
return this.observers;
};
/*
Unobserve changes in instance repository.
@method unobserve
@return {array} Observers availables.
*/
Hamsa.unobserve = function(callback) {
return this.observers = _unobserve(this, callback);
};
/*
Create a nre instance for a Hamsa Class.
@method constructor
@param {object} Fields for the instance.
@param {function} A function to execute each time the fields change.
@return {object} Hamsa instance.
*/
function Hamsa(fields, callback, events) {
var define, field, ref, ref1;
if (fields == null) {
fields = {};
}
if (events == null) {
events = DEFAULT_EVENTS;
}
this.constructor.className = this.constructor.name;
this.constructor.records[this.uid = _guid()] = this;
ref = this.constructor.fields;
for (field in ref) {
define = ref[field];
if (fields[field] || (define["default"] != null)) {
if (typeof this[field] === 'function') {
this[field](fields[field] || define["default"]);
} else {
this[field] = _cast(fields[field], define);
}
}
}
this.observers = [];
if (callback != null) {
this.observe(callback, events);
this.observers.push(callback);
} else if (!callback && indexOf.call((ref1 = this.constructor) != null ? ref1.events : void 0, 'update') >= 0) {
Object.observe(this, (function(_this) {
return function(states) {
var j, len, ref2, results, state;
results = [];
for (j = 0, len = states.length; j < len; j++) {
state = states[j];
if (state.object.constructor === _this.constructor) {
if (ref2 = state.name, indexOf.call(_this.constructor.names, ref2) >= 0) {
results.push(_constructorUpdate(state, _this.constructor));
} else {
results.push(void 0);
}
}
}
return results;
};
})(this), ['update']);
}
this;
}
/*
Observe changes in a determinate Hamsa instance.
@method observe
@param {function} A function to execute each time the fields change.
@return {array} Observers availables for the instance.
*/
Hamsa.prototype.observe = function(callback, events) {
if (events == null) {
events = DEFAULT_EVENTS;
}
Object.observe(this, (function(_this) {
return function(states) {
var j, len, ref, results, state;
if (_existObserver(_this.observers, callback)) {
results = [];
for (j = 0, len = states.length; j < len; j++) {
state = states[j];
if (!(ref = state.name, indexOf.call(_this.constructor.names, ref) >= 0)) {
continue;
}
delete state.object.observer;
_constructorUpdate(state, _this.constructor);
results.push(callback(state));
}
return results;
}
};
})(this), events);
this.observers.push(callback);
return this.observers;
};
/*
Unobserve changes in a determinate Hamsa instance.
@method unobserve
@return {array} Observers availables for the instance.
*/
Hamsa.prototype.unobserve = function(callback) {
return this.observers = _unobserve(this, callback);
};
/*
Destroy current Hamsa instance
@method destroy
@return {object} Current Hamsa instance
*/
Hamsa.prototype.destroy = function(trigger) {
var callback, j, len, ref;
if (trigger == null) {
trigger = true;
}
if (trigger) {
ref = this.observers;
for (j = 0, len = ref.length; j < len; j++) {
callback = ref[j];
callback({
type: 'destroy',
name: this.uid,
oldValue: this.fields()
});
}
}
return delete this.constructor.records[this.uid];
};
Hamsa.prototype.fields = function() {
var j, len, name, ref, value;
value = {};
ref = this.constructor.names;
for (j = 0, len = ref.length; j < len; j++) {
name = ref[j];
value[name] = this[name];
}
return value;
};
return Hamsa;
})();
if (typeof define === 'function' && define.amd) {
define(function() {
return Hamsa;
});
} else {
exports.Hamsa = Hamsa;
}
if ((typeof module !== "undefined" && module !== null) && (module.exports != null)) {
return module.exports = Hamsa;
}
})(this);
_cast = function(value, define) {
if (define == null) {
define = {
type: String
};
}
if (define.type !== Date && define.type !== Array) {
return define.type(value || define["default"]);
} else if (define.type === Array) {
return value || define["default"];
} else {
return value || define.type(define["default"]);
}
};
_constructorUpdate = function(state, className) {
var j, len, observer, ref, ref1, results;
ref = className.observers;
results = [];
for (j = 0, len = ref.length; j < len; j++) {
observer = ref[j];
if (state.type === 'update' && (ref1 = state.type, indexOf.call(className.events, ref1) >= 0)) {
delete state.object.observer;
results.push(observer(state));
} else {
results.push(void 0);
}
}
return results;
};
_existObserver = function(observers, callback) {
var exists, j, len, observer;
exists = false;
for (j = 0, len = observers.length; j < len; j++) {
observer = observers[j];
if (!(observer === callback)) {
continue;
}
exists = true;
break;
}
return exists;
};
_guid = function() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r, v;
r = Math.random() * 16 | 0;
v = c === 'x' ? r : r & 3 | 8;
return v.toString(16);
}).toUpperCase();
};
_unobserve = function(instance, callback) {
var index, j, len, observe, ref;
ref = instance.observers;
for (index = j = 0, len = ref.length; j < len; index = ++j) {
observe = ref[index];
if (callback) {
if (observe === callback) {
Object.unobserve(instance, observe);
instance.observers.splice(index, 1);
break;
}
} else {
Object.unobserve(instance, observe);
}
}
if (!callback) {
instance.observers = [];
}
return instance.observers;
};
/*
Easy way for extends Javascript 'Classes'
@namespace window
@class Extends
@author Javier Jimenez Villar <javi.jimenez.villar@gmail.com> || @soyjavi
*/
"use strict";
window._extends = function(child, parent) {
var ctor, key;
ctor = function() {
this.constructor = child;
};
for (key in parent) {
if (_hasProp.call(parent, key)) {
child[key] = parent[key];
}
}
ctor.prototype = parent.prototype;
child.prototype = new ctor;
child.__super__ = parent.prototype;
return child;
};
window._hasProp = {}.hasOwnProperty;
/*
Polyfill for navigator who don't support Object.observe (ES7)
@namespace Hamsa.Polyfill
@class Polyfill
@author Javier Jimenez Villar <javi.jimenez.villar@gmail.com> || @soyjavi
*/
"use strict";
Object.observe || (function(O, A, root) {
var addChangeRecord, deliverHandlerRecords, handlers, inArray, nextFrame, observe, observed, performPropertyChecks, runGlobalLoop, setHandler;
DEFAULT_EVENTS = ['add', 'update', 'delete'];
inArray = A.indexOf || (function(array, pivot, start) {
return A.prototype.indexOf.call(array, pivot, start);
});
nextFrame = root.requestAnimationFrame || root.webkitRequestAnimationFrame || (function() {
var initial, last;
initial = +(new Date);
last = initial;
return function(func) {
var now;
now = +(new Date);
return setTimeout((function() {
return func((last = +(new Date)) - initial);
}), 17);
};
})();
observe = function(object, handler, events) {
var data, key, properties, value;
data = observed.get(object);
if (data) {
return setHandler(object, data, handler, events);
} else {
properties = Object.getOwnPropertyNames(object);
data = {
handlers: new Map,
properties: properties,
values: (function() {
var results;
results = [];
for (key in object) {
value = object[key];
if (indexOf.call(properties, key) >= 0) {
results.push(value);
}
}
return results;
})()
};
observed.set(object, data);
setHandler(object, data, handler, events);
if (observed.size === 1) {
return nextFrame(runGlobalLoop);
}
}
};
setHandler = function(object, data, handler, events) {
var handler_data;
handler_data = handlers.get(handler);
if (!handler_data) {
handlers.set(handler, handler_data = {
observed: new Map,
changeRecords: []
});
}
handler_data.observed.set(object, {
events: events,
data: data
});
return data.handlers.set(handler, handler_data);
};
performPropertyChecks = function(data, object, except) {
var i, index, j, key, keys, len, old_value, properties, properties_length, value, values;
if (!data.handlers.size) {
return;
}
values = data.values;
keys = Object.getOwnPropertyNames(object);
properties = data.properties.slice();
properties_length = properties.length;
for (j = 0, len = keys.length; j < len; j++) {
key = keys[j];
index = inArray(properties, key);
value = object[key];
if (index === -1) {
addChangeRecord(object, data, {
name: key,
type: 'add',
object: object
}, except);
data.properties.push(key);
values.push(value);
} else {
old_value = values[index];
properties[index] = null;
properties_length--;
if ((old_value === value ? old_value === 0 && 1 / old_value !== 1 / value : old_value === old_value || value === value)) {
addChangeRecord(object, data, {
name: key,
type: 'update',
object: object,
oldValue: old_value
}, except);
data.values[index] = value;
}
}
}
i = properties.length;
while (properties_length && i--) {
if (properties[i] !== null) {
addChangeRecord(object, data, {
name: properties[i],
type: 'delete',
object: object,
oldValue: values[i]
}, except);
data.properties.splice(i, 1);
data.values.splice(i, 1);
properties_length--;
}
}
};
addChangeRecord = function(object, data, changeRecord, except) {
data.handlers.forEach(function(handler_data) {
var events;
events = handler_data.observed.get(object).events;
if ((typeof except !== 'string' || inArray(events, except) === -1) && inArray(events, changeRecord.type) > -1) {
handler_data.changeRecords.push(changeRecord);
}
});
};
runGlobalLoop = function() {
if (observed.size) {
observed.forEach(performPropertyChecks);
handlers.forEach(deliverHandlerRecords);
nextFrame(runGlobalLoop);
}
};
deliverHandlerRecords = function(handler_data, handler) {
if (handler_data.changeRecords.length) {
handler(handler_data.changeRecords);
handler_data.changeRecords = [];
}
};
observed = new Map;
handlers = new Map;
/*
@function Object.observe
@see http://arv.github.io/ecmascript-object-observe/#Object.observe
*/
O.observe = function(object, handler, events) {
if (events == null) {
events = DEFAULT_EVENTS;
}
if (!object || typeof object !== 'object' && typeof object !== 'function') {
throw new TypeError('Object.observe cannot observe non-object');
}
if (typeof handler !== 'function') {
throw new TypeError('Object.observe cannot deliver to non-function');
}
if (O.isFrozen && O.isFrozen(handler)) {
throw new TypeError('Object.observe cannot deliver to a frozen function object');
}
if (arguments.length > 2 && typeof events !== 'object') {
throw new TypeError('Object.observe cannot use non-object accept list');
}
observe(object, handler, events);
return object;
};
/*
@function Object.unobserve
@see http://arv.github.io/ecmascript-object-observe/#Object.unobserve
*/
return O.unobserve = function(object, handler) {
var handler_data, odata;
if (object === null || typeof object !== 'object' && typeof object !== 'function') {
throw new TypeError('Object.unobserve cannot unobserve non-object');
}
if (typeof handler !== 'function') {
throw new TypeError('Object.unobserve cannot deliver to non-function');
}
handler_data = handlers.get(handler);
odata = void 0;
if (handler_data && (odata = handler_data.observed.get(object))) {
handler_data.observed.forEach(function(odata, object) {
performPropertyChecks(odata.data, object);
});
nextFrame(function() {
deliverHandlerRecords(handler_data, handler);
});
if (handler_data.observed.size === 1 && handler_data.observed.has(object)) {
handlers['delete'](handler);
} else {
handler_data.observed['delete'](object);
}
if (odata.data.handlers.size === 1) {
observed['delete'](object);
} else {
odata.data.handlers['delete'](handler);
}
}
return object;
};
})(Object, Array, this);
}).call(this);
(function(){"use strict";var e,t,r,n,o,s,i,u=[].indexOf||function(e){for(var t=0,r=this.length;r>t;t++)if(t in this&&this[t]===e)return t;return-1};e=["add","update","delete"],function(c){var a;return a=function(){function c(n,s,i){var c,a,l,f;null==n&&(n={}),null==i&&(i=e),this.constructor.className=this.constructor.name,this.constructor.records[this.uid=o()]=this,l=this.constructor.fields;for(a in l)c=l[a],(n[a]||null!=c["default"])&&("function"==typeof this[a]?this[a](n[a]||c["default"]):this[a]=t(n[a],c));this.observers=[],null!=s?(this.observe(s,i),this.observers.push(s)):!s&&u.call(null!=(f=this.constructor)?f.events:void 0,"update")>=0&&Object.observe(this,function(e){return function(t){var n,o,s,i,c;for(i=[],n=0,o=t.length;o>n;n++)c=t[n],c.object.constructor===e.constructor&&(s=c.name,u.call(e.constructor.names,s)>=0?i.push(r(c,e.constructor)):i.push(void 0));return i}}(this),["update"])}return c.define=function(e){var t;return this.fields=null!=e?e:{},this.events=[],this.names=function(){var e;e=[];for(t in this.fields)e.push(t);return e}.call(this),this.observers=[],this.records={},this},c.all=function(){return this.find()},c.destroyAll=function(){var e;for(e in this.records)delete this.records[e];return this.records},c.find=function(e){var r,n,o,i,u,c,a,l;null==e&&(e={}),console.warn("@find",e),i=[],u=this.records;for(a in u){o=u[a],r=!0,c=e.query;for(n in c)l=c[n],r&&t(o[n],this.fields[n])!==l&&(r=!1);r&&i.push(o)}return null!=e.sort&&(n=Object.keys(e.sort)[0],i=s(i,n,e.sort[n])),null!=e.limit?i.slice(0,e.limit):i},c.findOne=function(e){return this.find({query:e,amount:1})},c.findAndModify=function(e){var t,r,n,o;if(null==e&&(e={}),r=this.findOne(e.query)){n=e.update;for(t in n)o=n[t],r[t]=o}return r||new this(e.update)},c.observe=function(t,r){return this.events=null!=r?r:e,Object.observe(this.records,function(e){return function(r){var o,s,i,u,c,a,l,f;if(n(e.observers,t)){for(l=[],i=0,u=r.length;u>i;i++)f=r[i],o=(null!=(c=e.records[f.name])?c.constructor:void 0)||f.oldValue.constructor,o===e?(s={type:f.type,name:f.name},"add"===(a=f.type)||"update"===a?s.object=e.records[f.name]:s.oldValue=f.oldValue,l.push(t(s))):l.push(void 0);return l}}}(this),this.events),this.observers.push(t),this.observers},c.unobserve=function(e){return this.observers=i(this,e)},c.prototype.observe=function(t,o){return null==o&&(o=e),Object.observe(this,function(e){return function(o){var s,i,c,a,l;if(n(e.observers,t)){for(a=[],s=0,i=o.length;i>s;s++)l=o[s],c=l.name,u.call(e.constructor.names,c)>=0&&(delete l.object.observer,r(l,e.constructor),a.push(t(l)));return a}}}(this),o),this.observers.push(t),this.observers},c.prototype.unobserve=function(e){return this.observers=i(this,e)},c.prototype.destroy=function(e){var t,r,n,o;if(null==e&&(e=!0),e)for(o=this.observers,r=0,n=o.length;n>r;r++)(t=o[r])({type:"destroy",name:this.uid,oldValue:this.fields()});return delete this.constructor.records[this.uid]},c.prototype.fields=function(){var e,t,r,n,o;for(o={},n=this.constructor.names,e=0,t=n.length;t>e;e++)r=n[e],o[r]=this[r];return o},c}(),"function"==typeof define&&define.amd?define(function(){return a}):c.Hamsa=a,"undefined"!=typeof module&&null!==module&&null!=module.exports?module.exports=a:void 0}(this),t=function(e,t){return null==t&&(t={type:String}),t.type!==Date&&t.type!==Array?t.type(e||t["default"]):t.type===Array?e||t["default"]:e||t.type(t["default"])},r=function(e,t){var r,n,o,s,i,c;for(s=t.observers,c=[],r=0,n=s.length;n>r;r++)o=s[r],"update"===e.type&&(i=e.type,u.call(t.events,i)>=0)?(delete e.object.observer,c.push(o(e))):c.push(void 0);return c},n=function(e,t){var r,n,o,s;for(r=!1,n=0,o=e.length;o>n;n++)if(s=e[n],s===t){r=!0;break}return r},o=function(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(e){var t,r;return t=16*Math.random()|0,r="x"===e?t:3&t|8,r.toString(16)}).toUpperCase()},s=function(e,t,r){return e.sort(function(e,n){return e[t]>n[t]?-1*r:e[t]<n[t]?1*r:0})},i=function(e,t){var r,n,o,s,i;for(i=e.observers,r=n=0,o=i.length;o>n;r=++n)if(s=i[r],t){if(s===t){Object.unobserve(e,s),e.observers.splice(r,1);break}}else Object.unobserve(e,s);return t||(e.observers=[]),e.observers},window._extends=function(e,t){var r,n;r=function(){this.constructor=e};for(n in t)_hasProp.call(t,n)&&(e[n]=t[n]);return r.prototype=t.prototype,e.prototype=new r,e.__super__=t.prototype,e},window._hasProp={}.hasOwnProperty,Object.observe||function(t,r,n){var o,s,i,c,a,l,f,d,h,p;return e=["add","update","delete"],c=r.indexOf||function(e,t,n){return r.prototype.indexOf.call(e,t,n)},a=n.requestAnimationFrame||n.webkitRequestAnimationFrame||function(){var e,t;return e=+new Date,t=e,function(r){var n;return n=+new Date,setTimeout(function(){return r((t=+new Date)-e)},17)}}(),l=function(e,t,r){var n,o,s,i;return n=f.get(e),n?p(e,n,t,r):(s=Object.getOwnPropertyNames(e),n={handlers:new Map,properties:s,values:function(){var t;t=[];for(o in e)i=e[o],u.call(s,o)>=0&&t.push(i);return t}()},f.set(e,n),p(e,n,t,r),1===f.size?a(h):void 0)},p=function(e,t,r,n){var o;return o=i.get(r),o||i.set(r,o={observed:new Map,changeRecords:[]}),o.observed.set(e,{events:n,data:t}),t.handlers.set(r,o)},d=function(e,t,r){var n,s,i,u,a,l,f,d,h,p,v;if(e.handlers.size){for(v=e.values,a=Object.getOwnPropertyNames(t),d=e.properties.slice(),h=d.length,i=0,l=a.length;l>i;i++)u=a[i],s=c(d,u),p=t[u],-1===s?(o(t,e,{name:u,type:"add",object:t},r),e.properties.push(u),v.push(p)):(f=v[s],d[s]=null,h--,(f===p?0===f&&1/f!==1/p:f===f||p===p)&&(o(t,e,{name:u,type:"update",object:t,oldValue:f},r),e.values[s]=p));for(n=d.length;h&&n--;)null!==d[n]&&(o(t,e,{name:d[n],type:"delete",object:t,oldValue:v[n]},r),e.properties.splice(n,1),e.values.splice(n,1),h--)}},o=function(e,t,r,n){t.handlers.forEach(function(t){var o;o=t.observed.get(e).events,("string"!=typeof n||-1===c(o,n))&&c(o,r.type)>-1&&t.changeRecords.push(r)})},h=function(){f.size&&(f.forEach(d),i.forEach(s),a(h))},s=function(e,t){e.changeRecords.length&&(t(e.changeRecords),e.changeRecords=[])},f=new Map,i=new Map,t.observe=function(r,n,o){if(null==o&&(o=e),!r||"object"!=typeof r&&"function"!=typeof r)throw new TypeError("Object.observe cannot observe non-object");if("function"!=typeof n)throw new TypeError("Object.observe cannot deliver to non-function");if(t.isFrozen&&t.isFrozen(n))throw new TypeError("Object.observe cannot deliver to a frozen function object");if(arguments.length>2&&"object"!=typeof o)throw new TypeError("Object.observe cannot use non-object accept list");return l(r,n,o),r},t.unobserve=function(e,t){var r,n;if(null===e||"object"!=typeof e&&"function"!=typeof e)throw new TypeError("Object.unobserve cannot unobserve non-object");if("function"!=typeof t)throw new TypeError("Object.unobserve cannot deliver to non-function");return r=i.get(t),n=void 0,r&&(n=r.observed.get(e))&&(r.observed.forEach(function(e,t){d(e.data,t)}),a(function(){s(r,t)}),1===r.observed.size&&r.observed.has(e)?i["delete"](t):r.observed["delete"](e),1===n.data.handlers.size?f["delete"](e):n.data.handlers["delete"](t)),e}}(Object,Array,this)}).call(this);
/**
* hamsa - A dead simple, data-binding & observable model.
* @version v0.11.1
* @version v0.12.0
* @link http://gethamsa.com

@@ -8,659 +8,2 @@ * @author Javi Jimenez Villar (http://soyjavi.com)

*/
/*
Basic Module
@namespace Hamsa
@class Module
@author Javier Jimenez Villar <javi.jimenez.villar@gmail.com> || @soyjavi
*/
(function() {
'use strict';
var DEFAULT_EVENTS, _cast, _constructorUpdate, _existObserver, _guid, _unobserve,
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; };
DEFAULT_EVENTS = ['add', 'update', 'delete'];
(function(exports) {
var Hamsa;
Hamsa = (function() {
/*
Set a array of fields used in the Class
@method fields
@param {string} Unknown arguments, each argument is the name of field.
*/
Hamsa.define = function(fields1) {
var field;
this.fields = fields1 != null ? fields1 : {};
this.events = [];
this.names = (function() {
var results;
results = [];
for (field in this.fields) {
results.push(field);
}
return results;
}).call(this);
this.observers = [];
this.records = {};
return this;
};
/*
Returns all instances of the Class
@method all
@return {array} Array of all repository instances.
*/
Hamsa.all = function() {
return this.find();
};
/*
Destroy all instances of the Class
@method destroyAll
@return {array} Empty array of all repository instances.
*/
Hamsa.destroyAll = function() {
var uid;
for (uid in this.records) {
delete this.records[uid];
}
return this.records;
};
/*
Returns instances of the defined Hamsa Class
@method find
@param {object} [OPTIONAL] Specifies selection criteria using query operators.
To return all instances, omit this parameter.
@return {array} Array of Hamsa instances
*/
Hamsa.find = function(query) {
var exists, field, record, records, ref, uid, value;
if (query == null) {
query = {};
}
records = [];
ref = this.records;
for (uid in ref) {
record = ref[uid];
exists = true;
for (field in query) {
value = query[field];
if (exists) {
if (_cast(record[field], this.fields[field]) !== value) {
exists = false;
}
}
}
if (exists) {
records.push(record);
}
}
return records;
};
/*
Returns one instance that satisfies the specified query criteria
@method findOne
@param {object} [OPTIONAL] Specifies selection criteria using query operators.
To return the first instance, omit this parameter.
@return {object} Hamsa instance.
*/
Hamsa.findOne = function(query) {
return this.find(query)[0];
};
/*
Modifies and returns a single instance
@method findAndModify
@param {object} Document parameter with the embedded document fields.
@return {object} Hamsa instance.
*/
Hamsa.findAndModify = function(document) {
var key, record, ref, value;
record = this.findOne(document.query);
if (record) {
ref = document.update;
for (key in ref) {
value = ref[key];
record[key] = value;
}
}
return record || new this(document.update);
};
/*
Observe changes in instance repository.
@method observe
@param {function} A function to execute each time the object is changed.
@return {array} Observers availables.
*/
Hamsa.observe = function(callback, events1) {
this.events = events1 != null ? events1 : DEFAULT_EVENTS;
Object.observe(this.records, (function(_this) {
return function(states) {
var constructor, event, j, len, ref, ref1, results, state;
if (_existObserver(_this.observers, callback)) {
results = [];
for (j = 0, len = states.length; j < len; j++) {
state = states[j];
constructor = ((ref = _this.records[state.name]) != null ? ref.constructor : void 0) || state.oldValue.constructor;
if (constructor === _this) {
event = {
type: state.type,
name: state.name
};
if ((ref1 = state.type) === 'add' || ref1 === 'update') {
event.object = _this.records[state.name];
} else {
event.oldValue = state.oldValue;
}
results.push(callback(event));
} else {
results.push(void 0);
}
}
return results;
}
};
})(this), this.events);
this.observers.push(callback);
return this.observers;
};
/*
Unobserve changes in instance repository.
@method unobserve
@return {array} Observers availables.
*/
Hamsa.unobserve = function(callback) {
return this.observers = _unobserve(this, callback);
};
/*
Create a nre instance for a Hamsa Class.
@method constructor
@param {object} Fields for the instance.
@param {function} A function to execute each time the fields change.
@return {object} Hamsa instance.
*/
function Hamsa(fields, callback, events) {
var define, field, ref, ref1;
if (fields == null) {
fields = {};
}
if (events == null) {
events = DEFAULT_EVENTS;
}
this.constructor.className = this.constructor.name;
this.constructor.records[this.uid = _guid()] = this;
ref = this.constructor.fields;
for (field in ref) {
define = ref[field];
if (fields[field] || (define["default"] != null)) {
if (typeof this[field] === 'function') {
this[field](fields[field] || define["default"]);
} else {
this[field] = _cast(fields[field], define);
}
}
}
this.observers = [];
if (callback != null) {
this.observe(callback, events);
this.observers.push(callback);
} else if (!callback && indexOf.call((ref1 = this.constructor) != null ? ref1.events : void 0, 'update') >= 0) {
Object.observe(this, (function(_this) {
return function(states) {
var j, len, ref2, results, state;
results = [];
for (j = 0, len = states.length; j < len; j++) {
state = states[j];
if (state.object.constructor === _this.constructor) {
if (ref2 = state.name, indexOf.call(_this.constructor.names, ref2) >= 0) {
results.push(_constructorUpdate(state, _this.constructor));
} else {
results.push(void 0);
}
}
}
return results;
};
})(this), ['update']);
}
this;
}
/*
Observe changes in a determinate Hamsa instance.
@method observe
@param {function} A function to execute each time the fields change.
@return {array} Observers availables for the instance.
*/
Hamsa.prototype.observe = function(callback, events) {
if (events == null) {
events = DEFAULT_EVENTS;
}
Object.observe(this, (function(_this) {
return function(states) {
var j, len, ref, results, state;
if (_existObserver(_this.observers, callback)) {
results = [];
for (j = 0, len = states.length; j < len; j++) {
state = states[j];
if (!(ref = state.name, indexOf.call(_this.constructor.names, ref) >= 0)) {
continue;
}
delete state.object.observer;
_constructorUpdate(state, _this.constructor);
results.push(callback(state));
}
return results;
}
};
})(this), events);
this.observers.push(callback);
return this.observers;
};
/*
Unobserve changes in a determinate Hamsa instance.
@method unobserve
@return {array} Observers availables for the instance.
*/
Hamsa.prototype.unobserve = function(callback) {
return this.observers = _unobserve(this, callback);
};
/*
Destroy current Hamsa instance
@method destroy
@return {object} Current Hamsa instance
*/
Hamsa.prototype.destroy = function(trigger) {
var callback, j, len, ref;
if (trigger == null) {
trigger = true;
}
if (trigger) {
ref = this.observers;
for (j = 0, len = ref.length; j < len; j++) {
callback = ref[j];
callback({
type: 'destroy',
name: this.uid,
oldValue: this.fields()
});
}
}
return delete this.constructor.records[this.uid];
};
Hamsa.prototype.fields = function() {
var j, len, name, ref, value;
value = {};
ref = this.constructor.names;
for (j = 0, len = ref.length; j < len; j++) {
name = ref[j];
value[name] = this[name];
}
return value;
};
return Hamsa;
})();
if (typeof define === 'function' && define.amd) {
define(function() {
return Hamsa;
});
} else {
exports.Hamsa = Hamsa;
}
if ((typeof module !== "undefined" && module !== null) && (module.exports != null)) {
return module.exports = Hamsa;
}
})(this);
_cast = function(value, define) {
if (define == null) {
define = {
type: String
};
}
if (define.type !== Date && define.type !== Array) {
return define.type(value || define["default"]);
} else if (define.type === Array) {
return value || define["default"];
} else {
return value || define.type(define["default"]);
}
};
_constructorUpdate = function(state, className) {
var j, len, observer, ref, ref1, results;
ref = className.observers;
results = [];
for (j = 0, len = ref.length; j < len; j++) {
observer = ref[j];
if (state.type === 'update' && (ref1 = state.type, indexOf.call(className.events, ref1) >= 0)) {
delete state.object.observer;
results.push(observer(state));
} else {
results.push(void 0);
}
}
return results;
};
_existObserver = function(observers, callback) {
var exists, j, len, observer;
exists = false;
for (j = 0, len = observers.length; j < len; j++) {
observer = observers[j];
if (!(observer === callback)) {
continue;
}
exists = true;
break;
}
return exists;
};
_guid = function() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r, v;
r = Math.random() * 16 | 0;
v = c === 'x' ? r : r & 3 | 8;
return v.toString(16);
}).toUpperCase();
};
_unobserve = function(instance, callback) {
var index, j, len, observe, ref;
ref = instance.observers;
for (index = j = 0, len = ref.length; j < len; index = ++j) {
observe = ref[index];
if (callback) {
if (observe === callback) {
Object.unobserve(instance, observe);
instance.observers.splice(index, 1);
break;
}
} else {
Object.unobserve(instance, observe);
}
}
if (!callback) {
instance.observers = [];
}
return instance.observers;
};
/*
Easy way for extends Javascript 'Classes'
@namespace window
@class Extends
@author Javier Jimenez Villar <javi.jimenez.villar@gmail.com> || @soyjavi
*/
"use strict";
window._extends = function(child, parent) {
var ctor, key;
ctor = function() {
this.constructor = child;
};
for (key in parent) {
if (_hasProp.call(parent, key)) {
child[key] = parent[key];
}
}
ctor.prototype = parent.prototype;
child.prototype = new ctor;
child.__super__ = parent.prototype;
return child;
};
window._hasProp = {}.hasOwnProperty;
/*
Polyfill for navigator who don't support Object.observe (ES7)
@namespace Hamsa.Polyfill
@class Polyfill
@author Javier Jimenez Villar <javi.jimenez.villar@gmail.com> || @soyjavi
*/
"use strict";
Object.observe || (function(O, A, root) {
var addChangeRecord, deliverHandlerRecords, handlers, inArray, nextFrame, observe, observed, performPropertyChecks, runGlobalLoop, setHandler;
DEFAULT_EVENTS = ['add', 'update', 'delete'];
inArray = A.indexOf || (function(array, pivot, start) {
return A.prototype.indexOf.call(array, pivot, start);
});
nextFrame = root.requestAnimationFrame || root.webkitRequestAnimationFrame || (function() {
var initial, last;
initial = +(new Date);
last = initial;
return function(func) {
var now;
now = +(new Date);
return setTimeout((function() {
return func((last = +(new Date)) - initial);
}), 17);
};
})();
observe = function(object, handler, events) {
var data, key, properties, value;
data = observed.get(object);
if (data) {
return setHandler(object, data, handler, events);
} else {
properties = Object.getOwnPropertyNames(object);
data = {
handlers: new Map,
properties: properties,
values: (function() {
var results;
results = [];
for (key in object) {
value = object[key];
if (indexOf.call(properties, key) >= 0) {
results.push(value);
}
}
return results;
})()
};
observed.set(object, data);
setHandler(object, data, handler, events);
if (observed.size === 1) {
return nextFrame(runGlobalLoop);
}
}
};
setHandler = function(object, data, handler, events) {
var handler_data;
handler_data = handlers.get(handler);
if (!handler_data) {
handlers.set(handler, handler_data = {
observed: new Map,
changeRecords: []
});
}
handler_data.observed.set(object, {
events: events,
data: data
});
return data.handlers.set(handler, handler_data);
};
performPropertyChecks = function(data, object, except) {
var i, index, j, key, keys, len, old_value, properties, properties_length, value, values;
if (!data.handlers.size) {
return;
}
values = data.values;
keys = Object.getOwnPropertyNames(object);
properties = data.properties.slice();
properties_length = properties.length;
for (j = 0, len = keys.length; j < len; j++) {
key = keys[j];
index = inArray(properties, key);
value = object[key];
if (index === -1) {
addChangeRecord(object, data, {
name: key,
type: 'add',
object: object
}, except);
data.properties.push(key);
values.push(value);
} else {
old_value = values[index];
properties[index] = null;
properties_length--;
if ((old_value === value ? old_value === 0 && 1 / old_value !== 1 / value : old_value === old_value || value === value)) {
addChangeRecord(object, data, {
name: key,
type: 'update',
object: object,
oldValue: old_value
}, except);
data.values[index] = value;
}
}
}
i = properties.length;
while (properties_length && i--) {
if (properties[i] !== null) {
addChangeRecord(object, data, {
name: properties[i],
type: 'delete',
object: object,
oldValue: values[i]
}, except);
data.properties.splice(i, 1);
data.values.splice(i, 1);
properties_length--;
}
}
};
addChangeRecord = function(object, data, changeRecord, except) {
data.handlers.forEach(function(handler_data) {
var events;
events = handler_data.observed.get(object).events;
if ((typeof except !== 'string' || inArray(events, except) === -1) && inArray(events, changeRecord.type) > -1) {
handler_data.changeRecords.push(changeRecord);
}
});
};
runGlobalLoop = function() {
if (observed.size) {
observed.forEach(performPropertyChecks);
handlers.forEach(deliverHandlerRecords);
nextFrame(runGlobalLoop);
}
};
deliverHandlerRecords = function(handler_data, handler) {
if (handler_data.changeRecords.length) {
handler(handler_data.changeRecords);
handler_data.changeRecords = [];
}
};
observed = new Map;
handlers = new Map;
/*
@function Object.observe
@see http://arv.github.io/ecmascript-object-observe/#Object.observe
*/
O.observe = function(object, handler, events) {
if (events == null) {
events = DEFAULT_EVENTS;
}
if (!object || typeof object !== 'object' && typeof object !== 'function') {
throw new TypeError('Object.observe cannot observe non-object');
}
if (typeof handler !== 'function') {
throw new TypeError('Object.observe cannot deliver to non-function');
}
if (O.isFrozen && O.isFrozen(handler)) {
throw new TypeError('Object.observe cannot deliver to a frozen function object');
}
if (arguments.length > 2 && typeof events !== 'object') {
throw new TypeError('Object.observe cannot use non-object accept list');
}
observe(object, handler, events);
return object;
};
/*
@function Object.unobserve
@see http://arv.github.io/ecmascript-object-observe/#Object.unobserve
*/
return O.unobserve = function(object, handler) {
var handler_data, odata;
if (object === null || typeof object !== 'object' && typeof object !== 'function') {
throw new TypeError('Object.unobserve cannot unobserve non-object');
}
if (typeof handler !== 'function') {
throw new TypeError('Object.unobserve cannot deliver to non-function');
}
handler_data = handlers.get(handler);
odata = void 0;
if (handler_data && (odata = handler_data.observed.get(object))) {
handler_data.observed.forEach(function(odata, object) {
performPropertyChecks(odata.data, object);
});
nextFrame(function() {
deliverHandlerRecords(handler_data, handler);
});
if (handler_data.observed.size === 1 && handler_data.observed.has(object)) {
handlers['delete'](handler);
} else {
handler_data.observed['delete'](object);
}
if (odata.data.handlers.size === 1) {
observed['delete'](object);
} else {
odata.data.handlers['delete'](handler);
}
}
return object;
};
})(Object, Array, this);
}).call(this);
(function(){"use strict";var e,t,r,n,o,s,i,u=[].indexOf||function(e){for(var t=0,r=this.length;r>t;t++)if(t in this&&this[t]===e)return t;return-1};e=["add","update","delete"],function(c){var a;return a=function(){function c(n,s,i){var c,a,l,f;null==n&&(n={}),null==i&&(i=e),this.constructor.className=this.constructor.name,this.constructor.records[this.uid=o()]=this,l=this.constructor.fields;for(a in l)c=l[a],(n[a]||null!=c["default"])&&("function"==typeof this[a]?this[a](n[a]||c["default"]):this[a]=t(n[a],c));this.observers=[],null!=s?(this.observe(s,i),this.observers.push(s)):!s&&u.call(null!=(f=this.constructor)?f.events:void 0,"update")>=0&&Object.observe(this,function(e){return function(t){var n,o,s,i,c;for(i=[],n=0,o=t.length;o>n;n++)c=t[n],c.object.constructor===e.constructor&&(s=c.name,u.call(e.constructor.names,s)>=0?i.push(r(c,e.constructor)):i.push(void 0));return i}}(this),["update"])}return c.define=function(e){var t;return this.fields=null!=e?e:{},this.events=[],this.names=function(){var e;e=[];for(t in this.fields)e.push(t);return e}.call(this),this.observers=[],this.records={},this},c.all=function(){return this.find()},c.destroyAll=function(){var e;for(e in this.records)delete this.records[e];return this.records},c.find=function(e){var r,n,o,i,u,c,a,l;null==e&&(e={}),console.warn("@find",e),i=[],u=this.records;for(a in u){o=u[a],r=!0,c=e.query;for(n in c)l=c[n],r&&t(o[n],this.fields[n])!==l&&(r=!1);r&&i.push(o)}return null!=e.sort&&(n=Object.keys(e.sort)[0],i=s(i,n,e.sort[n])),null!=e.limit?i.slice(0,e.limit):i},c.findOne=function(e){return this.find({query:e,amount:1})},c.findAndModify=function(e){var t,r,n,o;if(null==e&&(e={}),r=this.findOne(e.query)){n=e.update;for(t in n)o=n[t],r[t]=o}return r||new this(e.update)},c.observe=function(t,r){return this.events=null!=r?r:e,Object.observe(this.records,function(e){return function(r){var o,s,i,u,c,a,l,f;if(n(e.observers,t)){for(l=[],i=0,u=r.length;u>i;i++)f=r[i],o=(null!=(c=e.records[f.name])?c.constructor:void 0)||f.oldValue.constructor,o===e?(s={type:f.type,name:f.name},"add"===(a=f.type)||"update"===a?s.object=e.records[f.name]:s.oldValue=f.oldValue,l.push(t(s))):l.push(void 0);return l}}}(this),this.events),this.observers.push(t),this.observers},c.unobserve=function(e){return this.observers=i(this,e)},c.prototype.observe=function(t,o){return null==o&&(o=e),Object.observe(this,function(e){return function(o){var s,i,c,a,l;if(n(e.observers,t)){for(a=[],s=0,i=o.length;i>s;s++)l=o[s],c=l.name,u.call(e.constructor.names,c)>=0&&(delete l.object.observer,r(l,e.constructor),a.push(t(l)));return a}}}(this),o),this.observers.push(t),this.observers},c.prototype.unobserve=function(e){return this.observers=i(this,e)},c.prototype.destroy=function(e){var t,r,n,o;if(null==e&&(e=!0),e)for(o=this.observers,r=0,n=o.length;n>r;r++)(t=o[r])({type:"destroy",name:this.uid,oldValue:this.fields()});return delete this.constructor.records[this.uid]},c.prototype.fields=function(){var e,t,r,n,o;for(o={},n=this.constructor.names,e=0,t=n.length;t>e;e++)r=n[e],o[r]=this[r];return o},c}(),"function"==typeof define&&define.amd?define(function(){return a}):c.Hamsa=a,"undefined"!=typeof module&&null!==module&&null!=module.exports?module.exports=a:void 0}(this),t=function(e,t){return null==t&&(t={type:String}),t.type!==Date&&t.type!==Array?t.type(e||t["default"]):t.type===Array?e||t["default"]:e||t.type(t["default"])},r=function(e,t){var r,n,o,s,i,c;for(s=t.observers,c=[],r=0,n=s.length;n>r;r++)o=s[r],"update"===e.type&&(i=e.type,u.call(t.events,i)>=0)?(delete e.object.observer,c.push(o(e))):c.push(void 0);return c},n=function(e,t){var r,n,o,s;for(r=!1,n=0,o=e.length;o>n;n++)if(s=e[n],s===t){r=!0;break}return r},o=function(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(e){var t,r;return t=16*Math.random()|0,r="x"===e?t:3&t|8,r.toString(16)}).toUpperCase()},s=function(e,t,r){return e.sort(function(e,n){return e[t]>n[t]?-1*r:e[t]<n[t]?1*r:0})},i=function(e,t){var r,n,o,s,i;for(i=e.observers,r=n=0,o=i.length;o>n;r=++n)if(s=i[r],t){if(s===t){Object.unobserve(e,s),e.observers.splice(r,1);break}}else Object.unobserve(e,s);return t||(e.observers=[]),e.observers},window._extends=function(e,t){var r,n;r=function(){this.constructor=e};for(n in t)_hasProp.call(t,n)&&(e[n]=t[n]);return r.prototype=t.prototype,e.prototype=new r,e.__super__=t.prototype,e},window._hasProp={}.hasOwnProperty,Object.observe||function(t,r,n){var o,s,i,c,a,l,f,d,h,p;return e=["add","update","delete"],c=r.indexOf||function(e,t,n){return r.prototype.indexOf.call(e,t,n)},a=n.requestAnimationFrame||n.webkitRequestAnimationFrame||function(){var e,t;return e=+new Date,t=e,function(r){var n;return n=+new Date,setTimeout(function(){return r((t=+new Date)-e)},17)}}(),l=function(e,t,r){var n,o,s,i;return n=f.get(e),n?p(e,n,t,r):(s=Object.getOwnPropertyNames(e),n={handlers:new Map,properties:s,values:function(){var t;t=[];for(o in e)i=e[o],u.call(s,o)>=0&&t.push(i);return t}()},f.set(e,n),p(e,n,t,r),1===f.size?a(h):void 0)},p=function(e,t,r,n){var o;return o=i.get(r),o||i.set(r,o={observed:new Map,changeRecords:[]}),o.observed.set(e,{events:n,data:t}),t.handlers.set(r,o)},d=function(e,t,r){var n,s,i,u,a,l,f,d,h,p,v;if(e.handlers.size){for(v=e.values,a=Object.getOwnPropertyNames(t),d=e.properties.slice(),h=d.length,i=0,l=a.length;l>i;i++)u=a[i],s=c(d,u),p=t[u],-1===s?(o(t,e,{name:u,type:"add",object:t},r),e.properties.push(u),v.push(p)):(f=v[s],d[s]=null,h--,(f===p?0===f&&1/f!==1/p:f===f||p===p)&&(o(t,e,{name:u,type:"update",object:t,oldValue:f},r),e.values[s]=p));for(n=d.length;h&&n--;)null!==d[n]&&(o(t,e,{name:d[n],type:"delete",object:t,oldValue:v[n]},r),e.properties.splice(n,1),e.values.splice(n,1),h--)}},o=function(e,t,r,n){t.handlers.forEach(function(t){var o;o=t.observed.get(e).events,("string"!=typeof n||-1===c(o,n))&&c(o,r.type)>-1&&t.changeRecords.push(r)})},h=function(){f.size&&(f.forEach(d),i.forEach(s),a(h))},s=function(e,t){e.changeRecords.length&&(t(e.changeRecords),e.changeRecords=[])},f=new Map,i=new Map,t.observe=function(r,n,o){if(null==o&&(o=e),!r||"object"!=typeof r&&"function"!=typeof r)throw new TypeError("Object.observe cannot observe non-object");if("function"!=typeof n)throw new TypeError("Object.observe cannot deliver to non-function");if(t.isFrozen&&t.isFrozen(n))throw new TypeError("Object.observe cannot deliver to a frozen function object");if(arguments.length>2&&"object"!=typeof o)throw new TypeError("Object.observe cannot use non-object accept list");return l(r,n,o),r},t.unobserve=function(e,t){var r,n;if(null===e||"object"!=typeof e&&"function"!=typeof e)throw new TypeError("Object.unobserve cannot unobserve non-object");if("function"!=typeof t)throw new TypeError("Object.unobserve cannot deliver to non-function");return r=i.get(t),n=void 0,r&&(n=r.observed.get(e))&&(r.observed.forEach(function(e,t){d(e.data,t)}),a(function(){s(r,t)}),1===r.observed.size&&r.observed.has(e)?i["delete"](t):r.observed["delete"](e),1===n.data.handlers.size?f["delete"](e):n.data.handlers["delete"](t)),e}}(Object,Array,this)}).call(this);
{
"name" : "hamsa",
"version" : "0.11.1",
"version" : "0.12.0",
"description" : "A dead simple, data-binding & observable model.",

@@ -5,0 +5,0 @@ "homepage" : "http://gethamsa.com",

hamsa
=====
A dead simple, data-binding & observable model.
A dead simple, data-binding & observable model with EcmaScript6.

@@ -165,7 +165,14 @@

####SELECT YOUR hamsa INSTANCES
Finally you will learn how to select instances of a particular Hamsa Class. To do this we will use the class method `find` which receives a filtering function and returns an array of instances that fulfill that function.
Finally you will learn how to find instances of a particular Hamsa Class. To do this we will use the class method <code>find</code> which receives a selection criteria using a object and returns an array of instances that fulfill that function. To return all documents in a collection, omit this parameter or pass an empty object.
+ **query**: *Optional*. The selection criteria for the modification.
+ **sort**: **Optional**. Specify in the sort parameter the field or fields to sort by and a value of `1` or `-1` to specify an ascending or descending sort respectively.
+ **limit**: *Optional*. To specify the maximum number of documents the cursor will return
+
```
Contact.find (instance) ->
if instance.since < 2014 and instance.username is "@javi"
Contact.find
query: since: 2014, username: '@javi'
sort: username: -1
amount: 3

@@ -177,11 +184,27 @@ ###

In case you want to filter for a particular field you can use the method of `findBy` class. Unlike with the `find` method in this case you must set two parameters, the first is the field you want to filter and the second one the exact search value.
In case you want to find a particular instance you can use the class method `findOne`:
Contact.findBy "username", "@javi"
```
Contact.findOne since: 2014
###
> {username: "@javi", ...}
###
```
In case you want to find and modify a single instance you can use the class method `findAndModify`. This method recibes a document parameter with the embedded document fields.
+ **query**: *Optional*. The selection criteria for the modification. The query field employs the same query selectors as used in the `find` method. Although the query may match multiple documents, `findAndModify` will only select one document to modify.
+ **update**: Must specify either the update field. Performs an update of the selected document.
+ **upsert**: *Optional*. When `true`, `findAndModify` creates a new instance if no document matches the query or if documents match the query.
```
Contact.findAndModify
query: since: 2014, username: '@javi'
update: username: '@soyjavi'
upsert: true
###
> [{username: "@javi", ...}]
> {username: '@soyjavi', ...}
###
```

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc