Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function App(){
- function init(){
- var o1 = new Object1();
- var o2 = new Object2();
- }
- function toggleOtherObjects(){
- o2.toggle();
- // o3.toggle();
- // o4.toggle();
- // o5.toggle();
- }
- }
- function Object1(){
- // I can be interacted with directly
- function toggleOther(){
- App.toggleOtherObjects();
- }
- }
- function Object2(){
- function toggle(){
- // some toggle logic
- }
- }
- window.App = new App();
- App.init();
- function App(){
- var o1handle,o2handle
- function init(o1,o2){
- o1handle = o1.handleObject.bind.apply(o1,arguments);
- o2handle = o2.handleObject.bind.apply(o2,arguments);
- }
- }
- function Object1(){
- this.handleObject = function(){ // put on prototype if no closure access needed
- for(var i=0;i<arguments.length;i++){
- if(arguments[i]!== this) someActionOn(arguments[i]);
- }
- }
- }
- function App(){
- function init(){
- var o1 = new Object1();
- var o2 = new Object2();
- }
- }
- function App(){
- var objs = [];
- function init(){
- objs.push(new Object1());
- objs.push(new Object2());
- // ... push all other objects of types
- }
- function toggleOtherObjects(idx){
- objs.forEach(function(elem,i){
- if(i!==idx) elem.toggleObject();
- });
- }
- }
- Dispatcher = function()
- {
- var channels = {};
- this.register = function( identifier, callback )
- {
- if ( typeof channels[identifier] === 'undefined' ){ channels[identifier] = [] }
- channels[identifier].push( callback )
- }
- this.broadcast = function( channel, payload )
- {
- if( !(typeof channels[identifier] === 'undefined' )){
- foreach (i=0, l=channels[identifier].length; i < l; i++) {
- channels[identifier][i]( payload )
- }
- }
- }
- }
- Object1 = function( dispatcher )
- {
- var that = this
- dispatcher.register( 'objectToggled', function( payload ){
- if ( payload != that ){ that.toggle( true ) }
- }
- this.toggle = function( silently ){
- do_something()
- if (!silently){ dispatcher.broadcast( 'objectToggled', this ) }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement