Lungo.View.Template.List.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /**
  2. * Auto generate lists based on Template and Data-Binding system
  3. *
  4. * @namespace LUNGO.View.Template
  5. * @class List
  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.List = (function(lng, undefined) {
  12. var _config = null;
  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 = config;
  23. _config.container_id += '_list';
  24. if (_validateConfig()) {
  25. _order();
  26. // @ToDo >> _group();
  27. _render();
  28. _createScroll();
  29. }
  30. };
  31. var _validateConfig = function() {
  32. var checked = false;
  33. var container_exists = !! lng.Dom.query(_config.container_id);
  34. var template_exists = lng.View.Template.exists(_config.template_id);
  35. if (container_exists && template_exists) {
  36. //@ToDo >> Refactor to other method
  37. lng.Dom.query("#"+_config.container_id).html('');
  38. if (_config.data.length) {
  39. checked = true;
  40. }
  41. }
  42. return checked;
  43. };
  44. var _order = function() {
  45. var order_field = _config.order_field;
  46. var order_type = (_config.order_type === 'desc') ? -1 : 1;
  47. if (order_field && order_type) {
  48. _config.data.sort(function(a, b) {
  49. return (a[order_field] < b[order_field]) ? - order_type :
  50. (a[order_field] > b[order_field]) ? order_type : 0;
  51. });
  52. }
  53. };
  54. // @ToDo >> group list by property
  55. var _group = function() {
  56. };
  57. var _render = function() {
  58. lng.View.Template.Binding.create(_config.container_id, _config.template_id, _config.data);
  59. };
  60. var _createScroll = function() {
  61. var container_id_for_scroll = lng.Dom.query('#' + _config.container_id).parent().attr('id');
  62. var list_config = {snap:'li'};
  63. lng.View.Scroll.create(container_id_for_scroll, list_config);
  64. };
  65. return {
  66. create: create
  67. };
  68. })(LUNGO);