sqlite-orm
Advanced tools
@@ -174,3 +174,3 @@ var Mixin, ModelAssociation, ObserverArray, Q, _, | ||
| children = parent[as]; | ||
| return children.scopeUnobserve(function() { | ||
| return children._scopeUnobserve(function() { | ||
| var index; | ||
@@ -187,3 +187,3 @@ index = children.indexOf(child); | ||
| children = parent[as]; | ||
| return children.scopeUnobserve(function() { | ||
| return children._scopeUnobserve(function() { | ||
| return children.push(child); | ||
@@ -311,11 +311,17 @@ }); | ||
| ModelAssociation.extendBelongsTo = function(opts, handler) { | ||
| var key; | ||
| key = privateName(opts.as); | ||
| var as, getHook, key, ref, setHook; | ||
| as = opts.as; | ||
| key = privateName(as); | ||
| if (handler == null) { | ||
| handler = getHandlerForHasAssos(this, opts); | ||
| } | ||
| return Object.defineProperty(this.prototype, opts.as, { | ||
| ref = this._getHook(as), setHook = ref[0], getHook = ref[1]; | ||
| return Object.defineProperty(this.prototype, as, { | ||
| get: function() { | ||
| var ref; | ||
| return (ref = this[key]) != null ? ref : null; | ||
| var ref1, val; | ||
| val = (ref1 = this[key]) != null ? ref1 : null; | ||
| if (getHook) { | ||
| val = getHook(val); | ||
| } | ||
| return val; | ||
| }, | ||
@@ -325,10 +331,15 @@ set: function(val) { | ||
| origin = this[key]; | ||
| setBelongsTo(opts, this, val); | ||
| if (handler) { | ||
| if (origin) { | ||
| handler.remove(origin, this); | ||
| if (setHook) { | ||
| val = setHook(val); | ||
| } | ||
| if (val !== origin) { | ||
| setBelongsTo(opts, this, val != null ? val : null); | ||
| if (handler) { | ||
| if (origin) { | ||
| handler.remove(origin, this); | ||
| } | ||
| if (val) { | ||
| return handler.add(val, this); | ||
| } | ||
| } | ||
| if (val) { | ||
| return handler.add(val, this); | ||
| } | ||
| } | ||
@@ -393,3 +404,2 @@ } | ||
| })(this)); | ||
| val.observe(); | ||
| } | ||
@@ -460,9 +470,14 @@ return val; | ||
| ModelAssociation.extendHasOne = function(opts) { | ||
| var handler, key; | ||
| var getHook, handler, key, ref, setHook; | ||
| handler = getHandlerInBelongsAssos(this, opts); | ||
| key = privateName(opts.as); | ||
| ref = this._getHook(opts.as), setHook = ref[0], getHook = ref[1]; | ||
| return Object.defineProperty(this.prototype, opts.as, { | ||
| get: function() { | ||
| var ref; | ||
| return (ref = this[key]) != null ? ref : null; | ||
| var ref1, val; | ||
| val = (ref1 = this[key]) != null ? ref1 : null; | ||
| if (getHook) { | ||
| val = getHook(val); | ||
| } | ||
| return val; | ||
| }, | ||
@@ -472,8 +487,13 @@ set: function(val) { | ||
| origin = this[key]; | ||
| if (origin) { | ||
| handler.remove(origin, null); | ||
| if (setHook) { | ||
| val = setHook(val); | ||
| } | ||
| this[key] = val; | ||
| if (val) { | ||
| return handler.add(val, this); | ||
| if (val !== origin) { | ||
| if (origin) { | ||
| handler.remove(origin, null); | ||
| } | ||
| this[key] = val != null ? val : null; | ||
| if (val) { | ||
| return handler.add(val, this); | ||
| } | ||
| } | ||
@@ -552,3 +572,3 @@ } | ||
| } else { | ||
| return members.scopeUnobserve(function() { | ||
| return members._scopeUnobserve(function() { | ||
| return members.splice.apply(members, [0, 0].concat(slice.call(children))); | ||
@@ -606,3 +626,3 @@ }); | ||
| } else { | ||
| return members.scopeUnobserve(function() { | ||
| return members._scopeUnobserve(function() { | ||
| return members.splice.apply(members, [0, 0].concat(slice.call(targets))); | ||
@@ -609,0 +629,0 @@ }); |
+53
-14
@@ -14,3 +14,3 @@ var Mixin, ModelAssociation, ModelBaseMixin, Q, _, | ||
| module.exports = ModelBaseMixin = (function(superClass) { | ||
| var boolOperator, dateOperator, getHook, getIdFromWhere, getOperator; | ||
| var boolOperator, dateOperator, getIdFromWhere, getOperator; | ||
@@ -101,6 +101,9 @@ extend(ModelBaseMixin, superClass); | ||
| getHook = function(name) { | ||
| var hookName, hookObj; | ||
| ModelBaseMixin._getHook = function(name) { | ||
| var get, getFunc, getVal, hookName, hookObj, set, setFunc, setVal; | ||
| hookName = "$" + name + "Hook"; | ||
| hookObj = this[hookName]; | ||
| if (!hookObj) { | ||
| return []; | ||
| } | ||
| if (hookObj instanceof Function) { | ||
@@ -111,20 +114,51 @@ hookObj = { | ||
| } | ||
| return hookObj; | ||
| set = hookObj.set, setVal = hookObj.setVal, get = hookObj.get, getVal = hookObj.getVal; | ||
| if (set && setVal) { | ||
| setFunc = function(val) { | ||
| set(val); | ||
| return setVal(val); | ||
| }; | ||
| } else if (set) { | ||
| setFunc = function(val) { | ||
| set(val); | ||
| return val; | ||
| }; | ||
| } else if (setVal) { | ||
| set = function(val) { | ||
| return setVal(val); | ||
| }; | ||
| } | ||
| if (get && getVal) { | ||
| getFunc = function(val) { | ||
| get(val); | ||
| return getVal(val); | ||
| }; | ||
| } else if (get) { | ||
| getFunc = function(val) { | ||
| get(val); | ||
| return val; | ||
| }; | ||
| } else if (getVal) { | ||
| getFunc = function(val) { | ||
| return getVal(val); | ||
| }; | ||
| } | ||
| return [setFunc, getFunc]; | ||
| }; | ||
| ModelBaseMixin.defineAttr = function(name, opts) { | ||
| var defaultVal, hookFunc, key, operator, ref; | ||
| var defaultVal, getHook, key, operator, ref, ref1, setHook; | ||
| key = '_' + name; | ||
| defaultVal = (ref = opts["default"]) != null ? ref : null; | ||
| operator = getOperator(opts); | ||
| hookFunc = getHook.call(this, name); | ||
| ref1 = this._getHook(name), setHook = ref1[0], getHook = ref1[1]; | ||
| return Object.defineProperty(this.prototype, name, { | ||
| get: function() { | ||
| var ref1, val; | ||
| val = (ref1 = this[key]) != null ? ref1 : defaultVal; | ||
| var ref2, val; | ||
| val = (ref2 = this[key]) != null ? ref2 : defaultVal; | ||
| if (operator) { | ||
| val = operator.get(val); | ||
| } | ||
| if ((hookFunc != null) && (hookFunc.get != null)) { | ||
| val = hookFunc.get(val) || val; | ||
| if (getHook) { | ||
| val = getHook(val); | ||
| } | ||
@@ -134,10 +168,15 @@ return val; | ||
| set: function(val) { | ||
| if (val == null) { | ||
| val = null; | ||
| } | ||
| if (setHook) { | ||
| val = setHook(val); | ||
| } | ||
| if (operator) { | ||
| val = operator.set(val); | ||
| } | ||
| if ((hookFunc != null) && (hookFunc.set != null)) { | ||
| val = hookFunc.set(val) || val; | ||
| if (this[key] !== val) { | ||
| this[key] = val; | ||
| return this.changeFields[name] = val; | ||
| } | ||
| this[key] = val; | ||
| return this.changeFields[name] = val; | ||
| } | ||
@@ -144,0 +183,0 @@ }); |
+38
-22
@@ -8,14 +8,7 @@ var ObserverArray, | ||
| this.list = []; | ||
| this.inObserve = false; | ||
| this.inObserve = true; | ||
| this.observers = []; | ||
| } | ||
| ObserverArray.prototype.observe = function() { | ||
| return this.inObserve = true; | ||
| }; | ||
| ObserverArray.prototype.unobserve = function() { | ||
| return this.inObserve = false; | ||
| }; | ||
| ObserverArray.prototype.scopeUnobserve = function(callback) { | ||
| ObserverArray.prototype._scopeUnobserve = function(callback) { | ||
| var origin; | ||
@@ -28,15 +21,31 @@ origin = this.inObserve; | ||
| ObserverArray.prototype.observe = function(func) { | ||
| return this.observers.push(func); | ||
| }; | ||
| ObserverArray.prototype.unobserve = function(func) { | ||
| return this.observers.splice(this.observers.indexOf(func), 1); | ||
| }; | ||
| ObserverArray.prototype.set = function(index, value) { | ||
| var list, oldValue; | ||
| var change, i, len, list, obs, oldValue, ref, results; | ||
| list = this.list; | ||
| oldValue = list[index]; | ||
| list[index] = value; | ||
| if (this.inObserve) { | ||
| return this.observeFunc([ | ||
| { | ||
| name: index, | ||
| type: 'update', | ||
| oldValue: oldValue | ||
| } | ||
| ]); | ||
| if (this.inObserve || this.observers.length > 0) { | ||
| change = { | ||
| name: index, | ||
| type: 'update', | ||
| oldValue: oldValue | ||
| }; | ||
| if (this.inObserve) { | ||
| this.observeFunc([change]); | ||
| } | ||
| ref = this.observers; | ||
| results = []; | ||
| for (i = 0, len = ref.length; i < len; i++) { | ||
| obs = ref[i]; | ||
| results.push(obs(change)); | ||
| } | ||
| return results; | ||
| } | ||
@@ -50,6 +59,6 @@ }; | ||
| ObserverArray.prototype.splice = function() { | ||
| var change, insertItems, ref, removeCount, removed, start; | ||
| var change, i, insertItems, len, obs, ref, ref1, removeCount, removed, start; | ||
| start = arguments[0], removeCount = arguments[1], insertItems = 3 <= arguments.length ? slice.call(arguments, 2) : []; | ||
| removed = (ref = this.list).splice.apply(ref, [start, removeCount].concat(slice.call(insertItems))); | ||
| if (this.inObserve) { | ||
| if (this.inObserve || this.observers.length > 0) { | ||
| change = { | ||
@@ -61,3 +70,10 @@ type: 'splice', | ||
| }; | ||
| this.observeFunc([change]); | ||
| if (this.inObserve) { | ||
| this.observeFunc([change]); | ||
| } | ||
| ref1 = this.observers; | ||
| for (i = 0, len = ref1.length; i < len; i++) { | ||
| obs = ref1[i]; | ||
| obs(change); | ||
| } | ||
| } | ||
@@ -64,0 +80,0 @@ return removed; |
+1
-1
| { | ||
| "name": "sqlite-orm", | ||
| "description": "the ORM framework for sqlite", | ||
| "version": "0.1.23", | ||
| "version": "0.1.24", | ||
| "homepage": "https://github.com/liuxiong332/node-sqlite-orm", | ||
@@ -6,0 +6,0 @@ "bugs": "https://github.com/liuxiong332/node-sqlite-orm/issues", |
62735
3.42%1770
4.42%