Advertisement
Guest User

Untitled

a guest
Dec 28th, 2014
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. function App(){
  2. function init(){
  3. var o1 = new Object1();
  4. var o2 = new Object2();
  5. }
  6.  
  7. function toggleOtherObjects(){
  8. o2.toggle();
  9. // o3.toggle();
  10. // o4.toggle();
  11. // o5.toggle();
  12.  
  13. }
  14. }
  15.  
  16. function Object1(){
  17. // I can be interacted with directly
  18. function toggleOther(){
  19. App.toggleOtherObjects();
  20. }
  21. }
  22.  
  23. function Object2(){
  24. function toggle(){
  25. // some toggle logic
  26. }
  27. }
  28.  
  29. window.App = new App();
  30. App.init();
  31.  
  32. function App(){
  33. var o1handle,o2handle
  34. function init(o1,o2){
  35. o1handle = o1.handleObject.bind.apply(o1,arguments);
  36. o2handle = o2.handleObject.bind.apply(o2,arguments);
  37. }
  38. }
  39.  
  40. function Object1(){
  41. this.handleObject = function(){ // put on prototype if no closure access needed
  42. for(var i=0;i<arguments.length;i++){
  43. if(arguments[i]!== this) someActionOn(arguments[i]);
  44. }
  45. }
  46. }
  47.  
  48. function App(){
  49. function init(){
  50. var o1 = new Object1();
  51. var o2 = new Object2();
  52. }
  53. }
  54.  
  55. function App(){
  56. var objs = [];
  57. function init(){
  58. objs.push(new Object1());
  59. objs.push(new Object2());
  60. // ... push all other objects of types
  61. }
  62.  
  63. function toggleOtherObjects(idx){
  64. objs.forEach(function(elem,i){
  65. if(i!==idx) elem.toggleObject();
  66. });
  67. }
  68. }
  69.  
  70. Dispatcher = function()
  71. {
  72. var channels = {};
  73.  
  74. this.register = function( identifier, callback )
  75. {
  76. if ( typeof channels[identifier] === 'undefined' ){ channels[identifier] = [] }
  77. channels[identifier].push( callback )
  78. }
  79.  
  80. this.broadcast = function( channel, payload )
  81. {
  82. if( !(typeof channels[identifier] === 'undefined' )){
  83. foreach (i=0, l=channels[identifier].length; i < l; i++) {
  84. channels[identifier][i]( payload )
  85. }
  86. }
  87. }
  88.  
  89. }
  90.  
  91. Object1 = function( dispatcher )
  92. {
  93. var that = this
  94.  
  95. dispatcher.register( 'objectToggled', function( payload ){
  96. if ( payload != that ){ that.toggle( true ) }
  97. }
  98.  
  99. this.toggle = function( silently ){
  100. do_something()
  101. if (!silently){ dispatcher.broadcast( 'objectToggled', this ) }
  102. }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement