Lungo.Boot.Resources.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. ASIDE: 'asides',
  15. TEMPLATE: 'templates'
  16. };
  17. /**
  18. * Start loading async sections (local & remote)
  19. *
  20. * @method start
  21. *
  22. */
  23. var init = function(resources) {
  24. for (resource_key in resources) {
  25. _loadResources(resource_key, resources[resource_key]);
  26. }
  27. };
  28. var _loadResources = function(resource_key, resources, callback) {
  29. for (index in resources) {
  30. var url = _parseUrl(resources[index], resource_key);
  31. try {
  32. var response = _loadSyncResource(url);
  33. _factoryResources(resource_key, response);
  34. } catch(error) {
  35. lng.Core.log(3, error.message);
  36. }
  37. }
  38. };
  39. var _parseUrl = function(section_url, folder) {
  40. return (/http/.test(section_url)) ? section_url : 'app/resources/' + folder + '/' + section_url;
  41. };
  42. var _loadSyncResource = function(url) {
  43. return $$.ajax({
  44. url: url,
  45. async: false,
  46. dataType: 'html',
  47. error: function() {
  48. console.error(ERROR.LOADING_RESOURCE + url);
  49. // throw new Error(ERROR.LOADING_RESOURCE + url);
  50. }
  51. });
  52. };
  53. var _factoryResources = function(resource_key, response) {
  54. if (resource_key == RESOURCE.TEMPLATE) {
  55. _createTemplate(response);
  56. } else {
  57. _pushSectionInLayout(response);
  58. }
  59. };
  60. var _pushSectionInLayout = function(section) {
  61. if (lng.Core.toType(section) === 'string') {
  62. lng.dom(ELEMENT.BODY).append(section);
  63. }
  64. };
  65. var _createTemplate = function(markup) {
  66. var div = document.createElement(ELEMENT.DIV);
  67. div.innerHTML = markup;
  68. var template_id = lng.dom(div.firstChild).data('template');
  69. if (template_id) {
  70. lng.View.Template.create(template_id, markup);
  71. }
  72. };
  73. return {
  74. init: init
  75. };
  76. })(Lungo, Quo);