Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

further refactors model.js to improve readability

  • Loading branch information...
commit 96df37577a32bba68d258eafcce642119f44b848 1 parent 743a511
@semmypurewal authored
Showing with 26 additions and 46 deletions.
  1. +1 −1  build/jermaine-min.js
  2. +12 −22 build/jermaine.js
  3. +13 −23 src/core/model.js
View
2  build/jermaine-min.js
@@ -9,4 +9,4 @@ if(!Array.prototype.indexOf){Array.prototype.indexOf=function(c){if(this==null){
*
* + See issue 24 on github
*/
-window.jermaine.util.namespace("window.jermaine",function(a){var b=function(f){var h=[],l=this,m="invalid setter call for "+f,j,k,e,c,o=false,d,n={},p=window.jermaine.AttrList,g=window.jermaine.Validator;if(f===undefined||typeof(f)!=="string"){throw new Error("Attr: constructor requires a name parameter which must be a string")}d=function(i){for(k=0;k<h.length;++k){h[k](i)}return true};this.validatesWith=function(i){if(typeof(i)==="function"){h.push(new g(i));return this}else{throw new Error("Attr: validator must be a function")}};this.defaultsTo=function(i){j=i;return this};this.isReadOnly=function(){o=true;return this};this.isWritable=function(){o=false;return this};this.on=function(i,q){if(i!=="set"&&i!=="get"){throw new Error("Attr: first argument to the 'on' method should be 'set' or 'get'")}else{if(typeof(q)!=="function"){throw new Error("Attr: second argument to the 'on' method should be a function")}else{n[i]=q}}};this.name=function(){return f};this.validator=function(){return d};this.and=this;this.which=this;this.isImmutable=this.isReadOnly;this.isMutable=this.isWritable;this.clone=function(){var q,r;q=this instanceof p?new p(f):new b(f);for(r=0;r<h.length;++r){q.validatesWith(h[r])}q.defaultsTo(j);if(o){q.isImmutable()}return q};this.addTo=function(s){var q,r,i;if(!s||typeof(s)!=="object"){throw new Error("Attr: addAttr method requires an object parameter")}s[f]=function(u){var t;if(u!==undefined){if(o&&q!==undefined){throw new Error("cannot set the immutable property "+f+" after it has been set")}else{if(!d(u)){throw new Error(m)}else{t=q;q=u;if(n.set!==undefined){n.set.call(s,u,t)}}}return s}else{if(n.get!==undefined){n.get.call(s,q)}return q}};i=typeof(j)==="function"?j():j;if(i!==undefined&&d(i)){s[f](i)}else{if(i!==undefined&&!d(i)){throw new Error("Attr: Default value of "+i+" does not pass validation for "+f)}}};c=function(i){l[i]=function(q){h.push(g.getValidator(i)(q));return l}};for(k=0;k<g.validators().length;++k){c(g.validators()[k])}};a.Attr=b});window.jermaine.util.namespace("window.jermaine",function(a){function b(c){var e=this;a.Attr.call(this,c);var d=function(g,f){return function(){return g[f].apply(g,arguments)}};this.validateWith=this.validatesWith;this.defaultsTo=function(){};this.isImmutable=function(){};this.isMutable=function(){};this.eachOfWhich=this;this.addTo=function(h){var i,f=[],g={};if(!h||typeof(h)!=="object"){throw new Error("AttrList: addTo method requires an object parameter")}else{g.pop=d(f,"pop");g.add=function(j){if((e.validator())(j)){f.push(j);return this}else{throw new Error(e.errorMessage())}};g.replace=function(j,k){if((typeof(j)!=="number")||(parseInt(j,10)!==j)){throw new Error("AttrList: replace method requires index parameter to be an integer")}if(j<0||j>=this.size()){throw new Error("AttrList: replace method index parameter out of bounds")}if(!(e.validator())(k)){throw new Error(e.errorMessage())}f[j]=k;return this};g.at=function(j){if(j<0||j>=this.size()){throw new Error("AttrList: Index out of bounds")}return f[j]};g.get=g.at;g.size=function(){return f.length};g.toJSON=function(m){var k=[],n,l;if(m!==undefined){for(n=0;n<m.length;++n){if(m[n].object===this){k=m[n].JSONrep}}}for(n=0;n<f.length;++n){if(f[n].toJSON){k.push(f[n].toJSON(m))}else{k.push(f[n])}}return k};h[c]=function(){return g}}}}b.prototype=new window.jermaine.Attr(name);a.AttrList=b});window.jermaine.util.namespace("window.jermaine",function(a){var b=function(c,d){if(!c||typeof(c)!=="string"){throw new Error("Method: constructor requires a name parameter which must be a string")}else{if(!d||typeof(d)!=="function"){throw new Error("Method: second parameter must be a function")}}this.addTo=function(e){if(!e||typeof(e)!=="object"){throw new Error("Method: addTo method requires an object parameter")}e[c]=d}};a.Method=b});window.jermaine.util.namespace("window.jermaine",function(a){var c=function(d){};var b=function(w){var j=this,s={},k={},r,g=true,e=[],p=[],t=[],d=a.Method,v=a.Attr,n=a.AttrList,i=a.util.EventEmitter,h,x,m,l,u=function(){},q=function(){},f=function(){if(g){f.validate();m()}return q.apply(this,arguments)};if(arguments.length>1){w=arguments[arguments.length-1]}if(w&&typeof(w)==="function"){f=new b();w.call(f);return f}else{if(w){throw new Error("Model: specification parameter must be a function")}}var o=function(A,z){var C,y,B;C=A==="Attr"?v:n;y=A==="Attr"?"hasA":"hasMany";g=true;if(typeof(z)==="string"){B=new C(z);k[z]=B;return B}else{throw new Error("Model: "+y+" parameter must be a string")}};h=function(A,z){var y;if(typeof(z)!=="string"){throw new Error("Model: expected string argument to "+A+" method, but recieved "+z)}y=A==="attribute"?k[z]:s[z];if(y===undefined){throw new Error("Model: "+A+" "+z+" does not exist!")}return y};x=function(A){var z,B=[],y=A==="attributes"?k:s;for(z in y){if(y.hasOwnProperty(z)){B.push(z)}}return B};m=function(z){var A,y,B;q=function(){var G,D,E=f.attributes(),F=f.methods(),C=new i(),J,K={},I,H,L;if(!(this instanceof f)){throw new Error("Model: instances must be created using the new operator")}this.emitter=function(){return C};this.on=this.emitter().on;this.emit=this.emitter().emit;this.toJSON=function(N){var Q,O,M,P={},R;if(N===undefined){N=[];N.push({object:this,JSONrep:P})}else{if(typeof(N)!=="object"){throw new Error("Instance: toJSON should not take a parameter (unless called recursively)")}else{for(O=0;O<N.length;++O){if(N[O].object===this){P=N[O].JSONrep}}}}for(O=0;O<E.length;++O){R=null;Q=this[E[O]]();for(M=0;M<N.length;++M){if(N[M].object===Q){R=N[M].JSONrep}}if(Q!==undefined&&Q!==null&&Q.toJSON!==undefined&&R===null){R=(k[E[O]] instanceof n)?[]:{};N.push({object:Q,JSONrep:R});N[N.length-1].JSONrep=Q.toJSON(N)}if(R===null){P[E[O]]=Q}else{P[E[O]]=R}}return P};this.toString=(r!==undefined)?r:function(){return"Jermaine Model Instance"};I=function(M){M.on("set",function(P,O){var N=this;if(K[M.name()]===undefined){K[M.name()]=function(S){var R=[],Q=true;for(G=0;G<S.length&&Q===true;++G){R.push(S[G]);if(S[G].origin===this){Q=false}}if(Q){R.push({key:M.name(),origin:this});this.emit("change",R)}}}if(H!==undefined){if(O!==undefined&&O!==null&&O.emitter!==undefined){O.emitter().removeListener("change",H);H=undefined}}if(P!==null&&typeof(P)==="object"&&P.on!==undefined&&P.emitter!==undefined){if(O!==undefined&&O!==null&&H!==undefined){O.emitter().removeListener("change",H)}H=function(Q){K[M.name()].call(N,Q)};P.emitter().on("change",H)}this.emit("change",[{key:M.name(),value:P,origin:this}])})};for(G=0;G<E.length;++G){J=f.attribute(E[G]);if(J instanceof v){I.call(this,J)}}for(G=0;G<E.length+F.length;++G){if(G<E.length){if(l){k[E[G]].isImmutable()}k[E[G]].addTo(this)}else{s[F[G-E.length]].addTo(this)}}if(arguments.length>0){if(arguments.length<e.length){B="Constructor requires ";for(G=0;G<e.length;++G){B+=e[G];B+=G===e.length-1?"":", "}B+=" to be specified";throw new Error(B)}if(arguments.length>e.length+p.length){throw new Error("Too many arguments to constructor. Expected "+e.length+" required arguments and "+p.length+" optional arguments")}else{for(G=0;G<arguments.length;++G){D=G<e.length?e[G]:p[G-e.length];if(f.attribute(D) instanceof n){if(Object.prototype.toString.call(arguments[G])!=="[object Array]"){throw new Error("Model: Constructor requires 'names' attribute to be set with an Array")}else{for(y=0;y<arguments[G].length;++y){this[D]().add(arguments[G][y])}}}else{this[D](arguments[G])}}}}u.call(this)};return q};f.hasA=function(y){return o("Attr",y)};f.hasAn=f.hasA;f.hasSome=f.hasA;f.hasMany=function(y){return o("AttrList",y)};f.isA=function(A){var z,y,C,B;g=true;B=function(E){var D,F=new b();for(D in F){if(F.hasOwnProperty(D)&&typeof(E[D])!==typeof(F[D])){return false}}return true};if(typeof(A)!=="function"||!B(A)){throw new Error("Model: parameter sent to isA function must be a Model")}if(t.length===0){t.push(A)}else{throw new Error("Model: Model only supports single inheritance at this time")}y=t[0].attributes();for(z=0;z<y.length;++z){if(k[y[z]]===undefined){k[y[z]]=t[0].attribute(y[z]).clone();k[y[z]].isMutable()}}C=t[0].methods();for(z=0;z<C.length;++z){if(s[C[z]]===undefined){s[C[z]]=t[0].method(C[z])}}for(z=0;z<t.length;z++){f.prototype=new t[z]()}};f.isAn=f.isA;f.parent=function(){return t[0].apply(this,arguments)};f.attribute=function(y){return h("attribute",y)};f.attributes=function(){return x("attributes")};f.method=function(y){return h("method",y)};f.methods=function(){return x("methods")};f.isBuiltWith=function(){var y=false,z;g=true;e=[];p=[];for(z=0;z<arguments.length;++z){if(typeof(arguments[z])==="string"&&arguments[z].charAt(0)!=="%"){if(y){throw new Error("Model: isBuiltWith requires parameters preceded with a % to be the final parameters before the optional function")}else{e.push(arguments[z])}}else{if(typeof(arguments[z])==="string"&&arguments[z].charAt(0)==="%"){y=true;p.push(arguments[z].slice(1))}else{if(typeof(arguments[z])==="function"&&z===arguments.length-1){u=arguments[z]}else{throw new Error("Model: isBuiltWith parameters must be strings except for a function as the optional final parameter")}}}}};f.isImmutable=function(){l=true};f.looksLike=function(y){g=true;r=y};f.respondsTo=function(z,A){var y=new d(z,A);g=true;s[z]=y};f.validate=function(){var A,z=this.attributes(),y=this.methods();for(A=0;A<e.length;++A){try{this.attribute(e[A])}catch(B){throw new Error(e[A]+", specified in the isBuiltWith method, is not an attribute")}}for(A=0;A<p.length;++A){try{this.attribute(p[A])}catch(B){throw new Error(p[A]+", specified in the isBuiltWith method, is not an attribute")}}for(A=0;A<z.length;A++){if(y.indexOf(z[A])>-1){throw new Error("Model: invalid model specification to "+z[A]+" being both an attribute and method")}}if(l){for(A=0;A<z.length;A++){if(e.indexOf(z[A])<0){throw new Error("immutable objects must have all attributes required in a call to isBuiltWith")}}}g=false};return f};a.Model=b});
+window.jermaine.util.namespace("window.jermaine",function(a){var b=function(f){var h=[],l=this,m="invalid setter call for "+f,j,k,e,c,o=false,d,n={},p=window.jermaine.AttrList,g=window.jermaine.Validator;if(f===undefined||typeof(f)!=="string"){throw new Error("Attr: constructor requires a name parameter which must be a string")}d=function(i){for(k=0;k<h.length;++k){h[k](i)}return true};this.validatesWith=function(i){if(typeof(i)==="function"){h.push(new g(i));return this}else{throw new Error("Attr: validator must be a function")}};this.defaultsTo=function(i){j=i;return this};this.isReadOnly=function(){o=true;return this};this.isWritable=function(){o=false;return this};this.on=function(i,q){if(i!=="set"&&i!=="get"){throw new Error("Attr: first argument to the 'on' method should be 'set' or 'get'")}else{if(typeof(q)!=="function"){throw new Error("Attr: second argument to the 'on' method should be a function")}else{n[i]=q}}};this.name=function(){return f};this.validator=function(){return d};this.and=this;this.which=this;this.isImmutable=this.isReadOnly;this.isMutable=this.isWritable;this.clone=function(){var q,r;q=this instanceof p?new p(f):new b(f);for(r=0;r<h.length;++r){q.validatesWith(h[r])}q.defaultsTo(j);if(o){q.isImmutable()}return q};this.addTo=function(s){var q,r,i;if(!s||typeof(s)!=="object"){throw new Error("Attr: addAttr method requires an object parameter")}s[f]=function(u){var t;if(u!==undefined){if(o&&q!==undefined){throw new Error("cannot set the immutable property "+f+" after it has been set")}else{if(!d(u)){throw new Error(m)}else{t=q;q=u;if(n.set!==undefined){n.set.call(s,u,t)}}}return s}else{if(n.get!==undefined){n.get.call(s,q)}return q}};i=typeof(j)==="function"?j():j;if(i!==undefined&&d(i)){s[f](i)}else{if(i!==undefined&&!d(i)){throw new Error("Attr: Default value of "+i+" does not pass validation for "+f)}}};c=function(i){l[i]=function(q){h.push(g.getValidator(i)(q));return l}};for(k=0;k<g.validators().length;++k){c(g.validators()[k])}};a.Attr=b});window.jermaine.util.namespace("window.jermaine",function(a){function b(c){var e=this;a.Attr.call(this,c);var d=function(g,f){return function(){return g[f].apply(g,arguments)}};this.validateWith=this.validatesWith;this.defaultsTo=function(){};this.isImmutable=function(){};this.isMutable=function(){};this.eachOfWhich=this;this.addTo=function(h){var i,f=[],g={};if(!h||typeof(h)!=="object"){throw new Error("AttrList: addTo method requires an object parameter")}else{g.pop=d(f,"pop");g.add=function(j){if((e.validator())(j)){f.push(j);return this}else{throw new Error(e.errorMessage())}};g.replace=function(j,k){if((typeof(j)!=="number")||(parseInt(j,10)!==j)){throw new Error("AttrList: replace method requires index parameter to be an integer")}if(j<0||j>=this.size()){throw new Error("AttrList: replace method index parameter out of bounds")}if(!(e.validator())(k)){throw new Error(e.errorMessage())}f[j]=k;return this};g.at=function(j){if(j<0||j>=this.size()){throw new Error("AttrList: Index out of bounds")}return f[j]};g.get=g.at;g.size=function(){return f.length};g.toJSON=function(m){var k=[],n,l;if(m!==undefined){for(n=0;n<m.length;++n){if(m[n].object===this){k=m[n].JSONrep}}}for(n=0;n<f.length;++n){if(f[n].toJSON){k.push(f[n].toJSON(m))}else{k.push(f[n])}}return k};h[c]=function(){return g}}}}b.prototype=new window.jermaine.Attr(name);a.AttrList=b});window.jermaine.util.namespace("window.jermaine",function(a){var b=function(c,d){if(!c||typeof(c)!=="string"){throw new Error("Method: constructor requires a name parameter which must be a string")}else{if(!d||typeof(d)!=="function"){throw new Error("Method: second parameter must be a function")}}this.addTo=function(e){if(!e||typeof(e)!=="object"){throw new Error("Method: addTo method requires an object parameter")}e[c]=d}};a.Method=b});window.jermaine.util.namespace("window.jermaine",function(a){var b=function(u){var q={},i={},p,f=true,d=[],n=[],r=[],c=a.Method,s=a.Attr,l=a.AttrList,h=a.util.EventEmitter,g,v,k,j,t=function(){},o=function(){},e=function(){if(f){e.validate();k()}return o.apply(this,arguments)};if(arguments.length>1){u=arguments[arguments.length-1]}if(u&&typeof(u)==="function"){e=new b();u.call(e);return e}else{if(u){throw new Error("Model: specification parameter must be a function")}}var m=function(y,x){var A,w,z;A=y==="Attr"?s:l;w=y==="Attr"?"hasA":"hasMany";f=true;if(typeof(x)==="string"){z=new A(x);i[x]=z;return z}else{throw new Error("Model: "+w+" parameter must be a string")}};g=function(y,x){var w;if(typeof(x)!=="string"){throw new Error("Model: expected string argument to "+y+" method, but recieved "+x)}w=y==="attribute"?i[x]:q[x];if(w===undefined){throw new Error("Model: "+y+" "+x+" does not exist!")}return w};v=function(y){var x,z=[],w=y==="attributes"?i:q;for(x in w){if(w.hasOwnProperty(x)){z.push(x)}}return z};k=function(){o=function(){var C,B,z,x,y=e.attributes(),A=e.methods(),w=new h(),F,G={},E,D,H;if(!(this instanceof e)){throw new Error("Model: instances must be created using the new operator")}this.emitter=function(){return w};this.on=this.emitter().on;this.emit=this.emitter().emit;this.toJSON=function(J){var M,K,I,L={},N;if(J===undefined){J=[];J.push({object:this,JSONrep:L})}else{if(typeof(J)!=="object"){throw new Error("Instance: toJSON should not take a parameter (unless called recursively)")}else{for(K=0;K<J.length;++K){if(J[K].object===this){L=J[K].JSONrep}}}}for(K=0;K<y.length;++K){N=null;M=this[y[K]]();for(I=0;I<J.length;++I){if(J[I].object===M){N=J[I].JSONrep}}if(M!==undefined&&M!==null&&M.toJSON!==undefined&&N===null){N=(i[y[K]] instanceof l)?[]:{};J.push({object:M,JSONrep:N});J[J.length-1].JSONrep=M.toJSON(J)}if(N===null){L[y[K]]=M}else{L[y[K]]=N}}return L};this.toString=(p!==undefined)?p:function(){return"Jermaine Model Instance"};E=function(I){I.on("set",function(L,K){var J=this;if(G[I.name()]===undefined){G[I.name()]=function(O){var N=[],M=true;for(C=0;C<O.length&&M===true;++C){N.push(O[C]);if(O[C].origin===this){M=false}}if(M){N.push({key:I.name(),origin:this});this.emit("change",N)}}}if(D!==undefined){if(K!==undefined&&K!==null&&K.emitter!==undefined){K.emitter().removeListener("change",D);D=undefined}}if(L!==null&&typeof(L)==="object"&&L.on!==undefined&&L.emitter!==undefined){if(K!==undefined&&K!==null&&D!==undefined){K.emitter().removeListener("change",D)}D=function(M){G[I.name()].call(J,M)};L.emitter().on("change",D)}this.emit("change",[{key:I.name(),value:L,origin:this}])})};for(C=0;C<y.length;++C){F=e.attribute(y[C]);if(F instanceof s){E.call(this,F)}}for(C=0;C<y.length+A.length;++C){if(C<y.length){if(j){e.attribute(y[C]).isImmutable()}e.attribute(y[C]).addTo(this)}else{e.method(A[C-y.length]).addTo(this)}}if(arguments.length>0){if(arguments.length<d.length){z="Constructor requires ";for(C=0;C<d.length;++C){z+=d[C];z+=C===d.length-1?"":", "}z+=" to be specified";throw new Error(z)}if(arguments.length>d.length+n.length){throw new Error("Too many arguments to constructor. Expected "+d.length+" required arguments and "+n.length+" optional arguments")}else{for(C=0;C<arguments.length;++C){x=C<d.length?d[C]:n[C-d.length];if(e.attribute(x) instanceof l){if(Object.prototype.toString.call(arguments[C])!=="[object Array]"){throw new Error("Model: Constructor requires 'names' attribute to be set with an Array")}else{for(B=0;B<arguments[C].length;++B){this[x]().add(arguments[C][B])}}}else{this[x](arguments[C])}}}}t.call(this)}};e.hasA=function(w){return m("Attr",w)};e.hasAn=e.hasA;e.hasSome=e.hasA;e.hasMany=function(w){return m("AttrList",w)};e.isA=function(y){var x,w,A,z;f=true;z=function(C){var B,D=new b();for(B in D){if(D.hasOwnProperty(B)&&typeof(C[B])!==typeof(D[B])){return false}}return true};if(typeof(y)!=="function"||!z(y)){throw new Error("Model: parameter sent to isA function must be a Model")}if(r.length===0){r.push(y)}else{throw new Error("Model: Model only supports single inheritance at this time")}w=r[0].attributes();for(x=0;x<w.length;++x){if(i[w[x]]===undefined){i[w[x]]=r[0].attribute(w[x]).clone();i[w[x]].isMutable()}}A=r[0].methods();for(x=0;x<A.length;++x){if(q[A[x]]===undefined){q[A[x]]=r[0].method(A[x])}}for(x=0;x<r.length;x++){e.prototype=new r[x]()}};e.isAn=e.isA;e.parent=function(){return r[0].apply(this,arguments)};e.attribute=function(w){return g("attribute",w)};e.attributes=function(){return v("attributes")};e.method=function(w){return g("method",w)};e.methods=function(){return v("methods")};e.isBuiltWith=function(){var w=false,x;f=true;d=[];n=[];for(x=0;x<arguments.length;++x){if(typeof(arguments[x])==="string"&&arguments[x].charAt(0)!=="%"){if(w){throw new Error("Model: isBuiltWith requires parameters preceded with a % to be the final parameters before the optional function")}else{d.push(arguments[x])}}else{if(typeof(arguments[x])==="string"&&arguments[x].charAt(0)==="%"){w=true;n.push(arguments[x].slice(1))}else{if(typeof(arguments[x])==="function"&&x===arguments.length-1){t=arguments[x]}else{throw new Error("Model: isBuiltWith parameters must be strings except for a function as the optional final parameter")}}}}};e.isImmutable=function(){j=true};e.looksLike=function(w){f=true;p=w};e.respondsTo=function(x,y){var w=new c(x,y);f=true;q[x]=w};e.validate=function(){var y,x=this.attributes(),w=this.methods();for(y=0;y<d.length;++y){try{this.attribute(d[y])}catch(z){throw new Error(d[y]+", specified in the isBuiltWith method, is not an attribute")}}for(y=0;y<n.length;++y){try{this.attribute(n[y])}catch(z){throw new Error(n[y]+", specified in the isBuiltWith method, is not an attribute")}}for(y=0;y<x.length;y++){if(w.indexOf(x[y])>-1){throw new Error("Model: invalid model specification to "+x[y]+" being both an attribute and method")}}if(j){for(y=0;y<x.length;y++){if(d.indexOf(x[y])<0){throw new Error("immutable objects must have all attributes required in a call to isBuiltWith")}}}f=false};return e};a.Model=b});
View
34 build/jermaine.js
@@ -829,13 +829,8 @@ window.jermaine.util.namespace("window.jermaine", function (ns) {
window.jermaine.util.namespace("window.jermaine", function (ns) {
"use strict";
- var Constructor = function (model) {
-
- };
-
var Model = function (specification) {
- var that = this,
- methods = {},
+ var methods = {},
attributes = {},
pattern,
modified = true,
@@ -848,7 +843,7 @@ window.jermaine.util.namespace("window.jermaine", function (ns) {
EventEmitter = ns.util.EventEmitter,
property,
listProperties,
- create,
+ updateConstructor,
isImmutable,
initializer = function () {},
constructor = function () {},
@@ -856,7 +851,7 @@ window.jermaine.util.namespace("window.jermaine", function (ns) {
if (modified) {
//validate the model if it has been modified
model.validate();
- create();
+ updateConstructor();
}
return constructor.apply(this, arguments);
};
@@ -932,13 +927,11 @@ window.jermaine.util.namespace("window.jermaine", function (ns) {
return list;
};
- /* private function that creates the constructor */
- create = function (name) {
- var i, j,
- err;
-
+ /* private function that updates the constructor */
+ updateConstructor = function () {
constructor = function () {
- var i,
+ var i, j,
+ err,
attribute,
attributeList = model.attributes(),
methodList = model.methods(),
@@ -1104,11 +1097,11 @@ window.jermaine.util.namespace("window.jermaine", function (ns) {
if (i < attributeList.length) {
//if the object is immutable, all attributes should be immutable
if (isImmutable) {
- attributes[attributeList[i]].isImmutable();
+ model.attribute(attributeList[i]).isImmutable();
}
- attributes[attributeList[i]].addTo(this);
+ model.attribute(attributeList[i]).addTo(this);
} else {
- methods[methodList[i-attributeList.length]].addTo(this);
+ model.method(methodList[i-attributeList.length]).addTo(this);
}
}
@@ -1154,7 +1147,6 @@ window.jermaine.util.namespace("window.jermaine", function (ns) {
// finally, call the initializer
initializer.call(this);
};
- return constructor;
};
/*********** END PRIVATE METHODS **************/
@@ -1296,8 +1288,8 @@ window.jermaine.util.namespace("window.jermaine", function (ns) {
model.validate = function () {
var i,
- attributes = this.attributes(),
- methods = this.methods();
+ attributes = this.attributes(),
+ methods = this.methods();
//check to make sure that isBuiltWith has actual attributes
for (i = 0; i < requiredConstructorArgs.length; ++i) {
@@ -1336,8 +1328,6 @@ window.jermaine.util.namespace("window.jermaine", function (ns) {
modified = false;
};
/************** END PUBLIC API ****************/
-
-
//here we are returning our model object
//which is a function with a bunch of methods that
View
36 src/core/model.js
@@ -1,13 +1,8 @@
window.jermaine.util.namespace("window.jermaine", function (ns) {
"use strict";
- var Constructor = function (model) {
-
- };
-
var Model = function (specification) {
- var that = this,
- methods = {},
+ var methods = {},
attributes = {},
pattern,
modified = true,
@@ -20,7 +15,7 @@ window.jermaine.util.namespace("window.jermaine", function (ns) {
EventEmitter = ns.util.EventEmitter,
property,
listProperties,
- create,
+ updateConstructor,
isImmutable,
initializer = function () {},
constructor = function () {},
@@ -28,7 +23,7 @@ window.jermaine.util.namespace("window.jermaine", function (ns) {
if (modified) {
//validate the model if it has been modified
model.validate();
- create();
+ updateConstructor();
}
return constructor.apply(this, arguments);
};
@@ -104,13 +99,11 @@ window.jermaine.util.namespace("window.jermaine", function (ns) {
return list;
};
- /* private function that creates the constructor */
- create = function (name) {
- var i, j,
- err;
-
+ /* private function that updates the constructor */
+ updateConstructor = function () {
constructor = function () {
- var i,
+ var i, j,
+ err,
attribute,
attributeList = model.attributes(),
methodList = model.methods(),
@@ -276,11 +269,11 @@ window.jermaine.util.namespace("window.jermaine", function (ns) {
if (i < attributeList.length) {
//if the object is immutable, all attributes should be immutable
if (isImmutable) {
- attributes[attributeList[i]].isImmutable();
+ model.attribute(attributeList[i]).isImmutable();
}
- attributes[attributeList[i]].addTo(this);
+ model.attribute(attributeList[i]).addTo(this);
} else {
- methods[methodList[i-attributeList.length]].addTo(this);
+ model.method(methodList[i-attributeList.length]).addTo(this);
}
}
@@ -326,7 +319,6 @@ window.jermaine.util.namespace("window.jermaine", function (ns) {
// finally, call the initializer
initializer.call(this);
};
- return constructor;
};
/*********** END PRIVATE METHODS **************/
@@ -468,8 +460,8 @@ window.jermaine.util.namespace("window.jermaine", function (ns) {
model.validate = function () {
var i,
- attributes = this.attributes(),
- methods = this.methods();
+ attributes = this.attributes(),
+ methods = this.methods();
//check to make sure that isBuiltWith has actual attributes
for (i = 0; i < requiredConstructorArgs.length; ++i) {
@@ -508,8 +500,6 @@ window.jermaine.util.namespace("window.jermaine", function (ns) {
modified = false;
};
/************** END PUBLIC API ****************/
-
-
//here we are returning our model object
//which is a function with a bunch of methods that
@@ -518,4 +508,4 @@ window.jermaine.util.namespace("window.jermaine", function (ns) {
};
ns.Model = Model;
-});
+});
Please sign in to comment.
Something went wrong with that request. Please try again.