Lungo.View.Template.List.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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(_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("#"+_config.container_id).html('');
  38. var type = lng.Core.toType(_config.data);
  39. if (type === 'array' || type === 'object') {
  40. checked = true;
  41. }
  42. }
  43. return checked;
  44. };
  45. var _order = function() {
  46. var order_field = _config.order_field;
  47. var order_type = (_config.order_type === 'desc') ? -1 : 1;
  48. if (order_field && order_type) {
  49. _config.data.sort(function(a, b) {
  50. return (a[order_field] < b[order_field]) ? - order_type :
  51. (a[order_field] > b[order_field]) ? order_type : 0;
  52. });
  53. }
  54. };
  55. // @ToDo >> group list by property
  56. var _group = function() {
  57. };
  58. var _render = function() {
  59. lng.View.Template.Binding.create(_config.container_id, _config.template_id, _config.data);
  60. };
  61. var _createScroll = function() {
  62. var container_id_for_scroll = lng.dom('#' + _config.container_id).parent().attr('id');
  63. var list_config = { snap: 'li' };
  64. lng.View.Scroll.create(container_id_for_scroll, list_config);
  65. };
  66. return {
  67. create: create
  68. };
  69. })(LUNGO);