Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* General Notes
- - When possible, do all DOM construction in the PHP view, using onClick instead of adding JS eventListeners.
- - Never use instance_ids when making views with multiple instances. Only use uniqueHashes generated by PHP. Way easier to work with.
- */
- var awesomeJSView = function () {
- // make the current scope accessible. EventListeners which change scope will need this guy.
- var self = this;
- /*************** Constants *****************/
- // internally accessible constants - display modes, settings, etc.
- var INTERNAL_ONLY_CONSTANT = 'hey';
- // externally accessible constants
- this.EXTERNALLY_ACCESSIBLE_CONSTANT = 'sup';
- /*************** Translation *****************/
- // all translatable strings grouped together in one place for convenience. Leave the replacement tokens in so that JS can use them.
- var LANG_MY_TEXT_STRING = "<?php translate("Hey, this is a long piece of %s translated text. %t")?>";
- // an sprintf function, pulling from the local language constants.
- p(translate(LANG_MY_TEXT_STRING, 'variable 1', 'variable 2'));
- /*************** Event Registry *****************/
- // EventRegistry custom events
- var EVENT_CUSTOM_EVENT = 'Unique event for eventRegistry to catch.';
- /*************** Method definitions *****************/
- // methods declared as functions are local-scope only.
- // methods declared as object properties are available globally.
- function privateFunction() {
- // stuff
- }
- this.publicFunction = function () {
- // other stuff
- }
- // public function to trigger this view
- this.init = function () {
- // ADD EVENT REGISTRY LISTENERS HERE ( i.e: once only! )
- // Add a listener to an existing shared event
- eventRegistry.addEventListener(EventRegistry.AjaxResponse, myFunc, scope);
- // Add a listener to a private event
- eventRegistry.addEventListener(this.CustomEvent, myFunc, scope);
- }
- /*************** Display methods *****************/
- // public function to re-render the view. This should be made to change as few DOM nodes as possible.
- this.render = function () {
- // render stuff
- }
- // private function to get the DOM elements for a specific part of the view.
- function getContentSpecificElement() {
- // render specific stuff
- }
- /*************** Ajax methods *****************/
- // always add an 'ajax' prefix to all ajax methods.
- function ajaxDoStuff() {
- // Where possible, prevent duplicate ajax calls from going out.
- if (this.ajax_busy) {
- return false;
- } else {
- this.ajax_busy = true;
- }
- // do HTTP post stuff
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement