knockout-decorators
Advanced tools
Comparing version 1.1.1 to 1.2.0
@@ -9,2 +9,11 @@ # Change Log | ||
## [1.2.0] 2018-08-30 | ||
### Added | ||
- `@observable({ expose: true })` option | ||
- `@observableArray({ expose: true })` option | ||
With `{ expose: true }` flag hidden `ko.observable` can be accessed through | ||
non-enumerable property with same name prefixed by `_`. | ||
Thanks to @FlorianBruckner. | ||
## [1.1.1] - 2018-04-20 | ||
@@ -113,3 +122,4 @@ ### Fixed | ||
[Unreleased]: https://github.com/gnaeus/knockout-decorators/compare/1.1.1...HEAD | ||
[Unreleased]: https://github.com/gnaeus/knockout-decorators/compare/1.2.0...HEAD | ||
[1.2.0]: https://github.com/gnaeus/knockout-decorators/compare/1.1.1...1.2.0 | ||
[1.1.1]: https://github.com/gnaeus/knockout-decorators/compare/1.1.0...1.1.1 | ||
@@ -116,0 +126,0 @@ [1.1.0]: https://github.com/gnaeus/knockout-decorators/compare/1.0.0...1.1.0 |
@@ -7,3 +7,4 @@ /// <reference types="knockout" /> | ||
export declare function observable(options: { | ||
deep: boolean; | ||
deep?: boolean; | ||
expose?: boolean; | ||
}): PropertyDecorator; | ||
@@ -19,3 +20,4 @@ /** | ||
export declare function observableArray(options: { | ||
deep: boolean; | ||
deep?: boolean; | ||
expose?: boolean; | ||
}): PropertyDecorator; | ||
@@ -22,0 +24,0 @@ /** |
@@ -88,3 +88,3 @@ import { computed, components, utils, subscribable, observableArray, observable } from 'knockout'; | ||
*/ | ||
function defineObservableProperty(instance, key, value, deep) { | ||
function defineObservableProperty(instance, key, value, deep, expose) { | ||
const observable$$1 = applyExtenders(instance, key, observable()); | ||
@@ -94,3 +94,3 @@ let setter = observable$$1; | ||
setter = function (newValue) { | ||
observable$$1(prepareDeepValue(newValue)); | ||
observable$$1(prepareDeepValue(newValue, expose)); | ||
}; | ||
@@ -103,5 +103,10 @@ } | ||
}); | ||
if (expose) { | ||
defineProperty(instance, "_" + String(key), { | ||
value: observable$$1, | ||
}); | ||
} | ||
setter(value); | ||
} | ||
function prepareDeepValue(value) { | ||
function prepareDeepValue(value, expose) { | ||
if (typeof value === "object") { | ||
@@ -117,3 +122,3 @@ if (isArray(value) || value === null) { | ||
// value is plain Object | ||
return prepareDeepObject(value); | ||
return prepareDeepObject(value, expose); | ||
} | ||
@@ -123,3 +128,3 @@ } | ||
// value is plain Object | ||
return prepareDeepObject(value); | ||
return prepareDeepObject(value, expose); | ||
} | ||
@@ -130,3 +135,3 @@ } | ||
} | ||
function prepareDeepObject(instance) { | ||
function prepareDeepObject(instance, expose) { | ||
if (!hasOwnProperty(instance, PATCHED_KEY)) { | ||
@@ -140,6 +145,6 @@ // mark instance as ObservableObject | ||
if (isArray(value)) { | ||
defineObservableArray(instance, key, value, true); | ||
defineObservableArray(instance, key, value, true, expose); | ||
} | ||
else { | ||
defineObservableProperty(instance, key, value, true); | ||
defineObservableProperty(instance, key, value, true, expose); | ||
} | ||
@@ -160,3 +165,3 @@ }); | ||
const allMethods = [...allArrayMethods, ...allObservableArrayMethods, "mutate", "set"]; | ||
function defineObservableArray(instance, key, value, deep) { | ||
function defineObservableArray(instance, key, value, deep, expose) { | ||
const obsArray = applyExtenders(instance, key, observableArray()); | ||
@@ -169,2 +174,7 @@ let insideObsArray = false; | ||
}); | ||
if (expose) { | ||
defineProperty(instance, "_" + String(key), { | ||
value: obsArray, | ||
}); | ||
} | ||
setter(value); | ||
@@ -195,3 +205,3 @@ function setter(newValue) { | ||
for (let i = 0; i < newValue.length; ++i) { | ||
newValue[i] = prepareDeepValue(newValue[i]); | ||
newValue[i] = prepareDeepValue(newValue[i], expose); | ||
} | ||
@@ -242,3 +252,3 @@ } | ||
for (let i = 0; i < args.length; ++i) { | ||
args[i] = prepareDeepValue(args[i]); | ||
args[i] = prepareDeepValue(args[i], expose); | ||
} | ||
@@ -258,3 +268,3 @@ insideObsArray = true; | ||
for (let i = 0; i < args.length; ++i) { | ||
args[i] = prepareDeepValue(args[i]); | ||
args[i] = prepareDeepValue(args[i], expose); | ||
} | ||
@@ -282,3 +292,3 @@ insideObsArray = true; | ||
case 3: { | ||
result = obsArray.splice(arguments[0], arguments[1], prepareDeepValue(arguments[2])); | ||
result = obsArray.splice(arguments[0], arguments[1], prepareDeepValue(arguments[2], expose)); | ||
break; | ||
@@ -289,3 +299,3 @@ } | ||
for (let i = 2; i < args.length; ++i) { | ||
args[i] = prepareDeepValue(args[i]); | ||
args[i] = prepareDeepValue(args[i], expose); | ||
} | ||
@@ -303,3 +313,3 @@ result = obsArray.splice.apply(obsArray, arguments); | ||
insideObsArray = true; | ||
const result = obsArray.replace(oldItem, prepareDeepValue(newItem)); | ||
const result = obsArray.replace(oldItem, prepareDeepValue(newItem, expose)); | ||
insideObsArray = false; | ||
@@ -316,3 +326,3 @@ return result; | ||
for (let i = 0; i < nativeArray.length; ++i) { | ||
nativeArray[i] = prepareDeepValue(nativeArray[i]); | ||
nativeArray[i] = prepareDeepValue(nativeArray[i], expose); | ||
} | ||
@@ -325,3 +335,3 @@ // it is defined for ko.observableArray | ||
value(index, newItem) { | ||
return obsArray.splice(index, 1, prepareDeepValue(newItem))[0]; | ||
return obsArray.splice(index, 1, prepareDeepValue(newItem, expose))[0]; | ||
}, | ||
@@ -361,4 +371,6 @@ }); | ||
deepObservableOption = false; | ||
exposeObservableOption = false; | ||
if (arguments.length === 1) { | ||
deepObservableOption = prototypeOrOptions.deep; | ||
exposeObservableOption = prototypeOrOptions.expose; | ||
return observableDecorator; | ||
@@ -374,4 +386,6 @@ } | ||
deepObservableOption = false; | ||
exposeObservableOption = false; | ||
if (arguments.length === 1) { | ||
deepObservableOption = prototypeOrOptions.deep; | ||
exposeObservableOption = prototypeOrOptions.expose; | ||
return observableDecorator; | ||
@@ -384,15 +398,17 @@ } | ||
let deepObservableOption; | ||
let exposeObservableOption; | ||
function observableDecorator(prototype, propKey) { | ||
const array = observableArrayOption; | ||
const deep = deepObservableOption; | ||
const expose = exposeObservableOption; | ||
defineProperty(prototype, propKey, { | ||
get() { | ||
throw new Error("@observable property '" + propKey.toString() + "' was not initialized"); | ||
throw new Error("@observable property '" + String(propKey) + "' was not initialized"); | ||
}, | ||
set(value) { | ||
if (array || isArray(value)) { | ||
defineObservableArray(this, propKey, value, deep); | ||
defineObservableArray(this, propKey, value, deep, expose); | ||
} | ||
else { | ||
defineObservableProperty(this, propKey, value, deep); | ||
defineObservableProperty(this, propKey, value, deep, expose); | ||
} | ||
@@ -423,3 +439,3 @@ }, | ||
if (!get) { | ||
throw new Error("@computed property '" + propKey.toString() + "' has no getter"); | ||
throw new Error("@computed property '" + String(propKey) + "' has no getter"); | ||
} | ||
@@ -608,3 +624,4 @@ desc.get = function () { | ||
const subscription = subscribe.apply(null, arguments); | ||
const subscriptions = this[SUBSCRIPTIONS_KEY] || (this[SUBSCRIPTIONS_KEY] = []); | ||
const subscriptions = this[SUBSCRIPTIONS_KEY] || | ||
(this[SUBSCRIPTIONS_KEY] = []); | ||
subscriptions.push(subscription); | ||
@@ -611,0 +628,0 @@ return subscription; |
@@ -1,2 +0,2 @@ | ||
import{computed as e,components as t,utils as n,subscribable as r,observableArray as o,observable as u}from"knockout";const c="__ko_decorators_";let s=c+"patched__",i=c+"extenders__",l=c+"subscriptions__";"undefined"!=typeof Symbol&&(s=Symbol(s),i=Symbol(i),l=Symbol(l));const a=Array.prototype;function f(e,t,n){n.configurable=!0,Object.defineProperty(e,t,n)}const b=n.extend,h=n.objectForEach,p=Array.isArray.bind(Array),v=Object.getPrototypeOf.bind(Object),d=Object.getOwnPropertyDescriptor.bind(Object),m=Function.prototype.call.bind(Object.prototype.hasOwnProperty),y=Function.prototype.call.bind(a.slice);function g(e,t){const n=new r,o=function(){const e=y(arguments);n.notifySubscribers(e)};return o.subscribe=function(e){return n.subscribe(function(t){e.apply(null,t)})},f(e,t,{value:o}),o}function w(e,t,n){const r=e[i],o=r&&r[t];return o&&o.forEach(t=>{const r=t instanceof Function?t.call(e):t;n=n.extend(r)}),n}function j(e,t,n){let r=e[i];m(e,i)||(e[i]=r=b({},r),h(r,(e,t)=>{r[e]=[...t]})),(r[t]||(r[t]=[])).push(n)}function _(e,t,n,r){const o=w(e,t,u());let c=o;r&&(c=function(e){o(O(e))}),f(e,t,{enumerable:!0,get:o,set:c}),c(n)}function O(e){if("object"==typeof e){if(p(e)||null===e)return e;if(m(e,"constructor")){const t=v(e);if(t===Object.prototype||null===t)return A(e)}else if(e.constructor===Object)return A(e)}return e}function A(e){return m(e,s)||(f(e,s,{value:!0}),h(e,(t,n)=>{p(n)?F(e,t,n,!0):_(e,t,n,!0)})),e}const k=["pop","reverse","shift","sort"],x=[...k,"push","splice","unshift"],S=["remove","removeAll","destroy","destroyAll","replace","subscribe"],C=[...S,"replace"],E=[...x,...C,"mutate","set"];function F(e,t,n,r){const u=w(e,t,o());let c=!1;function i(e){const t=u.peek();if(t!==e&&(p(t)&&m(t,s)&&(delete t[s],E.forEach(e=>{delete t[e]})),p(e))){if(m(e,s)&&(e=[...e]),r)for(let t=0;t<e.length;++t)e[t]=O(e[t]);f(e,s,{value:!0}),n=e,(r?k:x).forEach(e=>f(n,e,{value(){if(c)return a[e].apply(n,arguments);c=!0;const t=u[e].apply(u,arguments);return c=!1,t}})),(r?S:C).forEach(e=>f(n,e,{value(){c=!0;const t=u[e].apply(u,arguments);return c=!1,t}})),r?(f(n,"push",{value(){if(c)return a.push.apply(n,arguments);const e=y(arguments);for(let t=0;t<e.length;++t)e[t]=O(e[t]);c=!0;const t=u.push.apply(u,e);return c=!1,t}}),f(n,"unshift",{value(){if(c)return a.unshift.apply(n,arguments);const e=y(arguments);for(let t=0;t<e.length;++t)e[t]=O(e[t]);c=!0;const t=u.unshift.apply(u,e);return c=!1,t}}),f(n,"splice",{value(){if(c)return a.splice.apply(n,arguments);let e;switch(c=!0,arguments.length){case 0:case 1:case 2:e=u.splice.apply(u,arguments);break;case 3:e=u.splice(arguments[0],arguments[1],O(arguments[2]));break;default:{const t=y(arguments);for(let e=2;e<t.length;++e)t[e]=O(t[e]);e=u.splice.apply(u,arguments);break}}return c=!1,e}}),f(n,"replace",{value(e,t){c=!0;const n=u.replace(e,O(t));return c=!1,n}}),f(n,"mutate",{value(e){const t=u.peek();u.valueWillMutate(),e(t);for(let e=0;e<t.length;++e)t[e]=O(t[e]);u.valueHasMutated()}}),f(n,"set",{value:(e,t)=>u.splice(e,1,O(t))[0]})):(f(n,"mutate",{value(e){u.valueWillMutate(),e(u.peek()),u.valueHasMutated()}}),f(n,"set",{value:(e,t)=>u.splice(e,1,t)[0]}))}var n;c=!0,u(e),c=!1}f(e,t,{enumerable:!0,get:u,set:i}),i(n)}function M(e,t){return z=!1,N=!1,1===arguments.length?(N=e.deep,B):B(e,t)}function q(e,t){return z=!0,N=!1,1===arguments.length?(N=e.deep,B):B(e,t)}let z,N,V;function B(e,t){const n=z,r=N;f(e,t,{get(){throw new Error("@observable property '"+t.toString()+"' was not initialized")},set(e){n||p(e)?F(this,t,e,r):_(this,t,e,r)}})}function D(e,t,n){return V={pure:!0},1===arguments.length?(V=e,G):G(e,t,n)}function G(t,n,r){const o=V,{get:u,set:c}=r||(r=d(t,n));if(!u)throw new Error("@computed property '"+n.toString()+"' has no getter");return r.get=function(){const t=w(this,n,e(u,this,o));return f(this,n,{get:t,set:c}),t()},r}function H(e){return function(t,n){j(t,n,e)}}function I(e,n,r,o){return void 0===o&&(void 0===r?"object"!=typeof n||n.constructor!==Object||"require"in n||"element"in n||(o=n,n=void 0):"object"==typeof r&&(o=r,r=void 0)),function(r){t.register(e,b({viewModel:r.length<2?r:{createViewModel:(e,{element:t,templateNodes:n})=>new r(e,t,n)},template:n||"\x3c!----\x3e",synchronous:!0},o))}}function J(e,t,n){const{value:r,configurable:o,enumerable:u}=n||(n=d(e,t));return{configurable:o,enumerable:u,get(){if(this===e)return r;const n=r.bind(this);return f(this,t,{value:n}),n}}}function K(e,t){f(e,t,{get(){return g(this,t)}})}function L(t,n,r){const o=r&&r.once||!1;if(m(t,"subscribe")){const e=t;if(o){const t=e.subscribe(function(){t.dispose(),n.apply(null,arguments)});return t}return e.subscribe(n)}{const u=r&&r.event||"change";let c,i;if(c=o?function(){i.dispose(),n.apply(null,arguments)}:n,"arrayChange"===u){const e=t();if(!p(e)||!m(e,s))throw new Error("Can not subscribe to 'arrayChange' because dependency is not an 'observableArray'");i=e.subscribe(c,null,u)}else{const n=e(t),r=(i=n.subscribe(c,null,u)).dispose;i.dispose=function(){r.call(this),n.dispose()}}return i}}function P(e,t){return m(e,t)||e[t],d(e,t).get}function Q(e){return void 0===e&&(e=class{}),class extends e{dispose(){const e=this[l];e&&(e.forEach(e=>{e.dispose()}),delete this[l])}subscribe(){const e=L.apply(null,arguments);return(this[l]||(this[l]=[])).push(e),e}unwrap(e){return P(this,e)}}}export{M as observable,q as observableArray,D as computed,H as extend,I as component,J as autobind,K as event,L as subscribe,P as unwrap,Q as Disposable}; | ||
import{computed as e,components as t,utils as n,subscribable as r,observableArray as o,observable as u}from"knockout";const c="__ko_decorators_";let s=c+"patched__",i=c+"extenders__",l=c+"subscriptions__";"undefined"!=typeof Symbol&&(s=Symbol(s),i=Symbol(i),l=Symbol(l));const a=Array.prototype;function f(e,t,n){n.configurable=!0,Object.defineProperty(e,t,n)}const b=n.extend,v=n.objectForEach,h=Array.isArray.bind(Array),p=Object.getPrototypeOf.bind(Object),d=Object.getOwnPropertyDescriptor.bind(Object),m=Function.prototype.call.bind(Object.prototype.hasOwnProperty),y=Function.prototype.call.bind(a.slice);function g(e,t){const n=new r,o=function(){const e=y(arguments);n.notifySubscribers(e)};return o.subscribe=function(e){return n.subscribe(function(t){e.apply(null,t)})},f(e,t,{value:o}),o}function w(e,t,n){const r=e[i],o=r&&r[t];return o&&o.forEach(t=>{const r=t instanceof Function?t.call(e):t;n=n.extend(r)}),n}function j(e,t,n){let r=e[i];m(e,i)||(e[i]=r=b({},r),v(r,(e,t)=>{r[e]=[...t]})),(r[t]||(r[t]=[])).push(n)}function _(e,t,n,r,o){const c=w(e,t,u());let s=c;r&&(s=function(e){c(O(e,o))}),f(e,t,{enumerable:!0,get:c,set:s}),o&&f(e,"_"+String(t),{value:c}),s(n)}function O(e,t){if("object"==typeof e){if(h(e)||null===e)return e;if(m(e,"constructor")){const n=p(e);if(n===Object.prototype||null===n)return S(e,t)}else if(e.constructor===Object)return S(e,t)}return e}function S(e,t){return m(e,s)||(f(e,s,{value:!0}),v(e,(n,r)=>{h(r)?F(e,n,r,!0,t):_(e,n,r,!0,t)})),e}const A=["pop","reverse","shift","sort"],k=[...A,"push","splice","unshift"],x=["remove","removeAll","destroy","destroyAll","replace","subscribe"],C=[...x,"replace"],E=[...k,...C,"mutate","set"];function F(e,t,n,r,u){const c=w(e,t,o());let i=!1;function l(e){const t=c.peek();if(t!==e&&(h(t)&&m(t,s)&&(delete t[s],E.forEach(e=>{delete t[e]})),h(e))){if(m(e,s)&&(e=[...e]),r)for(let t=0;t<e.length;++t)e[t]=O(e[t],u);f(e,s,{value:!0}),n=e,(r?A:k).forEach(e=>f(n,e,{value(){if(i)return a[e].apply(n,arguments);i=!0;const t=c[e].apply(c,arguments);return i=!1,t}})),(r?x:C).forEach(e=>f(n,e,{value(){i=!0;const t=c[e].apply(c,arguments);return i=!1,t}})),r?(f(n,"push",{value(){if(i)return a.push.apply(n,arguments);const e=y(arguments);for(let t=0;t<e.length;++t)e[t]=O(e[t],u);i=!0;const t=c.push.apply(c,e);return i=!1,t}}),f(n,"unshift",{value(){if(i)return a.unshift.apply(n,arguments);const e=y(arguments);for(let t=0;t<e.length;++t)e[t]=O(e[t],u);i=!0;const t=c.unshift.apply(c,e);return i=!1,t}}),f(n,"splice",{value(){if(i)return a.splice.apply(n,arguments);let e;switch(i=!0,arguments.length){case 0:case 1:case 2:e=c.splice.apply(c,arguments);break;case 3:e=c.splice(arguments[0],arguments[1],O(arguments[2],u));break;default:{const t=y(arguments);for(let e=2;e<t.length;++e)t[e]=O(t[e],u);e=c.splice.apply(c,arguments);break}}return i=!1,e}}),f(n,"replace",{value(e,t){i=!0;const n=c.replace(e,O(t,u));return i=!1,n}}),f(n,"mutate",{value(e){const t=c.peek();c.valueWillMutate(),e(t);for(let e=0;e<t.length;++e)t[e]=O(t[e],u);c.valueHasMutated()}}),f(n,"set",{value:(e,t)=>c.splice(e,1,O(t,u))[0]})):(f(n,"mutate",{value(e){c.valueWillMutate(),e(c.peek()),c.valueHasMutated()}}),f(n,"set",{value:(e,t)=>c.splice(e,1,t)[0]}))}var n;i=!0,c(e),i=!1}f(e,t,{enumerable:!0,get:c,set:l}),u&&f(e,"_"+String(t),{value:c}),l(n)}function M(e,t){return z=!1,N=!1,V=!1,1===arguments.length?(N=e.deep,V=e.expose,D):D(e,t)}function q(e,t){return z=!0,N=!1,V=!1,1===arguments.length?(N=e.deep,V=e.expose,D):D(e,t)}let z,N,V,B;function D(e,t){const n=z,r=N,o=V;f(e,t,{get(){throw new Error("@observable property '"+String(t)+"' was not initialized")},set(e){n||h(e)?F(this,t,e,r,o):_(this,t,e,r,o)}})}function G(e,t,n){return B={pure:!0},1===arguments.length?(B=e,H):H(e,t,n)}function H(t,n,r){const o=B,{get:u,set:c}=r||(r=d(t,n));if(!u)throw new Error("@computed property '"+String(n)+"' has no getter");return r.get=function(){const t=w(this,n,e(u,this,o));return f(this,n,{get:t,set:c}),t()},r}function I(e){return function(t,n){j(t,n,e)}}function J(e,n,r,o){return void 0===o&&(void 0===r?"object"!=typeof n||n.constructor!==Object||"require"in n||"element"in n||(o=n,n=void 0):"object"==typeof r&&(o=r,r=void 0)),function(r){t.register(e,b({viewModel:r.length<2?r:{createViewModel:(e,{element:t,templateNodes:n})=>new r(e,t,n)},template:n||"\x3c!----\x3e",synchronous:!0},o))}}function K(e,t,n){const{value:r,configurable:o,enumerable:u}=n||(n=d(e,t));return{configurable:o,enumerable:u,get(){if(this===e)return r;const n=r.bind(this);return f(this,t,{value:n}),n}}}function L(e,t){f(e,t,{get(){return g(this,t)}})}function P(t,n,r){const o=r&&r.once||!1;if(m(t,"subscribe")){const e=t;if(o){const t=e.subscribe(function(){t.dispose(),n.apply(null,arguments)});return t}return e.subscribe(n)}{const u=r&&r.event||"change";let c,i;if(c=o?function(){i.dispose(),n.apply(null,arguments)}:n,"arrayChange"===u){const e=t();if(!h(e)||!m(e,s))throw new Error("Can not subscribe to 'arrayChange' because dependency is not an 'observableArray'");i=e.subscribe(c,null,u)}else{const n=e(t),r=(i=n.subscribe(c,null,u)).dispose;i.dispose=function(){r.call(this),n.dispose()}}return i}}function Q(e,t){return m(e,t)||e[t],d(e,t).get}function R(e){return void 0===e&&(e=class{}),class extends e{dispose(){const e=this[l];e&&(e.forEach(e=>{e.dispose()}),delete this[l])}subscribe(){const e=P.apply(null,arguments);return(this[l]||(this[l]=[])).push(e),e}unwrap(e){return Q(this,e)}}}export{M as observable,q as observableArray,G as computed,I as extend,J as component,K as autobind,L as event,P as subscribe,Q as unwrap,R as Disposable}; | ||
//# sourceMappingURL=knockout-decorators.esm.min.js.map |
@@ -118,3 +118,3 @@ (function (global, factory) { | ||
*/ | ||
function defineObservableProperty(instance, key, value, deep) { | ||
function defineObservableProperty(instance, key, value, deep, expose) { | ||
var observable = applyExtenders(instance, key, ko.observable()); | ||
@@ -124,3 +124,3 @@ var setter = observable; | ||
setter = function (newValue) { | ||
observable(prepareDeepValue(newValue)); | ||
observable(prepareDeepValue(newValue, expose)); | ||
}; | ||
@@ -133,5 +133,10 @@ } | ||
}); | ||
if (expose) { | ||
defineProperty(instance, "_" + String(key), { | ||
value: observable, | ||
}); | ||
} | ||
setter(value); | ||
} | ||
function prepareDeepValue(value) { | ||
function prepareDeepValue(value, expose) { | ||
if (typeof value === "object") { | ||
@@ -147,3 +152,3 @@ if (isArray(value) || value === null) { | ||
// value is plain Object | ||
return prepareDeepObject(value); | ||
return prepareDeepObject(value, expose); | ||
} | ||
@@ -153,3 +158,3 @@ } | ||
// value is plain Object | ||
return prepareDeepObject(value); | ||
return prepareDeepObject(value, expose); | ||
} | ||
@@ -160,3 +165,3 @@ } | ||
} | ||
function prepareDeepObject(instance) { | ||
function prepareDeepObject(instance, expose) { | ||
if (!hasOwnProperty(instance, PATCHED_KEY)) { | ||
@@ -170,6 +175,6 @@ // mark instance as ObservableObject | ||
if (isArray(value)) { | ||
defineObservableArray(instance, key, value, true); | ||
defineObservableArray(instance, key, value, true, expose); | ||
} | ||
else { | ||
defineObservableProperty(instance, key, value, true); | ||
defineObservableProperty(instance, key, value, true, expose); | ||
} | ||
@@ -190,3 +195,3 @@ }); | ||
var allMethods = allArrayMethods.concat(allObservableArrayMethods, ["mutate", "set"]); | ||
function defineObservableArray(instance, key, value, deep) { | ||
function defineObservableArray(instance, key, value, deep, expose) { | ||
var obsArray = applyExtenders(instance, key, ko.observableArray()); | ||
@@ -199,2 +204,7 @@ var insideObsArray = false; | ||
}); | ||
if (expose) { | ||
defineProperty(instance, "_" + String(key), { | ||
value: obsArray, | ||
}); | ||
} | ||
setter(value); | ||
@@ -225,3 +235,3 @@ function setter(newValue) { | ||
for (var i = 0; i < newValue.length; ++i) { | ||
newValue[i] = prepareDeepValue(newValue[i]); | ||
newValue[i] = prepareDeepValue(newValue[i], expose); | ||
} | ||
@@ -272,3 +282,3 @@ } | ||
for (var i = 0; i < args.length; ++i) { | ||
args[i] = prepareDeepValue(args[i]); | ||
args[i] = prepareDeepValue(args[i], expose); | ||
} | ||
@@ -288,3 +298,3 @@ insideObsArray = true; | ||
for (var i = 0; i < args.length; ++i) { | ||
args[i] = prepareDeepValue(args[i]); | ||
args[i] = prepareDeepValue(args[i], expose); | ||
} | ||
@@ -312,3 +322,3 @@ insideObsArray = true; | ||
case 3: { | ||
result = obsArray.splice(arguments[0], arguments[1], prepareDeepValue(arguments[2])); | ||
result = obsArray.splice(arguments[0], arguments[1], prepareDeepValue(arguments[2], expose)); | ||
break; | ||
@@ -319,3 +329,3 @@ } | ||
for (var i = 2; i < args.length; ++i) { | ||
args[i] = prepareDeepValue(args[i]); | ||
args[i] = prepareDeepValue(args[i], expose); | ||
} | ||
@@ -333,3 +343,3 @@ result = obsArray.splice.apply(obsArray, arguments); | ||
insideObsArray = true; | ||
var result = obsArray.replace(oldItem, prepareDeepValue(newItem)); | ||
var result = obsArray.replace(oldItem, prepareDeepValue(newItem, expose)); | ||
insideObsArray = false; | ||
@@ -346,3 +356,3 @@ return result; | ||
for (var i = 0; i < nativeArray.length; ++i) { | ||
nativeArray[i] = prepareDeepValue(nativeArray[i]); | ||
nativeArray[i] = prepareDeepValue(nativeArray[i], expose); | ||
} | ||
@@ -355,3 +365,3 @@ // it is defined for ko.observableArray | ||
value: function (index, newItem) { | ||
return obsArray.splice(index, 1, prepareDeepValue(newItem))[0]; | ||
return obsArray.splice(index, 1, prepareDeepValue(newItem, expose))[0]; | ||
}, | ||
@@ -386,4 +396,6 @@ }); | ||
deepObservableOption = false; | ||
exposeObservableOption = false; | ||
if (arguments.length === 1) { | ||
deepObservableOption = prototypeOrOptions.deep; | ||
exposeObservableOption = prototypeOrOptions.expose; | ||
return observableDecorator; | ||
@@ -399,4 +411,6 @@ } | ||
deepObservableOption = false; | ||
exposeObservableOption = false; | ||
if (arguments.length === 1) { | ||
deepObservableOption = prototypeOrOptions.deep; | ||
exposeObservableOption = prototypeOrOptions.expose; | ||
return observableDecorator; | ||
@@ -409,15 +423,17 @@ } | ||
var deepObservableOption; | ||
var exposeObservableOption; | ||
function observableDecorator(prototype, propKey) { | ||
var array = observableArrayOption; | ||
var deep = deepObservableOption; | ||
var expose = exposeObservableOption; | ||
defineProperty(prototype, propKey, { | ||
get: function () { | ||
throw new Error("@observable property '" + propKey.toString() + "' was not initialized"); | ||
throw new Error("@observable property '" + String(propKey) + "' was not initialized"); | ||
}, | ||
set: function (value) { | ||
if (array || isArray(value)) { | ||
defineObservableArray(this, propKey, value, deep); | ||
defineObservableArray(this, propKey, value, deep, expose); | ||
} | ||
else { | ||
defineObservableProperty(this, propKey, value, deep); | ||
defineObservableProperty(this, propKey, value, deep, expose); | ||
} | ||
@@ -448,3 +464,3 @@ }, | ||
if (!get) { | ||
throw new Error("@computed property '" + propKey.toString() + "' has no getter"); | ||
throw new Error("@computed property '" + String(propKey) + "' has no getter"); | ||
} | ||
@@ -641,3 +657,4 @@ desc.get = function () { | ||
var subscription = subscribe.apply(null, arguments); | ||
var subscriptions = this[SUBSCRIPTIONS_KEY] || (this[SUBSCRIPTIONS_KEY] = []); | ||
var subscriptions = this[SUBSCRIPTIONS_KEY] || | ||
(this[SUBSCRIPTIONS_KEY] = []); | ||
subscriptions.push(subscription); | ||
@@ -644,0 +661,0 @@ return subscription; |
@@ -1,2 +0,2 @@ | ||
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("knockout")):"function"==typeof define&&define.amd?define(["exports","knockout"],t):t(n.KnockoutDecorators={},n.ko)}(this,function(n,t){"use strict";var e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(n,t){n.__proto__=t}||function(n,t){for(var e in t)t.hasOwnProperty(e)&&(n[e]=t[e])};var r="__ko_decorators_",u=r+"patched__",i=r+"extenders__",o=r+"subscriptions__";"undefined"!=typeof Symbol&&(u=Symbol(u),i=Symbol(i),o=Symbol(o));var c=Array.prototype;function f(n,t,e){e.configurable=!0,Object.defineProperty(n,t,e)}var a=t.utils.extend,s=t.utils.objectForEach,v=Array.isArray.bind(Array),l=Object.getPrototypeOf.bind(Object),b=Object.getOwnPropertyDescriptor.bind(Object),h=Function.prototype.call.bind(Object.prototype.hasOwnProperty),d=Function.prototype.call.bind(c.slice);function p(n,t,e){var r=n[i],u=r&&r[t];return u&&u.forEach(function(t){var r=t instanceof Function?t.call(n):t;e=e.extend(r)}),e}function y(n,e,r,u){var i=p(n,e,t.observable()),o=i;u&&(o=function(n){i(j(n))}),f(n,e,{enumerable:!0,get:i,set:o}),o(r)}function j(n){if("object"==typeof n){if(v(n)||null===n)return n;if(h(n,"constructor")){var t=l(n);if(t===Object.prototype||null===t)return m(n)}else if(n.constructor===Object)return m(n)}return n}function m(n){return h(n,u)||(f(n,u,{value:!0}),s(n,function(t,e){v(e)?C(n,t,e,!0):y(n,t,e,!0)})),n}var _,w,O,g=["pop","reverse","shift","sort"],k=g.concat(["push","splice","unshift"]),A=["remove","removeAll","destroy","destroyAll","replace","subscribe"],x=A.concat(["replace"]),S=k.concat(x,["mutate","set"]);function C(n,e,r,i){var o=p(n,e,t.observableArray()),a=!1;function s(n){var t,e=o.peek();if(e!==n&&(v(e)&&h(e,u)&&(delete e[u],S.forEach(function(n){delete e[n]})),v(n))){if(h(n,u)&&(n=n.slice()),i)for(var r=0;r<n.length;++r)n[r]=j(n[r]);f(n,u,{value:!0}),t=n,(i?g:k).forEach(function(n){return f(t,n,{value:function(){if(a)return c[n].apply(t,arguments);a=!0;var e=o[n].apply(o,arguments);return a=!1,e}})}),(i?A:x).forEach(function(n){return f(t,n,{value:function(){a=!0;var t=o[n].apply(o,arguments);return a=!1,t}})}),i?(f(t,"push",{value:function(){if(a)return c.push.apply(t,arguments);for(var n=d(arguments),e=0;e<n.length;++e)n[e]=j(n[e]);a=!0;var r=o.push.apply(o,n);return a=!1,r}}),f(t,"unshift",{value:function(){if(a)return c.unshift.apply(t,arguments);for(var n=d(arguments),e=0;e<n.length;++e)n[e]=j(n[e]);a=!0;var r=o.unshift.apply(o,n);return a=!1,r}}),f(t,"splice",{value:function(){if(a)return c.splice.apply(t,arguments);var n;switch(a=!0,arguments.length){case 0:case 1:case 2:n=o.splice.apply(o,arguments);break;case 3:n=o.splice(arguments[0],arguments[1],j(arguments[2]));break;default:for(var e=d(arguments),r=2;r<e.length;++r)e[r]=j(e[r]);n=o.splice.apply(o,arguments)}return a=!1,n}}),f(t,"replace",{value:function(n,t){a=!0;var e=o.replace(n,j(t));return a=!1,e}}),f(t,"mutate",{value:function(n){var t=o.peek();o.valueWillMutate(),n(t);for(var e=0;e<t.length;++e)t[e]=j(t[e]);o.valueHasMutated()}}),f(t,"set",{value:function(n,t){return o.splice(n,1,j(t))[0]}})):(f(t,"mutate",{value:function(n){o.valueWillMutate(),n(o.peek()),o.valueHasMutated()}}),f(t,"set",{value:function(n,t){return o.splice(n,1,t)[0]}}))}a=!0,o(n),a=!1}f(n,e,{enumerable:!0,get:o,set:s}),s(r)}function E(n,t){var e=_,r=w;f(n,t,{get:function(){throw new Error("@observable property '"+t.toString()+"' was not initialized")},set:function(n){e||v(n)?C(this,t,n,r):y(this,t,n,r)}})}function F(n,e,r){var u=O,i=r||(r=b(n,e)),o=i.get,c=i.set;if(!o)throw new Error("@computed property '"+e.toString()+"' has no getter");return r.get=function(){var n=p(this,e,t.computed(o,this,u));return f(this,e,{get:n,set:c}),n()},r}function M(n,e,r){var i=r&&r.once||!1;if(h(n,"subscribe")){var o=n;if(i){var c=o.subscribe(function(){c.dispose(),e.apply(null,arguments)});return c}return o.subscribe(e)}o=r&&r.event||"change";var f,a=void 0;if(a=i?function(){f.dispose(),e.apply(null,arguments)}:e,"arrayChange"===o){var s=n();if(!v(s)||!h(s,u))throw new Error("Can not subscribe to 'arrayChange' because dependency is not an 'observableArray'");f=s.subscribe(a,null,o)}else{var l=t.computed(n),b=(f=l.subscribe(a,null,o)).dispose;f.dispose=function(){b.call(this),l.dispose()}}return f}function q(n,t){return h(n,t)||n[t],b(n,t).get}n.observable=function(n,t){return _=!1,w=!1,1===arguments.length?(w=n.deep,E):E(n,t)},n.observableArray=function(n,t){return _=!0,w=!1,1===arguments.length?(w=n.deep,E):E(n,t)},n.computed=function(n,t,e){return O={pure:!0},1===arguments.length?(O=n,F):F(n,t,e)},n.extend=function(n){return function(t,e){!function(n,t,e){var r=n[i];h(n,i)||(n[i]=r=a({},r),s(r,function(n,t){r[n]=t.slice()})),(r[t]||(r[t]=[])).push(e)}(t,e,n)}},n.component=function(n,e,r,u){return void 0===u&&(void 0===r?"object"!=typeof e||e.constructor!==Object||"require"in e||"element"in e||(u=e,e=void 0):"object"==typeof r&&(u=r,r=void 0)),function(r){t.components.register(n,a({viewModel:r.length<2?r:{createViewModel:function(n,t){var e=t.element,u=t.templateNodes;return new r(n,e,u)}},template:e||"\x3c!----\x3e",synchronous:!0},u))}},n.autobind=function(n,t,e){var r=e||(e=b(n,t)),u=r.value;return{configurable:r.configurable,enumerable:r.enumerable,get:function(){if(this===n)return u;var e=u.bind(this);return f(this,t,{value:e}),e}}},n.event=function(n,e){f(n,e,{get:function(){return function(n,e){var r=new t.subscribable,u=function(){var n=d(arguments);r.notifySubscribers(n)};return u.subscribe=function(n){return r.subscribe(function(t){n.apply(null,t)})},f(n,e,{value:u}),u}(this,e)}})},n.subscribe=M,n.unwrap=q,n.Disposable=function(n){return void 0===n&&(n=function(){}),function(n){function t(){return null!==n&&n.apply(this,arguments)||this}return function(n,t){function r(){this.constructor=n}e(n,t),n.prototype=null===t?Object.create(t):(r.prototype=t.prototype,new r)}(t,n),t.prototype.dispose=function(){var n=this[o];n&&(n.forEach(function(n){n.dispose()}),delete this[o])},t.prototype.subscribe=function(){var n=M.apply(null,arguments);return(this[o]||(this[o]=[])).push(n),n},t.prototype.unwrap=function(n){return q(this,n)},t}(n)},Object.defineProperty(n,"__esModule",{value:!0})}); | ||
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("knockout")):"function"==typeof define&&define.amd?define(["exports","knockout"],t):t(n.KnockoutDecorators={},n.ko)}(this,function(n,t){"use strict";var e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(n,t){n.__proto__=t}||function(n,t){for(var e in t)t.hasOwnProperty(e)&&(n[e]=t[e])};var r="__ko_decorators_",u=r+"patched__",i=r+"extenders__",o=r+"subscriptions__";"undefined"!=typeof Symbol&&(u=Symbol(u),i=Symbol(i),o=Symbol(o));var c=Array.prototype;function f(n,t,e){e.configurable=!0,Object.defineProperty(n,t,e)}var a=t.utils.extend,s=t.utils.objectForEach,v=Array.isArray.bind(Array),l=Object.getPrototypeOf.bind(Object),b=Object.getOwnPropertyDescriptor.bind(Object),h=Function.prototype.call.bind(Object.prototype.hasOwnProperty),d=Function.prototype.call.bind(c.slice);function p(n,t,e){var r=n[i],u=r&&r[t];return u&&u.forEach(function(t){var r=t instanceof Function?t.call(n):t;e=e.extend(r)}),e}function y(n,e,r,u,i){var o=p(n,e,t.observable()),c=o;u&&(c=function(n){o(_(n,i))}),f(n,e,{enumerable:!0,get:o,set:c}),i&&f(n,"_"+String(e),{value:o}),c(r)}function _(n,t){if("object"==typeof n){if(v(n)||null===n)return n;if(h(n,"constructor")){var e=l(n);if(e===Object.prototype||null===e)return j(n,t)}else if(n.constructor===Object)return j(n,t)}return n}function j(n,t){return h(n,u)||(f(n,u,{value:!0}),s(n,function(e,r){v(r)?E(n,e,r,!0,t):y(n,e,r,!0,t)})),n}var m,g,w,O,S=["pop","reverse","shift","sort"],k=S.concat(["push","splice","unshift"]),A=["remove","removeAll","destroy","destroyAll","replace","subscribe"],x=A.concat(["replace"]),C=k.concat(x,["mutate","set"]);function E(n,e,r,i,o){var a=p(n,e,t.observableArray()),s=!1;function l(n){var t,e=a.peek();if(e!==n&&(v(e)&&h(e,u)&&(delete e[u],C.forEach(function(n){delete e[n]})),v(n))){if(h(n,u)&&(n=n.slice()),i)for(var r=0;r<n.length;++r)n[r]=_(n[r],o);f(n,u,{value:!0}),t=n,(i?S:k).forEach(function(n){return f(t,n,{value:function(){if(s)return c[n].apply(t,arguments);s=!0;var e=a[n].apply(a,arguments);return s=!1,e}})}),(i?A:x).forEach(function(n){return f(t,n,{value:function(){s=!0;var t=a[n].apply(a,arguments);return s=!1,t}})}),i?(f(t,"push",{value:function(){if(s)return c.push.apply(t,arguments);for(var n=d(arguments),e=0;e<n.length;++e)n[e]=_(n[e],o);s=!0;var r=a.push.apply(a,n);return s=!1,r}}),f(t,"unshift",{value:function(){if(s)return c.unshift.apply(t,arguments);for(var n=d(arguments),e=0;e<n.length;++e)n[e]=_(n[e],o);s=!0;var r=a.unshift.apply(a,n);return s=!1,r}}),f(t,"splice",{value:function(){if(s)return c.splice.apply(t,arguments);var n;switch(s=!0,arguments.length){case 0:case 1:case 2:n=a.splice.apply(a,arguments);break;case 3:n=a.splice(arguments[0],arguments[1],_(arguments[2],o));break;default:for(var e=d(arguments),r=2;r<e.length;++r)e[r]=_(e[r],o);n=a.splice.apply(a,arguments)}return s=!1,n}}),f(t,"replace",{value:function(n,t){s=!0;var e=a.replace(n,_(t,o));return s=!1,e}}),f(t,"mutate",{value:function(n){var t=a.peek();a.valueWillMutate(),n(t);for(var e=0;e<t.length;++e)t[e]=_(t[e],o);a.valueHasMutated()}}),f(t,"set",{value:function(n,t){return a.splice(n,1,_(t,o))[0]}})):(f(t,"mutate",{value:function(n){a.valueWillMutate(),n(a.peek()),a.valueHasMutated()}}),f(t,"set",{value:function(n,t){return a.splice(n,1,t)[0]}}))}s=!0,a(n),s=!1}f(n,e,{enumerable:!0,get:a,set:l}),o&&f(n,"_"+String(e),{value:a}),l(r)}function F(n,t){var e=m,r=g,u=w;f(n,t,{get:function(){throw new Error("@observable property '"+String(t)+"' was not initialized")},set:function(n){e||v(n)?E(this,t,n,r,u):y(this,t,n,r,u)}})}function M(n,e,r){var u=O,i=r||(r=b(n,e)),o=i.get,c=i.set;if(!o)throw new Error("@computed property '"+String(e)+"' has no getter");return r.get=function(){var n=p(this,e,t.computed(o,this,u));return f(this,e,{get:n,set:c}),n()},r}function q(n,e,r){var i=r&&r.once||!1;if(h(n,"subscribe")){var o=n;if(i){var c=o.subscribe(function(){c.dispose(),e.apply(null,arguments)});return c}return o.subscribe(e)}o=r&&r.event||"change";var f,a=void 0;if(a=i?function(){f.dispose(),e.apply(null,arguments)}:e,"arrayChange"===o){var s=n();if(!v(s)||!h(s,u))throw new Error("Can not subscribe to 'arrayChange' because dependency is not an 'observableArray'");f=s.subscribe(a,null,o)}else{var l=t.computed(n),b=(f=l.subscribe(a,null,o)).dispose;f.dispose=function(){b.call(this),l.dispose()}}return f}function z(n,t){return h(n,t)||n[t],b(n,t).get}n.observable=function(n,t){return m=!1,g=!1,w=!1,1===arguments.length?(g=n.deep,w=n.expose,F):F(n,t)},n.observableArray=function(n,t){return m=!0,g=!1,w=!1,1===arguments.length?(g=n.deep,w=n.expose,F):F(n,t)},n.computed=function(n,t,e){return O={pure:!0},1===arguments.length?(O=n,M):M(n,t,e)},n.extend=function(n){return function(t,e){!function(n,t,e){var r=n[i];h(n,i)||(n[i]=r=a({},r),s(r,function(n,t){r[n]=t.slice()})),(r[t]||(r[t]=[])).push(e)}(t,e,n)}},n.component=function(n,e,r,u){return void 0===u&&(void 0===r?"object"!=typeof e||e.constructor!==Object||"require"in e||"element"in e||(u=e,e=void 0):"object"==typeof r&&(u=r,r=void 0)),function(r){t.components.register(n,a({viewModel:r.length<2?r:{createViewModel:function(n,t){var e=t.element,u=t.templateNodes;return new r(n,e,u)}},template:e||"\x3c!----\x3e",synchronous:!0},u))}},n.autobind=function(n,t,e){var r=e||(e=b(n,t)),u=r.value;return{configurable:r.configurable,enumerable:r.enumerable,get:function(){if(this===n)return u;var e=u.bind(this);return f(this,t,{value:e}),e}}},n.event=function(n,e){f(n,e,{get:function(){return function(n,e){var r=new t.subscribable,u=function(){var n=d(arguments);r.notifySubscribers(n)};return u.subscribe=function(n){return r.subscribe(function(t){n.apply(null,t)})},f(n,e,{value:u}),u}(this,e)}})},n.subscribe=q,n.unwrap=z,n.Disposable=function(n){return void 0===n&&(n=function(){}),function(n){function t(){return null!==n&&n.apply(this,arguments)||this}return function(n,t){function r(){this.constructor=n}e(n,t),n.prototype=null===t?Object.create(t):(r.prototype=t.prototype,new r)}(t,n),t.prototype.dispose=function(){var n=this[o];n&&(n.forEach(function(n){n.dispose()}),delete this[o])},t.prototype.subscribe=function(){var n=q.apply(null,arguments);return(this[o]||(this[o]=[])).push(n),n},t.prototype.unwrap=function(n){return z(this,n)},t}(n)},Object.defineProperty(n,"__esModule",{value:!0})}); | ||
//# sourceMappingURL=knockout-decorators.min.js.map |
{ | ||
"name": "knockout-decorators", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "Decorators for use Knockout JS in TypeScript and ESNext environments", | ||
@@ -84,4 +84,5 @@ "main": "dist/knockout-decorators.js", | ||
}, | ||
"testRegex": "/test/.*(test|benchmark)\\.ts$" | ||
"testRegex": "/test/.*(test|benchmark)\\.ts$", | ||
"testURL": "http://localhost" | ||
} | ||
} |
@@ -57,3 +57,3 @@ # Knockout Decorators | ||
```js | ||
@observable(options: { deep: boolean }); | ||
@observable(options: { deep?: boolean, expose?: boolean }); | ||
@observable; | ||
@@ -106,3 +106,16 @@ ``` | ||
``` | ||
If `{ expose: true }` option is provided then hidden `ko.observable` will be | ||
exposed as non-enumerable property with same name prefixed by `_`. | ||
```js | ||
import { observable } from "knockout-decorators"; | ||
class Model { | ||
@observable({ expose: true }) | ||
field = 123; | ||
}; | ||
const model = new Model(); | ||
const hiddenObservable = model._field; // ko.observable | ||
``` | ||
<br> | ||
@@ -150,3 +163,3 @@ | ||
```js | ||
@observableArray(options: { deep: boolean }); | ||
@observableArray(options: { deep?: boolean, expose?: boolean }); | ||
@observableArray; | ||
@@ -494,3 +507,27 @@ ``` | ||
KnockoutValidation example | ||
Using `{ expose: true }`: | ||
```js | ||
import { observable, extend } from "knockout-decorators"; | ||
class MyViewModel { | ||
@extend({ required: "MyField is required" }) | ||
@observable({ expose: true }) | ||
myField = ""; | ||
checkMyField() { | ||
alert("MyField is valid: " + this._myField.isValid()); | ||
} | ||
} | ||
``` | ||
```html | ||
<div> | ||
<input type="text" data-bind="value: myField"/> | ||
<button data-bind="click: checkMyField">check</button> | ||
<p data-bind="validationMessage: _myField"></p> | ||
</div> | ||
``` | ||
Using `unwrap()`: | ||
```js | ||
import { observable, extend, unwrap } from "knockout-decorators"; | ||
@@ -510,2 +547,8 @@ | ||
} | ||
// from TypeScript 2.1 you can use keyof | ||
// to restrict to keys of the given type | ||
unwrap(key: keyof MyViewModel){ | ||
return unwrap(this, key); | ||
} | ||
} | ||
@@ -512,0 +555,0 @@ ``` |
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
Sorry, the diff of this file is not supported yet
290711
1506
632