I namespace my models and a single controller like this:
var MC = {};
and then added properties as needed.
For example, the code that initializes all my models looks like this:
MC.initAll = function() {
MC.MASettings.init();
MC.MATweet.init();
MC.MUserTry.init();
MC.MUserNew.init();
MC.MUserExist.init();
Su.UserOut.init();
Su.Media.init();
}
I plan on changing this to a loop...just loop through MC, and if init() exists execute it.
The only thing that bothers me about this architecture is there is no privacy or encapsulation in the traditional sense that everything is public.
Also, I don't instantiate my models, I just call them as functions, but this is pretty much the same as instantiation in .js.
Is there a simple way to add privacy to my models and still have them accessible as properties in an object.
Just for an example here is my simplest model :
MC.MUserTry = {
init: function() {
document.getElementById( 'ut_but' ).addEventListener( "click", function( ) {
MC.Controller( MC.o_p( 'MUserTry' ) );
}, false );
},
pre : function( o_p ) {
o_p.page.email = document.getElementById( 'ut_but' ).getAttribute( 'data-email' );
return o_p;
},
post : function( o_p ) {
sLocal( o_p.server.hash, o_p.server.privacy, o_p.server.name, o_p.server.picture, 'ma' );
vStateUpdate( o_p.server.name, o_p.server.picture, o_p.server.privacy );
vTPane( o_p.server.tweets ); vBPane( o_p.server.bookmarks );
vFlipP( 'ma' );
}
};
pre() runs before an ajax call, post() after, and init() is called via an onload event or similar.
Here is the controller that actually implements this.
MC.Controller = function( o_p ) {
console.log( 'o_p = ' + o_p.model );
var t1, t2, t3, t4,
i1, i2, i3, i4,
o_p_string_send;
if( SU.get('debug') ) {
t1 = new Date().getTime();
}
o_p = MC[ o_p.model ].pre( o_p );
if ( o_p.result !== 'complete' ) {
o_p_string_send = JSON.stringify( o_p );
if( SU.get('debug') ) {
t2 = new Date().getTime();
console.log( '---------------Server Send: \n ' + o_p_string_send );
}
cMachine( 'pipe=' + o_p_string_send , function( o_p_string_receive ) {
if( SU.get('debug') ) {
console.log( '---------------Server Receive: \n ' + o_p_string_receive );
t3 = new Date().getTime();
}
o_p.server = JSON.parse( o_p_string_receive );
MC[ o_p.model ].post( o_p );
if( SU.get('debug') ) {
t4 = new Date().getTime(); i1 = t2-t1 ; i2 = t3-t2 ; i3 = t4-t3; i4 = o_p.server.time;
console.log( '---------------Time: \n Pre | Transit | Post | Server = ', i1, ' | ', i2, ' | ', i3,' | ', i4 );
}
} );
}
};
I would like to add privacy. How do I do this and still keep my models accessible as object properties?
Related
How to create global, instance based objects from local scope?