Lungo.View.Template.Binding.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /**
  2. * Lungo Data-Binding system
  3. *
  4. * @namespace LUNGO.View.Template
  5. * @class Binding
  6. * @requires Zepto
  7. *
  8. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  9. * @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  10. */
  11. LUNGO.View.Template.Binding = (function(lng, undefined) {
  12. var BINDING_START = '{{';
  13. var BINDING_END = '}}';
  14. var BINDING_PARSER = /\{{.*?\}}/gi;
  15. /**
  16. * Performs databinding process for a data set and a given template
  17. *
  18. * @method create
  19. *
  20. * @param {String} Id of the container showing the result of databinding
  21. * @param {String} Databinding Template Id
  22. * @param {Object} Data for binding
  23. * @param {Function} Callback when the process is complete
  24. */
  25. var create = function(container_id, template_id, data, callback) {
  26. if (lng.View.Template.exists(template_id)) {
  27. var template = lng.View.Template.get(template_id);
  28. var markup = _processData(data, template);
  29. _render(container_id, markup);
  30. lng.Core.execute(callback);
  31. } else {
  32. lng.Core.log(3, 'lng.View.Template.binding: id ' + template_id + ' not exists');
  33. }
  34. };
  35. var dataAttribute = function(element, attribute) {
  36. var data = element.data(attribute.tag);
  37. if (data) {
  38. var html_binded = attribute.html.replace(BINDING_START + 'value' + BINDING_END, data);
  39. element.prepend(html_binded);
  40. }
  41. };
  42. var _processData = function(data, template) {
  43. var data_type = lng.Core.toType(data);
  44. if (data_type === 'array') {
  45. return _bindPropertiesInMultiplesElements(data, template);
  46. } else if (data_type === 'object') {
  47. return _bindProperties(data, template);
  48. } else {
  49. lng.Core.log(3, 'View.Template ERROR >> No type defined.');
  50. }
  51. };
  52. var _bindPropertiesInMultiplesElements = function(elements, template) {
  53. var markup = '';
  54. for (var i = 0, len = elements.length; i < len; i++) {
  55. markup += _bindProperties(elements[i], template);
  56. }
  57. return markup;
  58. };
  59. var _bindProperties = function(element, template) {
  60. var binding_field;
  61. for (var property in element) {
  62. if (lng.Core.isOwnProperty(element, property)) {
  63. binding_field = new RegExp(BINDING_START + property + BINDING_END, 'g');
  64. template = template.replace(binding_field, element[property]);
  65. }
  66. }
  67. return _removeNoBindedProperties(template);
  68. };
  69. var _removeNoBindedProperties = function(template) {
  70. return template.replace(BINDING_PARSER, '');
  71. };
  72. var _render = function(container_id, markup) {
  73. var container = lng.Dom.query('#' + container_id);
  74. container.html(markup);
  75. };
  76. return {
  77. create: create,
  78. dataAttribute: dataAttribute
  79. };
  80. })(LUNGO);