ws-ui-parts.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. "use strict";
  2. (function($) {
  3. $(document).ready(function() {
  4. var eventBinders = {
  5. menuExample: function() {
  6. console.log('eventBinder for menuExample', this);
  7. this.$elem.find('a').click(function(e) {
  8. e.preventDefault();
  9. var $link = $(this);
  10. var originalColor = $link.css('backgroundColor');
  11. // self.toggleMenu();
  12. $link.animate({
  13. backgroundColor: '#aaa',
  14. }, 70);
  15. $link.animate({
  16. backgroundColor: originalColor,
  17. }, 70);
  18. _ws.navigator.navigateTo($link.prop('href'));
  19. });
  20. }
  21. }
  22. /**
  23. * Render an UI part
  24. */
  25. function renderPart(partName, data) {
  26. var part = _ws.ui[partName];
  27. part.$elem.html( Mustache.render( part.tmpl, data ) );
  28. }
  29. /**
  30. * Initialize an empty object to be populated as follows:
  31. * - key is an UI element's id
  32. * - value associated is an obj containing: tmpl (the template) and $elem (jQueried target element)
  33. */
  34. _ws.ui = {};
  35. // Iterate over the template script elements
  36. var $templateElems = $('script[data-tmpl-for]');
  37. $templateElems.each(function(idx, el) {
  38. // Get the script element content and target element id
  39. var $script = $(el);
  40. var tmpl = $script.html();
  41. var elemId = $script.data('tmpl-for');
  42. var $elem = $('#' + elemId);
  43. var partName = _.camelCase(elemId);
  44. // Populate the uiParts object
  45. _ws.ui[partName] = {
  46. tmpl: tmpl,
  47. $elem: $elem,
  48. render: (function(_name) {
  49. return function(data) {
  50. var eventBinder = eventBinders[_name];
  51. renderPart(_name, data);
  52. if(eventBinder) {
  53. eventBinder = eventBinder.bind(this);
  54. console.log('bind events for', _name);
  55. eventBinder();
  56. }
  57. };
  58. })(partName)
  59. };
  60. });
  61. // console.log(_.map(_ws.uiParts, p => (p.render.toString())));
  62. });
  63. })(jQuery);