Comparing version 0.3.3-alpha to 0.4.0-alpha
{ | ||
"name": "imvvm", | ||
"version": "0.3.3-alpha", | ||
"version": "0.4.0-alpha", | ||
"homepage": "https://github.com/entrendipity/imvvm", | ||
@@ -5,0 +5,0 @@ "authors": [ |
@@ -12,3 +12,3 @@ !function(e){if("object"==typeof exports)module.exports=e();else if("function"==typeof define&&define.amd)define(e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.IMVVM=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(_dereq_,module,exports){ | ||
exports.getInitialState = function(appNamespace, domainModel, initArgs, domain, stateChangedHandler, disableUndo) { | ||
exports.getInitialState = function(appNamespace, domainModel, stateChangedHandler, disableUndo) { | ||
@@ -23,3 +23,4 @@ if(typeof stateChangedHandler !== 'function'){ | ||
watchedProps, | ||
watchList = {}; | ||
watchList = {}, | ||
domain; | ||
@@ -72,5 +73,7 @@ disableUndo === void(0) ? false : disableUndo; | ||
if(initialize){ | ||
nextState[dataContext] = new dataContexts[dataContext](nextState[dataContext], dependencies, prevState[dataContext]).init(domain[dataContext].initArgs); | ||
nextState[dataContext] = new dataContexts[dataContext](nextState[dataContext], dependencies, | ||
prevState[dataContext]).getInitialState(); | ||
} else { | ||
nextState[dataContext] = new dataContexts[dataContext](nextState[dataContext], dependencies, prevState[dataContext]); | ||
nextState[dataContext] = new dataContexts[dataContext](nextState[dataContext], dependencies, | ||
prevState[dataContext]); | ||
} | ||
@@ -80,3 +83,4 @@ if(watchedDataContext){ | ||
dependencies = getDependencies(domain[watchedDataContext.name]); | ||
nextState[watchedDataContext.name] = new dataContexts[watchedDataContext.name](nextState[watchedDataContext.name], dependencies, prevState[watchedDataContext.name]); | ||
nextState[watchedDataContext.name] = new dataContexts[watchedDataContext.name](nextState[watchedDataContext.name], | ||
dependencies, prevState[watchedDataContext.name]); | ||
} | ||
@@ -91,4 +95,3 @@ processed = processed ? processed : dataContext === watchedDataContext.name; | ||
var appStateChangedHandler = function(caller, newState, callback, initialize) { | ||
var appContext = {}, | ||
nextState = {}, | ||
var nextState = {}, | ||
prevState = {}, | ||
@@ -136,3 +139,3 @@ watchedDataContext = void(0), | ||
nextState[caller] = newState; | ||
nextState = extend(thisAppState.state, nextState); | ||
nextState = extend(thisAppState, nextState); | ||
} else { | ||
@@ -143,7 +146,7 @@ //appDataContext is calling function | ||
} else { | ||
nextState = extend(thisAppState.state, newState); | ||
nextState = extend(thisAppState, newState); | ||
} | ||
} | ||
prevState = thisAppState.state; | ||
nextState = transitionState(nextState, thisAppState.state, watchedDataContext); | ||
prevState = thisAppState; | ||
nextState = transitionState(nextState, thisAppState, watchedDataContext); | ||
} | ||
@@ -155,15 +158,15 @@ prevState = prevState || {}; | ||
thisAppState = new ApplicationDataContext(nextState, prevState, disableUndo, initialize); | ||
appContext = thisAppState.state; | ||
Object.freeze(thisAppState); | ||
if(!initialize && !disableUndo){ | ||
appContext.previousState = thisAppState.previousState; | ||
} | ||
Object.freeze(appContext); | ||
//All the work is done! -> Notify the View | ||
stateChangedHandler(appContext, caller, callback); | ||
stateChangedHandler(thisAppState, caller, callback); | ||
//Provided for the main app to return from init() to the View | ||
return appContext; | ||
//return appContext; | ||
return thisAppState; | ||
}; | ||
ApplicationDataContext = domainModel.call(this, appStateChangedHandler.bind(this, appNamespace)); | ||
var applicationDataContext = new ApplicationDataContext({}, {}, disableUndo, true); | ||
domain = applicationDataContext.dataContexts(); | ||
for(var dataContext in domain){ | ||
@@ -186,3 +189,3 @@ if(domain.hasOwnProperty(dataContext)){ | ||
} | ||
return new ApplicationDataContext().init(initArgs); | ||
return applicationDataContext.getInitialState(); | ||
}; | ||
@@ -279,43 +282,80 @@ },{"./utils":8}],3:[function(_dereq_,module,exports){ | ||
var dataContext = function(nextState, previousState, disableUndo, initialize) { | ||
var DataContext = function(initState, callback){ | ||
return desc.proto.setState(initState, callback, true); | ||
} | ||
var dataContext = function(nextState, prevState, disableUndo, initialize) { | ||
var initFunc; | ||
var calcFld; | ||
nextState = nextState || {}; | ||
previousState = previousState || {}; | ||
if(!('DataContext' in desc.proto)){ | ||
desc.proto.DataContext = function(initState, callback){ | ||
return desc.proto.setState(initState, callback, true); | ||
} | ||
} | ||
prevState = prevState || {}; | ||
if(!('init' in desc.proto)){ | ||
desc.proto.init = function(){ | ||
return this.DataContext(); | ||
if(!('getInitialState' in desc.proto)){ | ||
desc.proto.getInitialState = function(){ | ||
return DataContext(); | ||
} | ||
} else { | ||
initFunc = desc.proto.getInitialState; | ||
desc.proto.getInitialState = function(){ | ||
return DataContext(initFunc.call(this)); | ||
} | ||
} | ||
var model = Object.create(desc.proto, desc.descriptor); | ||
Object.defineProperty(model, 'state', { | ||
configurable: true, | ||
enumerable: false, | ||
writable: true, | ||
value: nextState | ||
}); | ||
//Need to have state prop in model before can extend model to get correct state | ||
nextState = extend(nextState, model); | ||
if(desc.originalSpec.getInitialState){ | ||
nextState = extend(nextState, desc.originalSpec.getInitialState.call(model, nextState, previousState)); | ||
//runs everytime to initialize calculated state but will not run the calc func | ||
//if the prop has already been initialized | ||
if(!!desc.originalSpec.getInitialCalculatedState){ | ||
for (var i = desc.calculatedFields.length - 1; i >= 0; i--) { | ||
if(!(desc.calculatedFields[i] in nextState) || nextState[desc.calculatedFields[i]] === void(0)){ | ||
calcFld = {} | ||
calcFld[desc.calculatedFields[i]] = desc.originalSpec.getInitialCalculatedState. | ||
call(model, nextState, prevState)[desc.calculatedFields[i]]; | ||
if(calcFld[desc.calculatedFields[i]] !== void(0)){ | ||
nextState = extend(nextState,calcFld); | ||
} | ||
} | ||
}; | ||
} | ||
if(!initialize && !disableUndo){ | ||
Object.defineProperty(model, 'previousState', { | ||
configurable: false, | ||
enumerable: false, | ||
writable: false, | ||
value: previousState | ||
}); | ||
if(!initialize){ | ||
//runs everytime after initialized | ||
if(desc.originalSpec.validateState){ | ||
nextState = extend(nextState, | ||
desc.originalSpec.validateState.call(model, nextState, prevState)); | ||
} | ||
if(!disableUndo && !!Object.keys(prevState).length){ | ||
Object.defineProperty(nextState, 'previousState', { | ||
configurable: false, | ||
enumerable: false, | ||
writable: false, | ||
value: prevState | ||
}); | ||
} | ||
} | ||
//TODO - rework this, as __proto__ is deprecated | ||
nextState.__proto__ = model.__proto__; | ||
Object.defineProperty(model, 'state', { | ||
Object.defineProperty(nextState, 'state', { | ||
configurable: false, | ||
enumerable: false, | ||
writable: false, | ||
value: nextState | ||
value: extend(nextState) | ||
}); | ||
return model; | ||
for(var k in desc.descriptor){ | ||
if(desc.descriptor.hasOwnProperty(k)){ | ||
Object.defineProperty(nextState, k, desc.descriptor[k]); | ||
} | ||
} | ||
nextState.__proto__ = model.__proto__; | ||
return nextState; | ||
}; | ||
@@ -339,7 +379,7 @@ return dataContext; | ||
var desc = getDescriptor.call(this); | ||
var dataContext = function(nextState, prevState, withContext) { | ||
var model = Object.create(desc.proto, desc.descriptor); | ||
var argCount = arguments.length; | ||
var lastIsBoolean = typeof Array.prototype.slice.call(arguments, -1)[0] === 'boolean'; | ||
var lastArgIsBool = typeof Array.prototype.slice.call(arguments, -1)[0] === 'boolean'; | ||
var calcFld; | ||
@@ -352,3 +392,3 @@ if(argCount === 0){ | ||
} else if(argCount === 1){ | ||
if(lastIsBoolean){ | ||
if(lastArgIsBool){ | ||
withContext = nextState; | ||
@@ -363,3 +403,3 @@ nextState = {}; | ||
} else if(argCount === 2){ | ||
if(lastIsBoolean){ | ||
if(lastArgIsBool){ | ||
withContext = prevState; | ||
@@ -371,8 +411,35 @@ prevState = nextState; | ||
} | ||
nextState = ('state' in nextState) ? nextState.state : nextState; | ||
prevState = ('state' in prevState) ? prevState.state : prevState; | ||
//Initialize any props | ||
if(desc.originalSpec.getInitialState){ | ||
nextState = extend(nextState, desc.originalSpec.getInitialState.call(model, nextState, ('state' in prevState) ? prevState.state : prevState)); | ||
Object.defineProperty(model, 'state', { | ||
configurable: true, | ||
enumerable: false, | ||
writable: true, | ||
value: nextState | ||
}); | ||
//Need to have state prop in model before can extend model to get correct state | ||
nextState = extend(nextState, model); | ||
//runs everytime to initialize calculated state but will not run the calc func | ||
//if the prop has already been initialized | ||
if(!!desc.originalSpec.getInitialCalculatedState){ | ||
for (var i = desc.calculatedFields.length - 1; i >= 0; i--) { | ||
if(!(desc.calculatedFields[i] in nextState) || nextState[desc.calculatedFields[i]] === void(0)){ | ||
calcFld = {} | ||
calcFld[desc.calculatedFields[i]] = desc.originalSpec.getInitialCalculatedState. | ||
call(model, nextState, prevState)[desc.calculatedFields[i]]; | ||
if(calcFld[desc.calculatedFields[i]] !== void(0)){ | ||
nextState = extend(nextState,calcFld); | ||
} | ||
} | ||
}; | ||
} | ||
//runs everytime | ||
if(desc.originalSpec.validateState){ | ||
nextState = extend(nextState, | ||
desc.originalSpec.validateState.call(model, nextState, prevState)); | ||
} | ||
if(withContext){ | ||
@@ -399,2 +466,3 @@ //This will self distruct | ||
}); | ||
if(!withContext){ | ||
@@ -425,13 +493,18 @@ Object.freeze(model); | ||
var dataContext = function(nextState, dependencies, prevState) { | ||
prevState = prevState || {}; | ||
var initFunc; | ||
var calcFld; | ||
//nextState has already been extended with prevState in core | ||
nextState = extend(nextState, dependencies); | ||
prevState = prevState || {}; | ||
prevState = ('state' in prevState) ? prevState.state : prevState; | ||
desc.proto.DataContext = dataContext; | ||
if(!('init' in desc.proto)){ | ||
desc.proto.init = function(){ | ||
return this.DataContext(); | ||
if(!('getInitialState' in desc.proto)){ | ||
desc.proto.getInitialState = function(){ | ||
return dataContext(); | ||
} | ||
} else { | ||
initFunc = desc.proto.getInitialState; | ||
desc.proto.getInitialState = function(){ | ||
return dataContext(initFunc.call(this)); | ||
} | ||
} | ||
@@ -441,6 +514,32 @@ | ||
if(desc.originalSpec.getInitialState){ | ||
nextState = extend(nextState, desc.originalSpec.getInitialState.call(model, nextState, prevState)); | ||
Object.defineProperty(model, 'state', { | ||
configurable: true, | ||
enumerable: false, | ||
writable: true, | ||
value: nextState | ||
}); | ||
//Need to have state prop in model before can extend model to get correct state | ||
nextState = extend(nextState, model); | ||
//runs everytime to initialize calculated state but will not run the calc func | ||
//if the prop has already been initialized | ||
if(!!desc.originalSpec.getInitialCalculatedState){ | ||
for (var i = desc.calculatedFields.length - 1; i >= 0; i--) { | ||
if(!(desc.calculatedFields[i] in nextState) || nextState[desc.calculatedFields[i]] === void(0)){ | ||
calcFld = {} | ||
calcFld[desc.calculatedFields[i]] = desc.originalSpec.getInitialCalculatedState. | ||
call(model, nextState, prevState)[desc.calculatedFields[i]]; | ||
if(calcFld[desc.calculatedFields[i]] !== void(0)){ | ||
nextState = extend(nextState,calcFld); | ||
} | ||
} | ||
}; | ||
} | ||
//runs everytime | ||
if(desc.originalSpec.validateState){ | ||
nextState = extend(nextState, | ||
desc.originalSpec.validateState.call(model, nextState, prevState)); | ||
} | ||
Object.defineProperty(model, 'state', { | ||
@@ -463,6 +562,17 @@ configurable: false, | ||
//TODO - rework this, as __proto__ is deprecated | ||
nextState.__proto__ = model.__proto__; | ||
return Object.freeze(nextState); | ||
//Add dependencies to model | ||
for(var dep in dependencies){ | ||
if(dependencies.hasOwnProperty(dep)){ | ||
Object.defineProperty(model, dep, { | ||
configurable: false, | ||
enumerable: false, | ||
writable: false, | ||
value: dependencies[dep] | ||
}); | ||
} | ||
} | ||
Object.freeze(nextState); | ||
return model; | ||
}; | ||
@@ -507,4 +617,4 @@ return dataContext; | ||
getInitialState: function(){ | ||
var appDataContext = core.getInitialState(NAMESPACE, this.props.domainModel, this.props.initArgs, | ||
this.props.domain, this.stateChangedHandler, this.props.disableUndo); | ||
var appDataContext = core.getInitialState(NAMESPACE, this.props.domainModel, | ||
this.stateChangedHandler, this.props.disableUndo); | ||
return {applicationDataContext: appDataContext}; | ||
@@ -522,2 +632,4 @@ } | ||
var proto = this.prototype; | ||
var calcFlds = []; | ||
//var originalSpec = this.originalSpec || {}; | ||
@@ -528,7 +640,15 @@ for(var key in this.originalSpec){ | ||
//assume it is a descriptor | ||
if(!('enumerable' in this.originalSpec[key])){ | ||
if('calculated' in this.originalSpec[key]){ | ||
//default enumerable to true | ||
descriptor[key] = utils.extend(this.originalSpec[key]); | ||
descriptor[key].enumerable = !this.originalSpec[key].calculated; | ||
delete descriptor[key].calculated; | ||
calcFlds.push(key); | ||
} else if(!('enumerable' in this.originalSpec[key])){ | ||
//default enumerable to true | ||
this.originalSpec[key].enumerable = true; | ||
descriptor[key] = this.originalSpec[key]; | ||
} else { | ||
descriptor[key] = this.originalSpec[key]; | ||
} | ||
descriptor[key] = this.originalSpec[key]; | ||
} else { | ||
@@ -545,3 +665,4 @@ proto[key] = this.originalSpec[key]; | ||
proto: proto, | ||
originalSpec: this.originalSpec || {} | ||
originalSpec: this.originalSpec || {}, | ||
calculatedFields: calcFlds | ||
} | ||
@@ -548,0 +669,0 @@ }, |
@@ -1,1 +0,1 @@ | ||
!function(b){if("object"==typeof exports){module.exports=b()}else{if("function"==typeof define&&define.amd){define(b)}else{var a;"undefined"!=typeof window?a=window:"undefined"!=typeof global?a=global:"undefined"!=typeof self&&(a=self),a.IMVVM=b()}}}(function(){var d,b,a;return(function c(f,k,h){function g(n,l){if(!k[n]){if(!f[n]){var i=typeof require=="function"&&require;if(!l&&i){return i(n,!0)}if(e){return e(n,!0)}throw new Error("Cannot find module '"+n+"'")}var m=k[n]={exports:{}};f[n][0].call(m.exports,function(o){var p=f[n][1][o];return g(p?p:o)},m,m.exports,c,f,k,h)}return k[n].exports}var e=typeof require=="function"&&require;for(var j=0;j<h.length;j++){g(h[j])}return g})({1:[function(h,g,f){var e=h("./src/imvvm.js");g.exports=e},{"./src/imvvm.js":3}],2:[function(h,g,f){var e=h("./utils");var i=e.extend;f.getInitialState=function(l,q,k,r,u,y){if(typeof u!=="function"){throw new TypeError()}var x,w={},j={},v,o={};y===void (0)?false:y;var p=function(B,F,D){var G=false,E,A;F=F||{};if(B===void (0)){A=true;B={}}var C=function(I){var K={},J,H;if("dependsOn" in I){I.dependsOn.forEach(function(L){H={};J=L.property.split(".");J.forEach(function(N,M){if(M===0){H=B[N]}else{H=H?H[N]:void (0)}});if("alias" in L){K[L.alias]=H}else{K[J.join("$")]=H}})}return K};for(var z in r){if(r.hasOwnProperty(z)){E=C(r[z]);if(A){B[z]=new j[z](B[z],E,F[z]).init(r[z].initArgs)}else{B[z]=new j[z](B[z],E,F[z])}if(D){if(G&&D.subscribers.indexOf(z)!==-1){E=C(r[D.name]);B[D.name]=new j[D.name](B[D.name],E,F[D.name])}G=G?G:z===D.name}}}return B};var n=function(z,A,I,E){var H={},L={},G={},K=void (0),F,D,J;E===void (0)?false:E;if(!E&&(A===void (0)||A===null||Object.keys(A).length===0)){return}var C=!!A?Object.getPrototypeOf(A).constructor.classType==="DomainModel":false;if(C){L=i(A);G=A.previousState}else{if(z in o){F=Object.keys(A);D=F.length;J={};for(var B=0;B<D;B++){if(o[z][F[B]]){J[o[z][F[B]]]=true}}K={};K.name=z;K.subscribers=Object.keys(J);K=!!K.subscribers.length?K:void (0)}if(z!==l){L[z]=A;L=i(w.state,L)}else{if(E){L=i(p(),A)}else{L=i(w.state,A)}}G=w.state;L=p(L,w.state,K)}G=G||{};Object.freeze(G);w=new x(L,G,y,E);H=w.state;if(!E&&!y){H.previousState=w.previousState}Object.freeze(H);u(H,z,I);return H};x=q.call(this,n.bind(this,l));for(var m in r){if(r.hasOwnProperty(m)){j[m]=r[m].viewModel.call(this,n.bind(this,m));if("dependsOn" in r[m]){for(var s=0,t=r[m].dependsOn.length;s<t;s++){v=r[m].dependsOn[s].property.split(".");if(v.length>1){o[v[0]]=o[v[0]]||{};o[v[0]][v[1]]=o[v[0]][v[1]]||[];if(o[v[0]][v[1]].indexOf(m)===-1){o[v[0]][v[1]].push(m)}}}}}}return new x().init(k)}},{"./utils":8}],3:[function(f,g,j){var k=f("./imvvmModel");var p=f("./imvvmViewModel");var i=f("./imvvmDomainModel");var r=f("./mixin");var q=f("./utils");var n=q.extend;var m=q.mixInto;var l=function(){};var h=function(){};var o=function(){};m(l,k.Mixin);m(h,p.Mixin);m(o,i.Mixin);var e={createClass:function(u,v,t){var y=function(){};y.prototype=new u();y.prototype.constructor=y;var x=y;var w=function(z){var A=new x();return A.construct.apply(w,arguments)};w.componentConstructor=y;y.ConvenienceConstructor=w;w.originalSpec=t;w.type=y;y.prototype.type=y;w.classType=v;y.prototype.classType=v;return w}};var s={createModel:e.createClass.bind(this,l,"Model"),createViewModel:e.createClass.bind(this,h,"ViewModel"),createDomainModel:e.createClass.bind(this,o,"DomainModel"),mixin:r};g.exports=s},{"./imvvmDomainModel":4,"./imvvmModel":5,"./imvvmViewModel":6,"./mixin":7,"./utils":8}],4:[function(j,h,f){var e=j("./utils");var k=e.extend;var g=e.getDescriptor;var i={Mixin:{construct:function(m){var n=g.call(this);n.proto.setState=m;var l=function(s,q,p,o){s=s||{};q=q||{};if(!("DataContext" in n.proto)){n.proto.DataContext=function(t,u){return n.proto.setState(t,u,true)}}if(!("init" in n.proto)){n.proto.init=function(){return this.DataContext()}}var r=Object.create(n.proto,n.descriptor);if(n.originalSpec.getInitialState){s=k(s,n.originalSpec.getInitialState.call(r,s,q))}if(!o&&!p){Object.defineProperty(r,"previousState",{configurable:false,enumerable:false,writable:false,value:q})}s.__proto__=r.__proto__;Object.defineProperty(r,"state",{configurable:false,enumerable:false,writable:false,value:s});return r};return l}}};h.exports=i},{"./utils":8}],5:[function(i,h,f){var e=i("./utils");var k=e.extend;var g=e.getDescriptor;var j={Mixin:{construct:function(m){var n=g.call(this);var l=function(r,s,o){var q=Object.create(n.proto,n.descriptor);var t=arguments.length;var p=typeof Array.prototype.slice.call(arguments,-1)[0]==="boolean";if(t===0){r={};s={};o=true}else{if(t===1){if(p){o=r;r={};s={}}else{s=r;o=true}}else{if(t===2){if(p){o=s;s=r}else{o=true}}}}if(n.originalSpec.getInitialState){r=k(r,n.originalSpec.getInitialState.call(q,r,("state" in s)?s.state:s))}if(o){Object.defineProperty(q,"context",{configurable:true,enumerable:true,set:function(u){this.setState=function(v,w){return m.bind(u).call(u,k(this.state,v),this.state,w)}.bind(this);delete this.context}})}Object.defineProperty(q,"state",{configurable:false,enumerable:false,writable:false,value:r});if(!o){Object.freeze(q)}return q};return l}}};h.exports=j},{"./utils":8}],6:[function(j,i,g){var f=j("./utils");var k=f.extend;var h=f.getDescriptor;var e={Mixin:{construct:function(m){var n=h.call(this);n.proto.setState=m;var l=function(p,q,r){r=r||{};p=k(p,q);n.proto.DataContext=l;if(!("init" in n.proto)){n.proto.init=function(){return this.DataContext()}}var o=Object.create(n.proto,n.descriptor);if(n.originalSpec.getInitialState){p=k(p,n.originalSpec.getInitialState.call(o,p,r))}Object.defineProperty(o,"state",{configurable:false,enumerable:false,writable:false,value:p});Object.keys(o).forEach(function(s){if(Object.prototype.toString.call(this[s])==="[object Object]"&&("context" in this[s])){this[s].context=this;Object.freeze(this[s])}else{if(Object.prototype.toString.call(this[s])==="[object Array]"){Object.freeze(this[s])}}}.bind(o));p.__proto__=o.__proto__;return Object.freeze(p)};return l}}};i.exports=e},{"./utils":8}],7:[function(j,i,h){var f=j("./core");var e="__IMVVM__";var g={stateChangedHandler:function(k,l,m){this.setState({applicationDataContext:k},function(){if(typeof m==="function"){if(this.state===null||!("applicationDataContext" in this.state)){m(void (0))}else{if(l in this.state.applicationDataContext){m(this.state.applicationDataContext[l])}else{if(l===e){m(this.state.applicationDataContext)}else{m(void (0))}}}}}.bind(this))},getInitialState:function(){var k=f.getInitialState(e,this.props.domainModel,this.props.initArgs,this.props.domain,this.stateChangedHandler,this.props.disableUndo);return{applicationDataContext:k}}};i.exports=g},{"./core":2}],8:[function(h,g,f){var e={getDescriptor:function(){var k={};var j=this.prototype;for(var i in this.originalSpec){if(this.originalSpec.hasOwnProperty(i)){if("get" in this.originalSpec[i]||"set" in this.originalSpec[i]){if(!("enumerable" in this.originalSpec[i])){this.originalSpec[i].enumerable=true}k[i]=this.originalSpec[i]}else{j[i]=this.originalSpec[i]}}}if(!("extend" in j)){j.extend=e.extend}return{descriptor:k,proto:j,originalSpec:this.originalSpec||{}}},extend:function(){var j={};for(var l=0;l<arguments.length;l++){var m=arguments[l];for(var k in m){if(m.hasOwnProperty(k)){j[k]=m[k]}}}return j},mixInto:function(j,k){var i;for(i in k){if(!k.hasOwnProperty(i)){continue}j.prototype[i]=k[i]}}};g.exports=e},{}]},{},[1])(1)}); | ||
!function(b){if("object"==typeof exports){module.exports=b()}else{if("function"==typeof define&&define.amd){define(b)}else{var a;"undefined"!=typeof window?a=window:"undefined"!=typeof global?a=global:"undefined"!=typeof self&&(a=self),a.IMVVM=b()}}}(function(){var d,b,a;return(function c(f,k,h){function g(n,l){if(!k[n]){if(!f[n]){var i=typeof require=="function"&&require;if(!l&&i){return i(n,!0)}if(e){return e(n,!0)}throw new Error("Cannot find module '"+n+"'")}var m=k[n]={exports:{}};f[n][0].call(m.exports,function(o){var p=f[n][1][o];return g(p?p:o)},m,m.exports,c,f,k,h)}return k[n].exports}var e=typeof require=="function"&&require;for(var j=0;j<h.length;j++){g(h[j])}return g})({1:[function(h,g,f){var e=h("./src/imvvm.js");g.exports=e},{"./src/imvvm.js":3}],2:[function(h,g,f){var e=h("./utils");var i=e.extend;f.getInitialState=function(k,p,t,x){if(typeof t!=="function"){throw new TypeError()}var w,v={},j={},u,n={},q;x===void (0)?false:x;var o=function(B,F,D){var G=false,E,A;F=F||{};if(B===void (0)){A=true;B={}}var C=function(I){var K={},J,H;if("dependsOn" in I){I.dependsOn.forEach(function(L){H={};J=L.property.split(".");J.forEach(function(N,M){if(M===0){H=B[N]}else{H=H?H[N]:void (0)}});if("alias" in L){K[L.alias]=H}else{K[J.join("$")]=H}})}return K};for(var z in q){if(q.hasOwnProperty(z)){E=C(q[z]);if(A){B[z]=new j[z](B[z],E,F[z]).getInitialState()}else{B[z]=new j[z](B[z],E,F[z])}if(D){if(G&&D.subscribers.indexOf(z)!==-1){E=C(q[D.name]);B[D.name]=new j[D.name](B[D.name],E,F[D.name])}G=G?G:z===D.name}}}return B};var m=function(z,A,H,E){var K={},G={},J=void (0),F,D,I;E===void (0)?false:E;if(!E&&(A===void (0)||A===null||Object.keys(A).length===0)){return}var C=!!A?Object.getPrototypeOf(A).constructor.classType==="DomainModel":false;if(C){K=i(A);G=A.previousState}else{if(z in n){F=Object.keys(A);D=F.length;I={};for(var B=0;B<D;B++){if(n[z][F[B]]){I[n[z][F[B]]]=true}}J={};J.name=z;J.subscribers=Object.keys(I);J=!!J.subscribers.length?J:void (0)}if(z!==k){K[z]=A;K=i(v,K)}else{if(E){K=i(o(),A)}else{K=i(v,A)}}G=v;K=o(K,v,J)}G=G||{};Object.freeze(G);v=new w(K,G,x,E);Object.freeze(v);t(v,z,H);return v};w=p.call(this,m.bind(this,k));var y=new w({},{},x,true);q=y.dataContexts();for(var l in q){if(q.hasOwnProperty(l)){j[l]=q[l].viewModel.call(this,m.bind(this,l));if("dependsOn" in q[l]){for(var r=0,s=q[l].dependsOn.length;r<s;r++){u=q[l].dependsOn[r].property.split(".");if(u.length>1){n[u[0]]=n[u[0]]||{};n[u[0]][u[1]]=n[u[0]][u[1]]||[];if(n[u[0]][u[1]].indexOf(l)===-1){n[u[0]][u[1]].push(l)}}}}}}return y.getInitialState()}},{"./utils":8}],3:[function(f,g,j){var k=f("./imvvmModel");var p=f("./imvvmViewModel");var i=f("./imvvmDomainModel");var r=f("./mixin");var q=f("./utils");var n=q.extend;var m=q.mixInto;var l=function(){};var h=function(){};var o=function(){};m(l,k.Mixin);m(h,p.Mixin);m(o,i.Mixin);var e={createClass:function(u,v,t){var y=function(){};y.prototype=new u();y.prototype.constructor=y;var x=y;var w=function(z){var A=new x();return A.construct.apply(w,arguments)};w.componentConstructor=y;y.ConvenienceConstructor=w;w.originalSpec=t;w.type=y;y.prototype.type=y;w.classType=v;y.prototype.classType=v;return w}};var s={createModel:e.createClass.bind(this,l,"Model"),createViewModel:e.createClass.bind(this,h,"ViewModel"),createDomainModel:e.createClass.bind(this,o,"DomainModel"),mixin:r};g.exports=s},{"./imvvmDomainModel":4,"./imvvmModel":5,"./imvvmViewModel":6,"./mixin":7,"./utils":8}],4:[function(j,h,f){var e=j("./utils");var k=e.extend;var g=e.getDescriptor;var i={Mixin:{construct:function(m){var o=g.call(this);o.proto.setState=m;var n=function(p,q){return o.proto.setState(p,q,true)};var l=function(x,v,w,r){var u;var t;x=x||{};v=v||{};if(!("getInitialState" in o.proto)){o.proto.getInitialState=function(){return n()}}else{u=o.proto.getInitialState;o.proto.getInitialState=function(){return n(u.call(this))}}var s=Object.create(o.proto,o.descriptor);Object.defineProperty(s,"state",{configurable:true,enumerable:false,writable:true,value:x});x=k(x,s);if(!!o.originalSpec.getInitialCalculatedState){for(var q=o.calculatedFields.length-1;q>=0;q--){if(!(o.calculatedFields[q] in x)||x[o.calculatedFields[q]]===void (0)){t={};t[o.calculatedFields[q]]=o.originalSpec.getInitialCalculatedState.call(s,x,v)[o.calculatedFields[q]];if(t[o.calculatedFields[q]]!==void (0)){x=k(x,t)}}}}if(!r){if(o.originalSpec.validateState){x=k(x,o.originalSpec.validateState.call(s,x,v))}if(!w&&!!Object.keys(v).length){Object.defineProperty(x,"previousState",{configurable:false,enumerable:false,writable:false,value:v})}}Object.defineProperty(x,"state",{configurable:false,enumerable:false,writable:false,value:k(x)});for(var p in o.descriptor){if(o.descriptor.hasOwnProperty(p)){Object.defineProperty(x,p,o.descriptor[p])}}x.__proto__=s.__proto__;return x};return l}}};h.exports=i},{"./utils":8}],5:[function(i,h,f){var e=i("./utils");var k=e.extend;var g=e.getDescriptor;var j={Mixin:{construct:function(m){var n=g.call(this);var l=function(s,u,o){var p=Object.create(n.proto,n.descriptor);var v=arguments.length;var r=typeof Array.prototype.slice.call(arguments,-1)[0]==="boolean";var t;if(v===0){s={};u={};o=true}else{if(v===1){if(r){o=s;s={};u={}}else{u=s;o=true}}else{if(v===2){if(r){o=u;u=s}else{o=true}}}}s=("state" in s)?s.state:s;u=("state" in u)?u.state:u;Object.defineProperty(p,"state",{configurable:true,enumerable:false,writable:true,value:s});s=k(s,p);if(!!n.originalSpec.getInitialCalculatedState){for(var q=n.calculatedFields.length-1;q>=0;q--){if(!(n.calculatedFields[q] in s)||s[n.calculatedFields[q]]===void (0)){t={};t[n.calculatedFields[q]]=n.originalSpec.getInitialCalculatedState.call(p,s,u)[n.calculatedFields[q]];if(t[n.calculatedFields[q]]!==void (0)){s=k(s,t)}}}}if(n.originalSpec.validateState){s=k(s,n.originalSpec.validateState.call(p,s,u))}if(o){Object.defineProperty(p,"context",{configurable:true,enumerable:true,set:function(w){this.setState=function(x,y){return m.bind(w).call(w,k(this.state,x),this.state,y)}.bind(this);delete this.context}})}Object.defineProperty(p,"state",{configurable:false,enumerable:false,writable:false,value:s});if(!o){Object.freeze(p)}return p};return l}}};h.exports=j},{"./utils":8}],6:[function(j,i,g){var f=j("./utils");var k=f.extend;var h=f.getDescriptor;var e={Mixin:{construct:function(m){var n=h.call(this);n.proto.setState=m;var l=function(q,r,u){var v;var t;q=k(q,r);u=u||{};u=("state" in u)?u.state:u;if(!("getInitialState" in n.proto)){n.proto.getInitialState=function(){return l()}}else{v=n.proto.getInitialState;n.proto.getInitialState=function(){return l(v.call(this))}}var o=Object.create(n.proto,n.descriptor);Object.defineProperty(o,"state",{configurable:true,enumerable:false,writable:true,value:q});q=k(q,o);if(!!n.originalSpec.getInitialCalculatedState){for(var p=n.calculatedFields.length-1;p>=0;p--){if(!(n.calculatedFields[p] in q)||q[n.calculatedFields[p]]===void (0)){t={};t[n.calculatedFields[p]]=n.originalSpec.getInitialCalculatedState.call(o,q,u)[n.calculatedFields[p]];if(t[n.calculatedFields[p]]!==void (0)){q=k(q,t)}}}}if(n.originalSpec.validateState){q=k(q,n.originalSpec.validateState.call(o,q,u))}Object.defineProperty(o,"state",{configurable:false,enumerable:false,writable:false,value:q});Object.keys(o).forEach(function(w){if(Object.prototype.toString.call(this[w])==="[object Object]"&&("context" in this[w])){this[w].context=this;Object.freeze(this[w])}else{if(Object.prototype.toString.call(this[w])==="[object Array]"){Object.freeze(this[w])}}}.bind(o));for(var s in r){if(r.hasOwnProperty(s)){Object.defineProperty(o,s,{configurable:false,enumerable:false,writable:false,value:r[s]})}}Object.freeze(q);return o};return l}}};i.exports=e},{"./utils":8}],7:[function(j,i,h){var f=j("./core");var e="__IMVVM__";var g={stateChangedHandler:function(k,l,m){this.setState({applicationDataContext:k},function(){if(typeof m==="function"){if(this.state===null||!("applicationDataContext" in this.state)){m(void (0))}else{if(l in this.state.applicationDataContext){m(this.state.applicationDataContext[l])}else{if(l===e){m(this.state.applicationDataContext)}else{m(void (0))}}}}}.bind(this))},getInitialState:function(){var k=f.getInitialState(e,this.props.domainModel,this.stateChangedHandler,this.props.disableUndo);return{applicationDataContext:k}}};i.exports=g},{"./core":2}],8:[function(h,g,f){var e={getDescriptor:function(){var l={};var k=this.prototype;var i=[];for(var j in this.originalSpec){if(this.originalSpec.hasOwnProperty(j)){if("get" in this.originalSpec[j]||"set" in this.originalSpec[j]){if("calculated" in this.originalSpec[j]){l[j]=e.extend(this.originalSpec[j]);l[j].enumerable=!this.originalSpec[j].calculated;delete l[j].calculated;i.push(j)}else{if(!("enumerable" in this.originalSpec[j])){this.originalSpec[j].enumerable=true;l[j]=this.originalSpec[j]}else{l[j]=this.originalSpec[j]}}}else{k[j]=this.originalSpec[j]}}}if(!("extend" in k)){k.extend=e.extend}return{descriptor:l,proto:k,originalSpec:this.originalSpec||{},calculatedFields:i}},extend:function(){var j={};for(var l=0;l<arguments.length;l++){var m=arguments[l];for(var k in m){if(m.hasOwnProperty(k)){j[k]=m[k]}}}return j},mixInto:function(j,k){var i;for(i in k){if(!k.hasOwnProperty(i)){continue}j.prototype[i]=k[i]}}};g.exports=e},{}]},{},[1])(1)}); |
@@ -8,16 +8,10 @@ /*jshint unused: vars */ | ||
var DomainModel = IMVVM.createDomainModel({ | ||
getInitialState: function(nextState, prevState){ //Optional | ||
getInitialState: function(){ //optional | ||
return { | ||
online: typeof nextState.online === 'boolean' ? nextState.online : false, | ||
busy: nextState.busy === void(0) ? false : nextState.busy | ||
online: true, | ||
busy: false | ||
}; | ||
}, | ||
init: function(args){ //optional | ||
var nextState = {}; | ||
nextState.online = true; | ||
return this.DataContext(this.extend(args,nextState)); | ||
}, | ||
undo: function(){ | ||
@@ -27,5 +21,27 @@ this.setState(this.previousState); | ||
setBusyTo: function(val){ | ||
this.setState({busy: val}); | ||
busy: { | ||
get: function(){ | ||
return this.state.busy; | ||
}, | ||
set: function(newValue){ | ||
this.setState({'busy': newValue }); | ||
} | ||
}, | ||
dataContexts: function(){ | ||
return { | ||
hobbies: { | ||
viewModel: HobbiesViewModel, | ||
initArgs : [], | ||
dependsOn: [{property: 'persons.selected'}, | ||
{property: 'busy', alias: 'appIsBusy'}] | ||
}, | ||
persons: { | ||
viewModel: PersonsViewModel, | ||
initArgs : [], | ||
dependsOn: [{property: 'hobbies.selected', alias: 'selectedHobby'}, | ||
{property: 'online', alias: 'imOnline'}] | ||
} | ||
} | ||
} | ||
}); |
@@ -16,4 +16,2 @@ /** | ||
domainModel={DomainModel} | ||
initArgs={{appName: 'IMVVM Demo'}} | ||
domain={domainObjects} | ||
disableUndo={false} />, | ||
@@ -20,0 +18,0 @@ document.getElementById('container')); |
@@ -43,5 +43,4 @@ /*jshint unused: false */ | ||
getInitialState: function(nextState, prevState){ | ||
getInitialCalculatedState: function(nextState, prevState){ | ||
return { | ||
id: nextState.id ? nextState.id : this.uuid(), | ||
age: this.calculateAge(nextState.dob), | ||
@@ -53,3 +52,3 @@ }; | ||
get: function(){ | ||
return this.state.id; | ||
return this.state.id ? this.state.id : this.uuid(); | ||
} | ||
@@ -78,2 +77,4 @@ }, | ||
enumerable: false, //calculated fields should set enumerable to false | ||
//calculated: true, //Only set this if you getInitialCalculatedState | ||
//otherwise you must set enumerable to false - one or the other | ||
get: function(){ | ||
@@ -124,3 +125,3 @@ if(this.lastName === void(0)){ | ||
age: { | ||
enumerable: false, | ||
calculated: true, | ||
get: function(){ | ||
@@ -127,0 +128,0 @@ return this.state.age; |
@@ -34,6 +34,5 @@ /*jshint unused: false */ | ||
getInitialState: function(nextState, prevState){ | ||
validateState: function(nextState, prevState){ | ||
return { | ||
selected: this.resetSelected(nextState, prevState), | ||
appIsBusy: nextState.appIsBusy === void(0) ? false : nextState.appIsBusy | ||
}; | ||
@@ -40,0 +39,0 @@ }, |
@@ -8,85 +8,86 @@ /*jshint unused: false */ | ||
select: function(id/*, callback*/){ | ||
var nextState = {}; | ||
nextState.collection = this.collection.map(function(person){ | ||
if(person.id === id){ | ||
nextState.selected = this.Person(person); | ||
return nextState.selected; | ||
} | ||
return person; | ||
}.bind(this)); | ||
select: function(id){ | ||
console.log(this); | ||
var nextState = {}; | ||
nextState.collection = this.collection.map(function(person){ | ||
if(person.id === id){ | ||
nextState.selected = this.Person(person); | ||
return nextState.selected; | ||
} | ||
return person; | ||
}.bind(this)); | ||
this.setState(nextState); | ||
}, | ||
this.setState(nextState); | ||
}, | ||
addPerson: function(value){ | ||
var nextState = {}; | ||
var name; | ||
addPerson: function(value){ | ||
var nextState = {}; | ||
var name; | ||
if(value && value.length > 0){ | ||
name = value.split(' '); | ||
//Cannot initialize by passing in calculated prop value | ||
//i.e. fullname | ||
nextState.selected = this.Person({ | ||
firstName: name[0], | ||
lastName: name.slice(1).join(' ') | ||
}); | ||
nextState.collection = this.collection.slice(0); | ||
nextState.collection = nextState.collection.concat(nextState.selected); | ||
this.setState(nextState); | ||
} | ||
}, | ||
if(value && value.length > 0){ | ||
name = value.split(' '); | ||
//Cannot initialize by passing in calculated prop value | ||
//i.e. fullname | ||
nextState.selected = this.Person({ | ||
firstName: name[0], | ||
lastName: name.slice(1).join(' ') | ||
}); | ||
nextState.collection = this.collection.slice(0); | ||
nextState.collection = nextState.collection.concat(nextState.selected); | ||
this.setState(nextState); | ||
} | ||
}, | ||
deletePerson: function(uid){ | ||
var nextState = {}; | ||
nextState.collection = this.collection.filter(function(person){ | ||
return person.id !== uid; | ||
}); | ||
nextState.selected = void(0); | ||
if(nextState.collection.length > 0){ | ||
if (this.selected.id === uid){ | ||
nextState.selected = this.Person(nextState.collection[0]); | ||
} else { | ||
nextState.selected = this.Person(this.selected); | ||
} | ||
} | ||
this.setState(nextState); | ||
}, | ||
deletePerson: function(uid){ | ||
var nextState = {}; | ||
nextState.collection = this.collection.filter(function(person){ | ||
return person.id !== uid; | ||
}); | ||
nextState.selected = void(0); | ||
if(nextState.collection.length > 0){ | ||
if (this.selected.id === uid){ | ||
nextState.selected = this.Person(nextState.collection[0]); | ||
} else { | ||
nextState.selected = this.Person(this.selected); | ||
} | ||
} | ||
this.setState(nextState); | ||
}, | ||
init: function(/*args*/){ | ||
var nextState = {}; | ||
nextState.collection = DataService.getData().map(function(person, idx){ | ||
if (idx === 0){ | ||
nextState.selected = this.Person(person); | ||
return nextState.selected; | ||
} | ||
return this.Person(person, false); | ||
}.bind(this)); | ||
return this.DataContext(nextState); | ||
}, | ||
getInitialState: function(){ | ||
var nextState = {}; | ||
nextState.collection = DataService.getData().map(function(person, idx){ | ||
if (idx === 0){ | ||
nextState.selected = this.Person(person); | ||
return nextState.selected; | ||
} | ||
return this.Person(person, false); | ||
}.bind(this)); | ||
return nextState; | ||
}, | ||
personStateChangedHandler: function(nextState, prevState/*, callback*/){ | ||
var persons = {}; | ||
persons.collection = this.collection.map(function(person){ | ||
if(person.id === prevState.id){ | ||
persons.selected = this.Person(nextState, person); | ||
return persons.selected; | ||
} | ||
return person; | ||
}.bind(this)); | ||
this.setState(persons); | ||
}, | ||
personStateChangedHandler: function(nextState, prevState/*, callback*/){ | ||
var persons = {}; | ||
persons.collection = this.collection.map(function(person){ | ||
if(person.id === prevState.id){ | ||
persons.selected = this.Person(nextState, person); | ||
return persons.selected; | ||
} | ||
return person; | ||
}.bind(this)); | ||
this.setState(persons); | ||
}, | ||
Person: function(){ | ||
return new PersonModel(this.personStateChangedHandler).apply(this, arguments); | ||
}, | ||
Person: function(){ | ||
return new PersonModel(this.personStateChangedHandler).apply(this, arguments); | ||
}, | ||
collection: { | ||
collection: { | ||
get: function(){ return this.state.collection; }, | ||
}, | ||
selected: { | ||
get: function() { return this.state.selected; } | ||
}, | ||
selected: { | ||
get: function() { return this.state.selected; } | ||
}, | ||
}); |
@@ -12,26 +12,25 @@ /** | ||
var ApplicationView = React.createClass({ | ||
mixins: [IMVVM.mixin], | ||
render: function(){ | ||
mixins: [IMVVM.mixin], | ||
render: function(){ | ||
console.log('------------------------------------------ Current Application State ------------------------------------------') | ||
console.log(this.state.applicationDataContext); | ||
return ( | ||
<div> | ||
<NavBarView appContext={this.state.applicationDataContext} /> | ||
<div className="container"> | ||
<div className="row"> | ||
<div className="col-md-4"> | ||
<SideBarView appContext={this.state.applicationDataContext} /> | ||
</div> | ||
<div className="col-md-8"> | ||
<DetailsView appContext={this.state.applicationDataContext} /> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
} | ||
}); | ||
console.log('------------------------------------------ Current Application State ------------------------------------------') | ||
console.log(this.state.applicationDataContext); | ||
return ( | ||
<div> | ||
<NavBarView appContext={this.state.applicationDataContext} /> | ||
<div className="container"> | ||
<div className="row"> | ||
<div className="col-md-4"> | ||
<SideBarView appContext={this.state.applicationDataContext} /> | ||
</div> | ||
<div className="col-md-8"> | ||
<DetailsView appContext={this.state.applicationDataContext} /> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
} | ||
}); |
@@ -23,3 +23,3 @@ /** | ||
e.stopPropagation(); | ||
this.props.appContext.setBusyTo(!this.props.appContext.busy); | ||
this.props.appContext.busy = !this.props.appContext.busy; | ||
}, | ||
@@ -26,0 +26,0 @@ render: function(){ |
{ | ||
"name": "imvvm", | ||
"description": "Immutable MVVM for React", | ||
"version": "0.3.3-alpha", | ||
"version": "0.4.0-alpha", | ||
"keywords": [ | ||
@@ -6,0 +6,0 @@ "imvvm", |
@@ -5,3 +5,3 @@ | ||
exports.getInitialState = function(appNamespace, domainModel, initArgs, domain, stateChangedHandler, disableUndo) { | ||
exports.getInitialState = function(appNamespace, domainModel, stateChangedHandler, disableUndo) { | ||
@@ -16,3 +16,4 @@ if(typeof stateChangedHandler !== 'function'){ | ||
watchedProps, | ||
watchList = {}; | ||
watchList = {}, | ||
domain; | ||
@@ -65,5 +66,7 @@ disableUndo === void(0) ? false : disableUndo; | ||
if(initialize){ | ||
nextState[dataContext] = new dataContexts[dataContext](nextState[dataContext], dependencies, prevState[dataContext]).init(domain[dataContext].initArgs); | ||
nextState[dataContext] = new dataContexts[dataContext](nextState[dataContext], dependencies, | ||
prevState[dataContext]).getInitialState(); | ||
} else { | ||
nextState[dataContext] = new dataContexts[dataContext](nextState[dataContext], dependencies, prevState[dataContext]); | ||
nextState[dataContext] = new dataContexts[dataContext](nextState[dataContext], dependencies, | ||
prevState[dataContext]); | ||
} | ||
@@ -73,3 +76,4 @@ if(watchedDataContext){ | ||
dependencies = getDependencies(domain[watchedDataContext.name]); | ||
nextState[watchedDataContext.name] = new dataContexts[watchedDataContext.name](nextState[watchedDataContext.name], dependencies, prevState[watchedDataContext.name]); | ||
nextState[watchedDataContext.name] = new dataContexts[watchedDataContext.name](nextState[watchedDataContext.name], | ||
dependencies, prevState[watchedDataContext.name]); | ||
} | ||
@@ -84,4 +88,3 @@ processed = processed ? processed : dataContext === watchedDataContext.name; | ||
var appStateChangedHandler = function(caller, newState, callback, initialize) { | ||
var appContext = {}, | ||
nextState = {}, | ||
var nextState = {}, | ||
prevState = {}, | ||
@@ -129,3 +132,3 @@ watchedDataContext = void(0), | ||
nextState[caller] = newState; | ||
nextState = extend(thisAppState.state, nextState); | ||
nextState = extend(thisAppState, nextState); | ||
} else { | ||
@@ -136,7 +139,7 @@ //appDataContext is calling function | ||
} else { | ||
nextState = extend(thisAppState.state, newState); | ||
nextState = extend(thisAppState, newState); | ||
} | ||
} | ||
prevState = thisAppState.state; | ||
nextState = transitionState(nextState, thisAppState.state, watchedDataContext); | ||
prevState = thisAppState; | ||
nextState = transitionState(nextState, thisAppState, watchedDataContext); | ||
} | ||
@@ -148,15 +151,15 @@ prevState = prevState || {}; | ||
thisAppState = new ApplicationDataContext(nextState, prevState, disableUndo, initialize); | ||
appContext = thisAppState.state; | ||
Object.freeze(thisAppState); | ||
if(!initialize && !disableUndo){ | ||
appContext.previousState = thisAppState.previousState; | ||
} | ||
Object.freeze(appContext); | ||
//All the work is done! -> Notify the View | ||
stateChangedHandler(appContext, caller, callback); | ||
stateChangedHandler(thisAppState, caller, callback); | ||
//Provided for the main app to return from init() to the View | ||
return appContext; | ||
//return appContext; | ||
return thisAppState; | ||
}; | ||
ApplicationDataContext = domainModel.call(this, appStateChangedHandler.bind(this, appNamespace)); | ||
var applicationDataContext = new ApplicationDataContext({}, {}, disableUndo, true); | ||
domain = applicationDataContext.dataContexts(); | ||
for(var dataContext in domain){ | ||
@@ -179,3 +182,3 @@ if(domain.hasOwnProperty(dataContext)){ | ||
} | ||
return new ApplicationDataContext().init(initArgs); | ||
return applicationDataContext.getInitialState(); | ||
}; |
@@ -12,43 +12,80 @@ | ||
var dataContext = function(nextState, previousState, disableUndo, initialize) { | ||
var DataContext = function(initState, callback){ | ||
return desc.proto.setState(initState, callback, true); | ||
} | ||
var dataContext = function(nextState, prevState, disableUndo, initialize) { | ||
var initFunc; | ||
var calcFld; | ||
nextState = nextState || {}; | ||
previousState = previousState || {}; | ||
if(!('DataContext' in desc.proto)){ | ||
desc.proto.DataContext = function(initState, callback){ | ||
return desc.proto.setState(initState, callback, true); | ||
} | ||
} | ||
prevState = prevState || {}; | ||
if(!('init' in desc.proto)){ | ||
desc.proto.init = function(){ | ||
return this.DataContext(); | ||
if(!('getInitialState' in desc.proto)){ | ||
desc.proto.getInitialState = function(){ | ||
return DataContext(); | ||
} | ||
} else { | ||
initFunc = desc.proto.getInitialState; | ||
desc.proto.getInitialState = function(){ | ||
return DataContext(initFunc.call(this)); | ||
} | ||
} | ||
var model = Object.create(desc.proto, desc.descriptor); | ||
Object.defineProperty(model, 'state', { | ||
configurable: true, | ||
enumerable: false, | ||
writable: true, | ||
value: nextState | ||
}); | ||
//Need to have state prop in model before can extend model to get correct state | ||
nextState = extend(nextState, model); | ||
if(desc.originalSpec.getInitialState){ | ||
nextState = extend(nextState, desc.originalSpec.getInitialState.call(model, nextState, previousState)); | ||
//runs everytime to initialize calculated state but will not run the calc func | ||
//if the prop has already been initialized | ||
if(!!desc.originalSpec.getInitialCalculatedState){ | ||
for (var i = desc.calculatedFields.length - 1; i >= 0; i--) { | ||
if(!(desc.calculatedFields[i] in nextState) || nextState[desc.calculatedFields[i]] === void(0)){ | ||
calcFld = {} | ||
calcFld[desc.calculatedFields[i]] = desc.originalSpec.getInitialCalculatedState. | ||
call(model, nextState, prevState)[desc.calculatedFields[i]]; | ||
if(calcFld[desc.calculatedFields[i]] !== void(0)){ | ||
nextState = extend(nextState,calcFld); | ||
} | ||
} | ||
}; | ||
} | ||
if(!initialize && !disableUndo){ | ||
Object.defineProperty(model, 'previousState', { | ||
configurable: false, | ||
enumerable: false, | ||
writable: false, | ||
value: previousState | ||
}); | ||
if(!initialize){ | ||
//runs everytime after initialized | ||
if(desc.originalSpec.validateState){ | ||
nextState = extend(nextState, | ||
desc.originalSpec.validateState.call(model, nextState, prevState)); | ||
} | ||
if(!disableUndo && !!Object.keys(prevState).length){ | ||
Object.defineProperty(nextState, 'previousState', { | ||
configurable: false, | ||
enumerable: false, | ||
writable: false, | ||
value: prevState | ||
}); | ||
} | ||
} | ||
//TODO - rework this, as __proto__ is deprecated | ||
nextState.__proto__ = model.__proto__; | ||
Object.defineProperty(model, 'state', { | ||
Object.defineProperty(nextState, 'state', { | ||
configurable: false, | ||
enumerable: false, | ||
writable: false, | ||
value: nextState | ||
value: extend(nextState) | ||
}); | ||
return model; | ||
for(var k in desc.descriptor){ | ||
if(desc.descriptor.hasOwnProperty(k)){ | ||
Object.defineProperty(nextState, k, desc.descriptor[k]); | ||
} | ||
} | ||
nextState.__proto__ = model.__proto__; | ||
return nextState; | ||
}; | ||
@@ -55,0 +92,0 @@ return dataContext; |
@@ -10,7 +10,7 @@ | ||
var desc = getDescriptor.call(this); | ||
var dataContext = function(nextState, prevState, withContext) { | ||
var model = Object.create(desc.proto, desc.descriptor); | ||
var argCount = arguments.length; | ||
var lastIsBoolean = typeof Array.prototype.slice.call(arguments, -1)[0] === 'boolean'; | ||
var lastArgIsBool = typeof Array.prototype.slice.call(arguments, -1)[0] === 'boolean'; | ||
var calcFld; | ||
@@ -23,3 +23,3 @@ if(argCount === 0){ | ||
} else if(argCount === 1){ | ||
if(lastIsBoolean){ | ||
if(lastArgIsBool){ | ||
withContext = nextState; | ||
@@ -34,3 +34,3 @@ nextState = {}; | ||
} else if(argCount === 2){ | ||
if(lastIsBoolean){ | ||
if(lastArgIsBool){ | ||
withContext = prevState; | ||
@@ -42,8 +42,35 @@ prevState = nextState; | ||
} | ||
nextState = ('state' in nextState) ? nextState.state : nextState; | ||
prevState = ('state' in prevState) ? prevState.state : prevState; | ||
//Initialize any props | ||
if(desc.originalSpec.getInitialState){ | ||
nextState = extend(nextState, desc.originalSpec.getInitialState.call(model, nextState, ('state' in prevState) ? prevState.state : prevState)); | ||
Object.defineProperty(model, 'state', { | ||
configurable: true, | ||
enumerable: false, | ||
writable: true, | ||
value: nextState | ||
}); | ||
//Need to have state prop in model before can extend model to get correct state | ||
nextState = extend(nextState, model); | ||
//runs everytime to initialize calculated state but will not run the calc func | ||
//if the prop has already been initialized | ||
if(!!desc.originalSpec.getInitialCalculatedState){ | ||
for (var i = desc.calculatedFields.length - 1; i >= 0; i--) { | ||
if(!(desc.calculatedFields[i] in nextState) || nextState[desc.calculatedFields[i]] === void(0)){ | ||
calcFld = {} | ||
calcFld[desc.calculatedFields[i]] = desc.originalSpec.getInitialCalculatedState. | ||
call(model, nextState, prevState)[desc.calculatedFields[i]]; | ||
if(calcFld[desc.calculatedFields[i]] !== void(0)){ | ||
nextState = extend(nextState,calcFld); | ||
} | ||
} | ||
}; | ||
} | ||
//runs everytime | ||
if(desc.originalSpec.validateState){ | ||
nextState = extend(nextState, | ||
desc.originalSpec.validateState.call(model, nextState, prevState)); | ||
} | ||
if(withContext){ | ||
@@ -70,2 +97,3 @@ //This will self distruct | ||
}); | ||
if(!withContext){ | ||
@@ -72,0 +100,0 @@ Object.freeze(model); |
@@ -13,13 +13,18 @@ | ||
var dataContext = function(nextState, dependencies, prevState) { | ||
prevState = prevState || {}; | ||
var initFunc; | ||
var calcFld; | ||
//nextState has already been extended with prevState in core | ||
nextState = extend(nextState, dependencies); | ||
prevState = prevState || {}; | ||
prevState = ('state' in prevState) ? prevState.state : prevState; | ||
desc.proto.DataContext = dataContext; | ||
if(!('init' in desc.proto)){ | ||
desc.proto.init = function(){ | ||
return this.DataContext(); | ||
if(!('getInitialState' in desc.proto)){ | ||
desc.proto.getInitialState = function(){ | ||
return dataContext(); | ||
} | ||
} else { | ||
initFunc = desc.proto.getInitialState; | ||
desc.proto.getInitialState = function(){ | ||
return dataContext(initFunc.call(this)); | ||
} | ||
} | ||
@@ -29,6 +34,32 @@ | ||
if(desc.originalSpec.getInitialState){ | ||
nextState = extend(nextState, desc.originalSpec.getInitialState.call(model, nextState, prevState)); | ||
Object.defineProperty(model, 'state', { | ||
configurable: true, | ||
enumerable: false, | ||
writable: true, | ||
value: nextState | ||
}); | ||
//Need to have state prop in model before can extend model to get correct state | ||
nextState = extend(nextState, model); | ||
//runs everytime to initialize calculated state but will not run the calc func | ||
//if the prop has already been initialized | ||
if(!!desc.originalSpec.getInitialCalculatedState){ | ||
for (var i = desc.calculatedFields.length - 1; i >= 0; i--) { | ||
if(!(desc.calculatedFields[i] in nextState) || nextState[desc.calculatedFields[i]] === void(0)){ | ||
calcFld = {} | ||
calcFld[desc.calculatedFields[i]] = desc.originalSpec.getInitialCalculatedState. | ||
call(model, nextState, prevState)[desc.calculatedFields[i]]; | ||
if(calcFld[desc.calculatedFields[i]] !== void(0)){ | ||
nextState = extend(nextState,calcFld); | ||
} | ||
} | ||
}; | ||
} | ||
//runs everytime | ||
if(desc.originalSpec.validateState){ | ||
nextState = extend(nextState, | ||
desc.originalSpec.validateState.call(model, nextState, prevState)); | ||
} | ||
Object.defineProperty(model, 'state', { | ||
@@ -51,6 +82,17 @@ configurable: false, | ||
//TODO - rework this, as __proto__ is deprecated | ||
nextState.__proto__ = model.__proto__; | ||
return Object.freeze(nextState); | ||
//Add dependencies to model | ||
for(var dep in dependencies){ | ||
if(dependencies.hasOwnProperty(dep)){ | ||
Object.defineProperty(model, dep, { | ||
configurable: false, | ||
enumerable: false, | ||
writable: false, | ||
value: dependencies[dep] | ||
}); | ||
} | ||
} | ||
Object.freeze(nextState); | ||
return model; | ||
}; | ||
@@ -57,0 +99,0 @@ return dataContext; |
@@ -31,4 +31,4 @@ | ||
getInitialState: function(){ | ||
var appDataContext = core.getInitialState(NAMESPACE, this.props.domainModel, this.props.initArgs, | ||
this.props.domain, this.stateChangedHandler, this.props.disableUndo); | ||
var appDataContext = core.getInitialState(NAMESPACE, this.props.domainModel, | ||
this.stateChangedHandler, this.props.disableUndo); | ||
return {applicationDataContext: appDataContext}; | ||
@@ -35,0 +35,0 @@ } |
@@ -6,2 +6,4 @@ | ||
var proto = this.prototype; | ||
var calcFlds = []; | ||
//var originalSpec = this.originalSpec || {}; | ||
@@ -12,7 +14,15 @@ for(var key in this.originalSpec){ | ||
//assume it is a descriptor | ||
if(!('enumerable' in this.originalSpec[key])){ | ||
if('calculated' in this.originalSpec[key]){ | ||
//default enumerable to true | ||
descriptor[key] = utils.extend(this.originalSpec[key]); | ||
descriptor[key].enumerable = !this.originalSpec[key].calculated; | ||
delete descriptor[key].calculated; | ||
calcFlds.push(key); | ||
} else if(!('enumerable' in this.originalSpec[key])){ | ||
//default enumerable to true | ||
this.originalSpec[key].enumerable = true; | ||
descriptor[key] = this.originalSpec[key]; | ||
} else { | ||
descriptor[key] = this.originalSpec[key]; | ||
} | ||
descriptor[key] = this.originalSpec[key]; | ||
} else { | ||
@@ -29,3 +39,4 @@ proto[key] = this.originalSpec[key]; | ||
proto: proto, | ||
originalSpec: this.originalSpec || {} | ||
originalSpec: this.originalSpec || {}, | ||
calculatedFields: calcFlds | ||
} | ||
@@ -32,0 +43,0 @@ }, |
Sorry, the diff of this file is not supported yet
132747
2265
48