Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. /* General Notes
  2.  
  3. - When possible, do all DOM construction in the PHP view, using onClick instead of adding JS eventListeners.
  4. - Never use instance_ids when making views with multiple instances. Only use uniqueHashes generated by PHP. Way easier to work with.
  5.  
  6. */
  7.  
  8. var awesomeJSView = function () {
  9.  
  10. // make the current scope accessible. EventListeners which change scope will need this guy.
  11. var self = this;
  12.  
  13.  
  14. /*************** Constants *****************/
  15.  
  16. // internally accessible constants - display modes, settings, etc.
  17. var INTERNAL_ONLY_CONSTANT = 'hey';
  18.  
  19. // externally accessible constants
  20. this.EXTERNALLY_ACCESSIBLE_CONSTANT = 'sup';
  21.  
  22.  
  23.  
  24.  
  25. /*************** Translation *****************/
  26.  
  27. // all translatable strings grouped together in one place for convenience. Leave the replacement tokens in so that JS can use them.
  28. var LANG_MY_TEXT_STRING = "<?php translate("Hey, this is a long piece of %s translated text. %t")?>";
  29.  
  30. // an sprintf function, pulling from the local language constants.
  31. p(translate(LANG_MY_TEXT_STRING, 'variable 1', 'variable 2'));
  32.  
  33.  
  34.  
  35.  
  36. /*************** Event Registry *****************/
  37.  
  38. // EventRegistry custom events
  39. var EVENT_CUSTOM_EVENT = 'Unique event for eventRegistry to catch.';
  40.  
  41.  
  42. /*************** Method definitions *****************/
  43. // methods declared as functions are local-scope only.
  44. // methods declared as object properties are available globally.
  45.  
  46. function privateFunction() {
  47. // stuff
  48. }
  49.  
  50. this.publicFunction = function () {
  51. // other stuff
  52. }
  53.  
  54. // public function to trigger this view
  55. this.init = function () {
  56.  
  57. // ADD EVENT REGISTRY LISTENERS HERE ( i.e: once only! )
  58.  
  59. // Add a listener to an existing shared event
  60. eventRegistry.addEventListener(EventRegistry.AjaxResponse, myFunc, scope);
  61.  
  62. // Add a listener to a private event
  63. eventRegistry.addEventListener(this.CustomEvent, myFunc, scope);
  64.  
  65. }
  66.  
  67.  
  68.  
  69. /*************** Display methods *****************/
  70.  
  71. // public function to re-render the view. This should be made to change as few DOM nodes as possible.
  72. this.render = function () {
  73. // render stuff
  74. }
  75.  
  76. // private function to get the DOM elements for a specific part of the view.
  77. function getContentSpecificElement() {
  78. // render specific stuff
  79. }
  80.  
  81. /*************** Ajax methods *****************/
  82. // always add an 'ajax' prefix to all ajax methods.
  83. function ajaxDoStuff() {
  84.  
  85. // Where possible, prevent duplicate ajax calls from going out.
  86. if (this.ajax_busy) {
  87. return false;
  88. } else {
  89. this.ajax_busy = true;
  90. }
  91.  
  92. // do HTTP post stuff
  93. }
  94.  
  95.  
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement