Lungo.View.Template.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * Lungo Template system
  3. *
  4. * @namespace LUNGO.View
  5. * @class Template
  6. *
  7. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  8. * @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  9. */
  10. LUNGO.View.Template = (function(lng, undefined) {
  11. var ERROR = lng.Constants.ERROR;
  12. var _templates = {};
  13. /**
  14. * Create a new databinding template based on a <markup>
  15. *
  16. * @method create
  17. *
  18. * @param {String} Id of the new databinding template
  19. * @param {String} <markup> of the new databinding template
  20. */
  21. var create = function(id, markup) {
  22. _templates[id] = markup;
  23. };
  24. /**
  25. * Returns the existence of a certain Id databinding template
  26. *
  27. * @method exists
  28. *
  29. * @param {String} Id of the databinding template
  30. * @return {Boolean} true if exists, false if not.
  31. */
  32. var exists = function(id) {
  33. return (_templates[id]) ? true : false;
  34. };
  35. /**
  36. * Returns the instance of a certain Id databinding template
  37. *
  38. * @method get
  39. *
  40. * @param {String} Id of the databinding template
  41. * @return {String} Markup of template
  42. */
  43. var get = function(id) {
  44. return _templates[id];
  45. };
  46. /**
  47. * Performs databinding process for a data set and a given template
  48. *
  49. * @method render
  50. *
  51. * @param {String} Element selector for showing the result of databinding
  52. * @param {String} Databinding Template Id
  53. * @param {Object} Data for binding
  54. * @param {Function} Callback when the process is complete
  55. */
  56. var render = function(element, template_id, data, callback) {
  57. if (lng.View.Template.exists(template_id)) {
  58. var container = lng.dom(element);
  59. var markup = this.markup(template_id, data);
  60. container.html(markup);
  61. lng.Core.execute(callback);
  62. } else {
  63. lng.Core.log(3, ERROR.BINDING_TEMPLATE + template_id);
  64. }
  65. };
  66. /**
  67. * Performs databinding process for a data set and a given template
  68. *
  69. * @method markup
  70. *
  71. * @param {String} Databinding Template Id
  72. * @param {Object} Data for binding
  73. */
  74. var markup = function(template_id, data) {
  75. return lng.View.Template.Binding.create(template_id, data);
  76. };
  77. return {
  78. create: create,
  79. exists: exists,
  80. get: get,
  81. render: render,
  82. markup: markup
  83. };
  84. })(LUNGO);