Lungo.Boot.Resources.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /**
  2. * Load Resources
  3. *
  4. * @namespace LUNGO.Boot
  5. * @class Resources
  6. *
  7. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  8. */
  9. LUNGO.Boot.Resources = (function(lng, $$, undefined) {
  10. var ELEMENT = lng.Constants.ELEMENT;
  11. var ERROR = lng.Constants.ERROR;
  12. var RESOURCE = {
  13. SECTION: 'sections',
  14. TEMPLATE: 'templates',
  15. SCRIPT: 'scripts'
  16. };
  17. /**
  18. * Start loading async sections (local & remote)
  19. *
  20. * @method start
  21. *
  22. */
  23. var start = function() {
  24. var resources = lng.App.get('resources');
  25. for (resource_key in resources) {
  26. _loadResources(resource_key, resources[resource_key]);
  27. }
  28. };
  29. var _loadResources = function(resource_key, resources, callback) {
  30. for (index in resources) {
  31. var url = _parseUrl(resources[index], resource_key);
  32. try {
  33. var response = _loadAsyncResource(url);
  34. _factoryResources(resource_key, response);
  35. } catch(error) {
  36. lng.Core.log(3, ERROR.LOADING_RESOURCE + ' ' + error);
  37. }
  38. }
  39. };
  40. var _parseUrl = function(section_url, folder) {
  41. return (/http/.test(section_url)) ? section_url : 'app/' + folder + '/' + section_url;
  42. };
  43. var _loadAsyncResource = function(url) {
  44. return $$.ajax({
  45. url: url,
  46. async: false,
  47. dataType: 'html',
  48. error: function() {
  49. console.error('[ERROR] Loading url', arguments);
  50. }
  51. });
  52. };
  53. var _factoryResources = function(resource_key, response) {
  54. switch(resource_key) {
  55. case RESOURCE.SECTION:
  56. _pushSectionInLayout(response);
  57. break;
  58. case RESOURCE.TEMPLATE:
  59. _createTemplate(response);
  60. break;
  61. case RESOURCE.SCRIPT:
  62. break;
  63. }
  64. };
  65. var _pushSectionInLayout = function(section) {
  66. if (lng.Core.toType(section) === 'string') {
  67. lng.dom(ELEMENT.BODY).append(section);
  68. }
  69. };
  70. var _createTemplate = function(markup) {
  71. var div = document.createElement(ELEMENT.DIV);
  72. div.innerHTML = markup;
  73. var template_id = lng.dom(div.firstChild).data('template');
  74. if (template_id) {
  75. lng.View.Template.create(template_id, markup);
  76. }
  77. };
  78. return {
  79. start: start
  80. };
  81. })(LUNGO, Quo);