jQuery app with custom namespace


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. /* inspired by iNettus tutorial http://net.tutsplus.com/tutorials/javascript-ajax/inettuts/ */
  2.  
  3. var JqAppName = {
  4.  
  5. jQuery: $,
  6.  
  7. settings: {
  8. defaults:{},
  9. individual:{
  10. elemId1: {},
  11. elemId2: {}
  12. }
  13. },
  14.  
  15. init: function(){
  16. this.method1();
  17. this.method2();
  18. this.method3();
  19. },
  20.  
  21. getElemSettings: function(arg){ // pass in either an element ID or an object literal containing options to override settings.default
  22. var $ = this.jQuery, settings = this.settings;
  23.  
  24. if ( typeof(arg) === "undefined" )
  25. { return settings.defaults; }
  26.  
  27. else if ( typeof(arg) === "string" )
  28. {
  29. return (arg&&settings.individuals[arg]) ? $.extend({}, settings.defaults, settings.individuals[arg]) : settings.defaults;
  30. }
  31. else if ( typeof(arg) === "object" )
  32. {
  33. return $.extend({}, settings.defaults, args );
  34. }
  35.  
  36. },
  37.  
  38. method1: function(){
  39. var JqAppName = this, $ = this.jQuery, settings = this.settings;
  40. // do something simple without messing with individual settings
  41. }
  42.  
  43. method2: function(id){ // pass in an element id to get its pre-set from settings.individual.elemIdX
  44. var JqAppName = this, $ = this.jQuery, settings = this.settings;
  45. var thisElemSettings = typeof(id) === 'undefined' ? JqAppName.getElemSettings('elemId1') : JqAppName.getElemSettings(id);
  46. // do something awesome
  47. },
  48.  
  49. method3: function(options){ // pass an an object for options to override settings.default
  50. var JqAppName = this, $ = this.jQuery, settings = this.settings;
  51. var thisElemSettings = typeof(options) === 'undefined' ? JqAppName.getElemSettings({/* options hash */}) : JqAppName.getElemSettings(options);
  52.  
  53. // do something awesomer
  54. }
  55.  
  56.  
  57. } // end of JqApp scope

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.