Lungo.View.Template.List.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /**
  2. * Auto generate lists based on Template and Data-Binding system
  3. *
  4. * @namespace LUNGO.View.Template
  5. * @class List
  6. *
  7. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  8. * @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  9. */
  10. LUNGO.View.Template.List = (function(lng, undefined) {
  11. var ERROR = lng.Constants.ERROR;
  12. var ATTRIBUTE = lng.Constants.ATTRIBUTE;
  13. /**
  14. * Create a list based DataBind with a configuration object for an element <article>
  15. * if the config has a 'norecords' property it will display the norecords markup rather than nothing.
  16. *
  17. * @method create
  18. *
  19. * @param {object} Id of the container showing the result of databinding
  20. */
  21. var create = function(config) {
  22. config.container = _getContainer(config.el);
  23. if (_validateConfig(config)) {
  24. config.data = _order(config);
  25. _render(config);
  26. }
  27. };
  28. /**
  29. * Append a list based DataBind with a configuration object for an element <article>
  30. * if the config has a 'norecords' property it will display the norecords markup rather than nothing.
  31. *
  32. * @method append
  33. *
  34. * @param {object} Id of the container showing the result of databinding
  35. */
  36. var append = function(config) {
  37. var markup = lng.View.Template.markup(config.template, config.data);
  38. var container = _getContainer(config.el);
  39. container.append(markup);
  40. };
  41. /**
  42. * Prepend a list based DataBind with a configuration object for an element <article>
  43. * if the config has a 'norecords' property it will display the norecords markup rather than nothing.
  44. *
  45. * @method prepend
  46. *
  47. * @param {object} Id of the container showing the result of databinding
  48. */
  49. var prepend = function(config) {
  50. var markup = lng.View.Template.markup(config.template, config.data);
  51. var container = _getContainer(config.el);
  52. container.prepend(markup);
  53. };
  54. var _validateConfig = function(config) {
  55. var checked = false;
  56. var container_exists = !! config.container.length > 0;
  57. var template_exists = lng.View.Template.exists(config.template);
  58. if (container_exists && template_exists) {
  59. var type = lng.Core.toType(config.data);
  60. if (type === 'array' || type === 'object') {
  61. checked = true;
  62. }
  63. } else {
  64. lng.Core.log(3, ERROR.BINDING_LIST);
  65. }
  66. return checked;
  67. };
  68. var _getContainer = function(element) {
  69. return lng.dom(element).children().first();
  70. };
  71. var _order = function(config) {
  72. if (config.order && config.order.field && config.order.type) {
  73. config.data = lng.Core.orderByProperty(config.data, config.order.field, config.order.type);
  74. }
  75. return config.data;
  76. };
  77. var _render = function(config) {
  78. lng.View.Template.render(config.container.selector, config.template, config.data);
  79. };
  80. return {
  81. create: create,
  82. append: append,
  83. prepend: prepend
  84. };
  85. })(LUNGO);