Skip to content
Browse files

Added MVC Pattern

  • Loading branch information...
1 parent a6b914b commit 2eded004168c3f1dbc55b2322069289989c767bc @rgr-myrg committed
Showing with 187 additions and 3 deletions.
  1. +1 −1 build.sh
  2. +6 −1 build/devshop-0.1-SNAPSHOT.js
  3. +6 −1 examples/devshop.js
  4. +174 −0 src/mvc.js
View
2 build.sh
@@ -3,7 +3,7 @@
VERSION="0.1-SNAPSHOT"
ARTIFACT="devshop-$VERSION.js"
-JS_FILES="--js src/core.js --js src/singletonfactory.js --js src/observable.js --js src/observer.js --js src/eventsignal.js"
+JS_FILES="--js src/core.js --js src/singletonfactory.js --js src/observable.js --js src/observer.js --js src/eventsignal.js --js src/mvc.js"
JS_BUILD="build/$ARTIFACT"
JS_TEMP="build/tmp.js"
EXAMPLE="examples/devshop.js"
View
7 build/devshop-0.1-SNAPSHOT.js
@@ -3,4 +3,9 @@
* Released under the MIT license:
* https://github.com/rgr-myrg/DevShop-JS/raw/master/MIT-LICENSE
*/
-(function(){window.DevShop=typeof window.DevShop==="object"?window.DevShop:{};window.DevShop.Me=function(a){for(var b in a)a.hasOwnProperty(b)&&(window.DevShop[b]=a[b]);DevShop=devshop=window.DevShop}})();(function(){DevShop.Me({SingletonFactory:function(a){var b={},c={};if(typeof a.extend==="function")try{b=new a.extend}catch(d){}else if(typeof a.extend==="object")b=a.extend;if(typeof a.instance==="function")try{c=new a.instance}catch(f){}else if(typeof a.instance==="object")c=a.instance;for(var e in b)b.hasOwnProperty(e)&&(c[e]||(c[e]=b[e]));if(typeof c.initialize==="function")try{c.initialize()}catch(g){}return c}})})();(function(){DevShop.Me({Observable:function(a){return DevShop.SingletonFactory({extend:function(){this.observers=[];this.addObserver=function(b){if((typeof b==="function"||typeof b==="object")&&typeof b.notify==="function")if(this.observers.push(b),typeof b.onRegister==="function")try{b.onRegister()}catch(a){}};this.notifyObservers=function(b){for(var a=this.observers.length,d=0;d<a;d++)try{this.observers[d].notify(b,this)}catch(f){}}},instance:a})}})})();(function(){DevShop.Me({Observer:function(a){return DevShop.SingletonFactory({extend:function(){this.onRegister=function(){};this.notify=function(b,a){this.observable=a;if(typeof this[b]==="function")try{this[b]()}catch(d){}}},instance:a})}})})();(function(){DevShop.Me({EventSignal:function(){var a=[];this.addListener=function(b){typeof b==="function"&&a.push(b)};this.removeListener=function(b){for(var c=a.length,d=0;d<c;d++)a[d]===b&&a.splice(d)};this.dispatch=function(){for(var b=a.length,c=0;c<b;c++)try{a[c].apply(this,arguments)}catch(d){}}}})})();
+(function(){window.DevShop=typeof window.DevShop==="object"?window.DevShop:{};window.DevShop.Me=function(d){for(var c in d)d.hasOwnProperty(c)&&(window.DevShop[c]=d[c]);DevShop=devshop=window.DevShop}})();(function(){DevShop.Me({SingletonFactory:function(d){var c=function(a){if(typeof a==="function")try{return new a}catch(b){}else if(typeof a==="object")return a},e=c(d.implement),a=c(d.extend),c=c(d.instance),b;for(b in a)a.hasOwnProperty(b)&&(c[b]||(c[b]=a[b]));for(b in e)if(e.hasOwnProperty(b)&&!c[b])throw d.instance+" must implement '"+b+"' "+typeof e[b];if(typeof c.initialize==="function")try{c.initialize()}catch(h){}return c}})})();(function(){DevShop.Me({Observable:function(d){return DevShop.SingletonFactory({extend:function(){this.observers=[];this.addObserver=function(c){if((typeof c==="function"||typeof c==="object")&&typeof c.notify==="function")if(this.observers.push(c),typeof c.onRegister==="function")try{c.onRegister()}catch(d){}};this.notifyObservers=function(c){for(var d=this.observers.length,a=0;a<d;a++)try{this.observers[a].notify(c,this)}catch(b){}}},instance:d})}})})();(function(){DevShop.Me({Observer:function(d){return DevShop.SingletonFactory({extend:function(){this.onRegister=function(){};this.notify=function(c,d){this.observable=d;if(typeof this[c]==="function")try{this[c]()}catch(a){}}},instance:d})}})})();(function(){DevShop.Me({EventSignal:function(){var d=[];this.addListener=function(c){typeof c==="function"&&d.push(c)};this.removeListener=function(c){for(var e=d.length,a=0;a<e;a++)d[a]===c&&d.splice(a)};this.dispatch=function(){for(var c=d.length,e=0;e<c;e++)try{d[e].apply(this,arguments)}catch(a){}}}})})();(function(){DevShop.Me({ProxyInterface:{NAME:""},MediatorInterface:{NAME:"",listNotificationInterests:function(){},handleNotification:function(){}},CommandInterface:{execute:function(){}},Proxy:function(){var d={};this.facade=null;this.setData=function(c){d=c};this.getData=function(){return d};this.onRegister=function(){};this.onRemove=function(){}},Mediator:new DevShop.Observer(function(){this.facade=null;this.onRegister=function(){};this.onRemove=function(){}}),Facade:function(){var d=new function(){var a=
+{};this.facade={};this.registerProxy=function(b){b.facade=this.facade;a[b.NAME]||(a[b.NAME]=b);if(typeof b.onRegister==="function")b.onRegister()};this.retrieveProxy=function(b){return a[b]?a[b]:null};this.removeProxy=function(b){if(typeof a[b].onRemove==="function")try{a[b].onRemove()}catch(c){}a[b]=null}},c=new DevShop.Observable(function(){var a={};this.facade={};this.notification={};this.registerMediator=function(b){b.facade=this.facade;a[b.NAME]||(a[b.NAME]=b,this.addObserver(b))};this.retrieveMediator=
+function(b){return a[b]?a[b]:null};this.removeMediator=function(b){if(typeof a[b].onRemove==="function")try{a[b].onRemove()}catch(c){}a[b]=null};this.notifyObservers=function(a){for(var c=this.observers.length,d=0;d<c;d++)try{for(var e=this.observers[d].listNotificationInterests(),g=!1,f=0;l=e.length,f<l;f++)if(e[f]==this.notification.name){g=!0;break}if(g)this.observers[d].notification=this.notification,this.observers[d].notify(a,this)}catch(i){}};this.sendNotification=function(a){this.notification=
+a;this.notifyObservers("handleNotification")}}),e=new DevShop.Observer(function(){var a={};this.facade={};this.NAME="DevShop.Controller";this.registerCommand=function(b,c){c.facade=this.facade;a[b]||(a[b]=c)};this.listNotificationInterests=function(){return[this.facade.CMD_STARTUP]};this.handleNotification=function(){var b=this.notification;if(typeof a[b.name]==="object"&&typeof a[b.name].execute==="function")try{a[b.name].execute(b)}catch(c){}}});this.CMD_STARTUP="CMD_STARTUP";this.registerProxy=
+function(a){d.registerProxy(a)};this.registerMediator=function(a){c.registerMediator(a)};this.registerCommand=function(a,b){e.registerCommand(a,b)};this.retrieveProxy=function(a){return d.retrieveProxy(a)};this.retrieveMediator=function(a){return c.retrieveMediator(a)};this.removeProxy=function(a){d.removeProxy(a)};this.removeMediator=function(a){c.removeMediator(a)};this.sendNotification=function(a,b,d){c.sendNotification({name:a,body:b,type:d})};this.initializeFacade=function(a){d.facade=a;c.facade=
+a;e.facade=a;a.registerMediator(e)}}})})();
View
7 examples/devshop.js
@@ -1 +1,6 @@
-(function(){window.DevShop=typeof window.DevShop==="object"?window.DevShop:{};window.DevShop.Me=function(a){for(var b in a)a.hasOwnProperty(b)&&(window.DevShop[b]=a[b]);DevShop=devshop=window.DevShop}})();(function(){DevShop.Me({SingletonFactory:function(a){var b={},c={};if(typeof a.extend==="function")try{b=new a.extend}catch(d){}else if(typeof a.extend==="object")b=a.extend;if(typeof a.instance==="function")try{c=new a.instance}catch(f){}else if(typeof a.instance==="object")c=a.instance;for(var e in b)b.hasOwnProperty(e)&&(c[e]||(c[e]=b[e]));if(typeof c.initialize==="function")try{c.initialize()}catch(g){}return c}})})();(function(){DevShop.Me({Observable:function(a){return DevShop.SingletonFactory({extend:function(){this.observers=[];this.addObserver=function(b){if((typeof b==="function"||typeof b==="object")&&typeof b.notify==="function")if(this.observers.push(b),typeof b.onRegister==="function")try{b.onRegister()}catch(a){}};this.notifyObservers=function(b){for(var a=this.observers.length,d=0;d<a;d++)try{this.observers[d].notify(b,this)}catch(f){}}},instance:a})}})})();(function(){DevShop.Me({Observer:function(a){return DevShop.SingletonFactory({extend:function(){this.onRegister=function(){};this.notify=function(b,a){this.observable=a;if(typeof this[b]==="function")try{this[b]()}catch(d){}}},instance:a})}})})();(function(){DevShop.Me({EventSignal:function(){var a=[];this.addListener=function(b){typeof b==="function"&&a.push(b)};this.removeListener=function(b){for(var c=a.length,d=0;d<c;d++)a[d]===b&&a.splice(d)};this.dispatch=function(){for(var b=a.length,c=0;c<b;c++)try{a[c].apply(this,arguments)}catch(d){}}}})})();
+(function(){window.DevShop=typeof window.DevShop==="object"?window.DevShop:{};window.DevShop.Me=function(d){for(var c in d)d.hasOwnProperty(c)&&(window.DevShop[c]=d[c]);DevShop=devshop=window.DevShop}})();(function(){DevShop.Me({SingletonFactory:function(d){var c=function(a){if(typeof a==="function")try{return new a}catch(b){}else if(typeof a==="object")return a},e=c(d.implement),a=c(d.extend),c=c(d.instance),b;for(b in a)a.hasOwnProperty(b)&&(c[b]||(c[b]=a[b]));for(b in e)if(e.hasOwnProperty(b)&&!c[b])throw d.instance+" must implement '"+b+"' "+typeof e[b];if(typeof c.initialize==="function")try{c.initialize()}catch(h){}return c}})})();(function(){DevShop.Me({Observable:function(d){return DevShop.SingletonFactory({extend:function(){this.observers=[];this.addObserver=function(c){if((typeof c==="function"||typeof c==="object")&&typeof c.notify==="function")if(this.observers.push(c),typeof c.onRegister==="function")try{c.onRegister()}catch(d){}};this.notifyObservers=function(c){for(var d=this.observers.length,a=0;a<d;a++)try{this.observers[a].notify(c,this)}catch(b){}}},instance:d})}})})();(function(){DevShop.Me({Observer:function(d){return DevShop.SingletonFactory({extend:function(){this.onRegister=function(){};this.notify=function(c,d){this.observable=d;if(typeof this[c]==="function")try{this[c]()}catch(a){}}},instance:d})}})})();(function(){DevShop.Me({EventSignal:function(){var d=[];this.addListener=function(c){typeof c==="function"&&d.push(c)};this.removeListener=function(c){for(var e=d.length,a=0;a<e;a++)d[a]===c&&d.splice(a)};this.dispatch=function(){for(var c=d.length,e=0;e<c;e++)try{d[e].apply(this,arguments)}catch(a){}}}})})();(function(){DevShop.Me({ProxyInterface:{NAME:""},MediatorInterface:{NAME:"",listNotificationInterests:function(){},handleNotification:function(){}},CommandInterface:{execute:function(){}},Proxy:function(){var d={};this.facade=null;this.setData=function(c){d=c};this.getData=function(){return d};this.onRegister=function(){};this.onRemove=function(){}},Mediator:new DevShop.Observer(function(){this.facade=null;this.onRegister=function(){};this.onRemove=function(){}}),Facade:function(){var d=new function(){var a=
+{};this.facade={};this.registerProxy=function(b){b.facade=this.facade;a[b.NAME]||(a[b.NAME]=b);if(typeof b.onRegister==="function")b.onRegister()};this.retrieveProxy=function(b){return a[b]?a[b]:null};this.removeProxy=function(b){if(typeof a[b].onRemove==="function")try{a[b].onRemove()}catch(c){}a[b]=null}},c=new DevShop.Observable(function(){var a={};this.facade={};this.notification={};this.registerMediator=function(b){b.facade=this.facade;a[b.NAME]||(a[b.NAME]=b,this.addObserver(b))};this.retrieveMediator=
+function(b){return a[b]?a[b]:null};this.removeMediator=function(b){if(typeof a[b].onRemove==="function")try{a[b].onRemove()}catch(c){}a[b]=null};this.notifyObservers=function(a){for(var c=this.observers.length,d=0;d<c;d++)try{for(var e=this.observers[d].listNotificationInterests(),g=!1,f=0;l=e.length,f<l;f++)if(e[f]==this.notification.name){g=!0;break}if(g)this.observers[d].notification=this.notification,this.observers[d].notify(a,this)}catch(i){}};this.sendNotification=function(a){this.notification=
+a;this.notifyObservers("handleNotification")}}),e=new DevShop.Observer(function(){var a={};this.facade={};this.NAME="DevShop.Controller";this.registerCommand=function(b,c){c.facade=this.facade;a[b]||(a[b]=c)};this.listNotificationInterests=function(){return[this.facade.CMD_STARTUP]};this.handleNotification=function(){var b=this.notification;if(typeof a[b.name]==="object"&&typeof a[b.name].execute==="function")try{a[b.name].execute(b)}catch(c){}}});this.CMD_STARTUP="CMD_STARTUP";this.registerProxy=
+function(a){d.registerProxy(a)};this.registerMediator=function(a){c.registerMediator(a)};this.registerCommand=function(a,b){e.registerCommand(a,b)};this.retrieveProxy=function(a){return d.retrieveProxy(a)};this.retrieveMediator=function(a){return c.retrieveMediator(a)};this.removeProxy=function(a){d.removeProxy(a)};this.removeMediator=function(a){c.removeMediator(a)};this.sendNotification=function(a,b,d){c.sendNotification({name:a,body:b,type:d})};this.initializeFacade=function(a){d.facade=a;c.facade=
+a;e.facade=a;a.registerMediator(e)}}})})();
View
174 src/mvc.js
@@ -0,0 +1,174 @@
+/**
+ * Copyright (c) 2011 Roger Myrg http://devshop.me/
+ * Released under the MIT license:
+ * https://github.com/rgr-myrg/DevShop-JS/raw/master/MIT-LICENSE
+ */
+(function(){
+ DevShop.Me({
+ ProxyInterface:{
+ NAME:""
+ },
+ MediatorInterface:{
+ NAME:"",
+ listNotificationInterests:function(){},
+ handleNotification:function(){}
+ },
+ CommandInterface:{
+ execute:function(notification){}
+ },
+ Proxy:function(){
+ var data={};
+ this.facade=null;
+ this.setData=function(obj){
+ data=obj;
+ };
+ this.getData=function(){
+ return data;
+ };
+ this.onRegister=function(){
+ return;
+ };
+ this.onRemove=function(){
+ return;
+ };
+ },
+ Mediator:new DevShop.Observer(function(){
+ this.facade=null;
+ this.onRegister=function(){
+ return;
+ };
+ this.onRemove=function(){
+ return;
+ };
+ }),
+ Facade:function(){
+ var Model=new function(){
+ var proxies={};
+ this.facade={};
+ this.registerProxy=function(proxy){
+ proxy.facade=this.facade;
+ if(!proxies[proxy.NAME])
+ proxies[proxy.NAME]=proxy;
+ if(typeof proxy.onRegister==="function")
+ proxy.onRegister();
+ };
+ this.retrieveProxy=function(key){
+ return proxies[key]?proxies[key]:null;
+ };
+ this.removeProxy=function(key){
+ if(typeof proxies[key].onRemove==="function")
+ try{proxies[key].onRemove();}catch(e){}
+ proxies[key]=null;
+ };
+ };
+ var View=new DevShop.Observable(function(){
+ var mediators={};
+ this.facade={};
+ this.notification={};
+ this.registerMediator=function(mediator){
+ mediator.facade=this.facade;
+ if(!mediators[mediator.NAME]){
+ mediators[mediator.NAME]=mediator;
+ this.addObserver(mediator);
+ }
+ };
+ this.retrieveMediator=function(key){
+ return mediators[key]?mediators[key]:null;
+ };
+ this.removeMediator=function(key){
+ if(typeof mediators[key].onRemove==="function")
+ try{mediators[key].onRemove();}catch(e){}
+ mediators[key]=null;
+ };
+ this.notifyObservers=function(eventName){
+ var size=this.observers.length;
+ for(var x=0;x<size;x++){
+ try{
+ var notices=this.observers[x].listNotificationInterests();
+ var deliver=false;
+ for(var i=0;l=notices.length,i<l;i++){
+ if(notices[i]==this.notification.name){
+ deliver=true;
+ break;
+ }
+ }
+ if(deliver){
+ this.observers[x].notification=this.notification;
+ this.observers[x].notify(eventName,this);
+ }
+ }catch(e){}
+ }
+ };
+ this.sendNotification=function(notification){
+ this.notification=notification;
+ this.notifyObservers('handleNotification');
+ };
+ });
+ var Controller=new DevShop.Observer(function(){
+ var commands={};
+ this.facade={};
+ this.NAME="DevShop.Controller";
+ this.registerCommand=function(key,command){
+ command.facade=this.facade;
+ if(!commands[key])
+ commands[key]=command;
+ };
+ this.listNotificationInterests=function(){
+ return [this.facade.CMD_STARTUP];
+ };
+ this.handleNotification=function(){
+ var notification=this.notification;
+ if(typeof commands[notification.name]==="object")
+ if(typeof commands[notification.name].execute==="function")
+ try{
+ commands[notification.name].execute(notification);
+ }catch(e){}
+ };
+ });
+ var initializeModel=function(app){
+ Model.facade=app;
+ };
+ var initializeView=function(app){
+ View.facade=app;
+ };
+ var initializeController=function(app){
+ Controller.facade=app;
+ app.registerMediator(Controller);
+ };
+ this.CMD_STARTUP="CMD_STARTUP";
+ this.registerProxy=function(proxy){
+ Model.registerProxy(proxy);
+ };
+ this.registerMediator=function(mediator){
+ View.registerMediator(mediator);
+ };
+ this.registerCommand=function(key,command){
+ Controller.registerCommand(key,command);
+ };
+ this.retrieveProxy=function(key){
+ return Model.retrieveProxy(key);
+ };
+ this.retrieveMediator=function(key){
+ return View.retrieveMediator(key);
+ };
+ this.removeProxy=function(key){
+ Model.removeProxy(key);
+ };
+ this.removeMediator=function(key){
+ View.removeMediator(key);
+ };
+ this.sendNotification=function(name,body,type){
+ View.sendNotification({
+ name:name,
+ body:body,
+ type:type
+ });
+ };
+ this.initializeFacade=function(app){
+ initializeModel(app);
+ initializeView(app);
+ initializeController(app);
+ };
+ }
+ });
+})();

0 comments on commit 2eded00

Please sign in to comment.
Something went wrong with that request. Please try again.