Quantcast
Channel: JavaScriptMVC Forum
Viewing all articles
Browse latest Browse all 3491

Mustache {{#each tag}} and {{#tag}} are inconsistent

$
0
0
In the below example, when using {{#each items}} instead of {{#items}}, the dom will render with the results spliced in place instead of completely replacing. With 20 items initially showing, and 5 items return, the first 5 will be updated, with the other 15 still existing.

  1. define(['jquery', 'models/alerts', 'components/paginate', 'models/posts', 'can'], function( $, Alerts, Paginate, posts ) {
  2. var Query = can.Component.extend({
  3. tag: 'listing',
  4. scope: function( attr ) {

  5. if(typeof attr.filters != 'object') {
  6. attr.filters = {};
  7. }

  8. return {
  9. loading: true,
  10. paginate: new Paginate({
  11. limit: 20
  12. }),
  13. filters: new can.Map(attr.filters),
  14. items: new can.List([]),
  15. query: can.compute(function() {

  16. var self = this;

  17. var params = {
  18. limit: this.attr('paginate.limit'),
  19. offset: this.attr('paginate.offset'),
  20. filters: this.attr('filters').attr()
  21. };

  22. var queryDeferred = Posts.findAll(params);

  23. queryDeferred.then(function( items ) {
  24. self.attr('paginate.count', items.length );
  25. });

  26. return queryDeferred;
  27. })
  28. };
  29. },
  30. events: {
  31. init: function() {
  32. this.update();
  33. },
  34. update: function() {
  35. var query = this.scope.attr('query'),
  36. scope = this.scope;

  37. if(can.isDeferred(query)) {
  38. query.then(function( items ) {
  39. scope.attr('items').replace(items);
  40. });
  41. query.fail(Alerts.failure);
  42. } else {
  43. this.scope.attr('items').replace(query.attr(), true);
  44. }
  45. },
  46. '{query} change': 'update',
  47. '{items} change': function( list, ev, index, mode, newItems, oldItem ) {
  48. this.scope.attr('loading', false);
  49. },
  50. 'form.query submit': function( el, ev ) {
  51. ev.preventDefault();
  52. can.batch.start();
  53. this.scope.attr('filters', can.deparam(el.serialize()));
  54. this.scope.attr('paginate.offset', 0);
  55. can.batch.stop();
  56. }
  57. }
  58. });

  59. return Query;
  60. });
  61. define(['jquery', 'can'], function( $ ) {

  62. var Paginate = can.Map.extend({
  63. offset: 0,
  64. count: 0,
  65. limit: 5,
  66. next: function () {
  67. this.attr('offset', this.offset + this.limit);
  68. },
  69. prev: function () {
  70. this.attr('offset', this.offset - this.limit);
  71. },
  72. canNext: function () {
  73. return this.attr('count') == this.attr('limit');
  74. },
  75. canPrev: function () {
  76. return this.attr('offset') > 0;
  77. },
  78. page: function (newVal) {
  79. if (newVal === undefined) {
  80. return Math.floor(this.attr('offset') / this.attr('limit')) + 1;
  81. } else {
  82. this.attr('offset', (parseInt(newVal, 10) - 1) * this.attr('limit'));
  83. }
  84. }
  85. });

  86. return Paginate;
  87. });

Viewing all articles
Browse latest Browse all 3491

Trending Articles