Lungo.Boot.Resources.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. var response = _loadAsyncResource(url);
  33. _factoryResources(resource_key, response);
  34. }
  35. };
  36. var _parseUrl = function(section_url, folder) {
  37. return (/http/.test(section_url)) ? section_url : 'app/' + folder + '/' + section_url;
  38. };
  39. var _loadAsyncResource = function(url) {
  40. return $$.ajax({
  41. url: url,
  42. async: false,
  43. dataType: 'html',
  44. error: function() {
  45. console.error('[ERROR] Loading url', arguments);
  46. }
  47. });
  48. };
  49. var _factoryResources = function(resource_key, response) {
  50. switch(resource_key) {
  51. case RESOURCE.SECTION:
  52. _pushSectionInLayout(response);
  53. break;
  54. case RESOURCE.TEMPLATE:
  55. _createTemplate(response);
  56. break;
  57. case RESOURCE.SCRIPT:
  58. break;
  59. }
  60. };
  61. var _pushSectionInLayout = function(section) {
  62. if (lng.Core.toType(section) === 'string') {
  63. lng.dom(ELEMENT.BODY).append(section);
  64. }
  65. };
  66. var _createTemplate = function(markup) {
  67. var div = document.createElement(ELEMENT.DIV);
  68. div.innerHTML = markup;
  69. var template_id = lng.dom(div.firstChild).data('template');
  70. if (template_id) {
  71. lng.View.Template.create(template_id, markup);
  72. }
  73. };
  74. return {
  75. start: start
  76. };
  77. })(LUNGO, Quo);