Lungo.View.Template.List.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 _config = null;
  12. /**
  13. * Create a list based DataBind with a configuration object for an element <article>
  14. * if the config has a 'norecords' property it will display the norecords markup rather than nothing.
  15. *
  16. * @method create
  17. *
  18. * @param {object} Id of the container showing the result of databinding
  19. */
  20. var create = function(config) {
  21. _config = config;
  22. _config.container_id += '_list';
  23. if (_validateConfig()) {
  24. _order();
  25. // @ToDo >> _group();
  26. _render();
  27. _createScroll();
  28. }
  29. };
  30. var _validateConfig = function() {
  31. var checked = false;
  32. var container_exists = !! lng.dom(_config.container_id);
  33. var template_exists = lng.View.Template.exists(_config.template_id);
  34. if (container_exists && template_exists) {
  35. //@ToDo >> Refactor to other method
  36. lng.dom("#"+_config.container_id).html('');
  37. var type = lng.Core.toType(_config.data);
  38. if (type === 'array' || type === 'object') {
  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('#' + _config.container_id).parent().attr('id');
  62. var list_config = { snap: 'li' };
  63. lng.View.Scroll.init(container_id_for_scroll, list_config);
  64. };
  65. return {
  66. create: create
  67. };
  68. })(LUNGO);