From cb18485c3abbab00460f62cf302d32a17d5f28fc Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Mon, 6 Jun 2022 10:34:48 +0300 Subject: [PATCH] stream fetch new no rebuild fix --- client/src/collections/note.js | 10 ++- client/src/views/notification/record/list.js | 44 ++++----- client/src/views/record/list.js | 7 ++ client/src/views/stream/record/list.js | 94 ++++++++++---------- 4 files changed, 82 insertions(+), 73 deletions(-) diff --git a/client/src/collections/note.js b/client/src/collections/note.js index 08cd485d93..13fbaaeebf 100644 --- a/client/src/collections/note.js +++ b/client/src/collections/note.js @@ -42,14 +42,19 @@ define('collections/note', 'collection', function (Dep) { this.total = total; } } + return list; }, fetchNew: function (options) { - var options = options || {}; + options = options || {}; options.data = options.data || {}; + options.fetchNew = true; + options.noRebuild = true; + options.lengthBeforeFetch = this.length; + if (this.length) { options.data.after = this.models[0].get('createdAt'); options.remove = false; @@ -57,9 +62,8 @@ define('collections/note', 'collection', function (Dep) { options.maxSize = null; } - this.fetch(options); + return this.fetch(options); }, }); - }); diff --git a/client/src/views/notification/record/list.js b/client/src/views/notification/record/list.js index ef2f8543c5..faa7a69f0b 100644 --- a/client/src/views/notification/record/list.js +++ b/client/src/views/notification/record/list.js @@ -30,32 +30,36 @@ define('views/notification/record/list', 'views/record/list-expanded', function return Dep.extend({ - showNewRecords: function () { - var collection = this.collection; - var initialCount = collection.length; + setup: function () { + Dep.prototype.setup.call(this); - var $list = this.$el.find(this.listContainerEl); - - var success = () => { - if (initialCount === 0) { - this.reRender(); - + this.listenTo(this.collection, 'sync', (c, r, options) => { + if (!options.fetchNew) { return; } - var rowCount = collection.length - initialCount; - var rowsReady = 0; + let lengthBeforeFetch = options.lengthBeforeFetch || 0; - for (var i = rowCount - 1; i >= 0; i--) { - var model = collection.at(i); + if (lengthBeforeFetch === 0) { + this.reRender(); + + return; + } + + let $list = this.$el.find(this.listContainerEl); + + let rowCount = this.collection.length - lengthBeforeFetch; + + for (let i = rowCount - 1; i >= 0; i--) { + let model = this.collection.at(i); this.buildRow(i, model, view => { view.getHtml(html => { - var $row = $(this.getRowContainerHtml(model.id)); + let $row = $(this.getRowContainerHtml(model.id)); $row.append(html); $list.prepend($row); - rowsReady++; + view._afterRender(); if (view.options.el) { @@ -64,13 +68,11 @@ define('views/notification/record/list', 'views/record/list-expanded', function }); }); } - - this.noRebuild = true; - }; - - collection.fetchNew({ - success: success, }); }, + + showNewRecords: function () { + this.collection.fetchNew(); + }, }); }); diff --git a/client/src/views/record/list.js b/client/src/views/record/list.js index a5e64d5e16..689051b90e 100644 --- a/client/src/views/record/list.js +++ b/client/src/views/record/list.js @@ -1616,6 +1616,12 @@ define( return; } + if (options.noRebuild) { + this.noRebuild = null; + + return; + } + this.checkedList = []; this.allResultIsChecked = false; @@ -2225,6 +2231,7 @@ define( } }); + // If using promise callback, then need to pass `noRebuild: true`. collection.fetch({ success: success, remove: false, diff --git a/client/src/views/stream/record/list.js b/client/src/views/stream/record/list.js index 104ff43560..1473ecfd7e 100644 --- a/client/src/views/stream/record/list.js +++ b/client/src/views/stream/record/list.js @@ -38,6 +38,43 @@ define('views/stream/record/list', 'views/record/list-expanded', function (Dep) this.itemViews = this.getMetadata().get('clientDefs.Note.itemViews') || {}; Dep.prototype.setup.call(this); + + this.listenTo(this.collection, 'sync', (c, r, options) => { + if (!options.fetchNew) { + return; + } + + let lengthBeforeFetch = options.lengthBeforeFetch || 0; + + if (lengthBeforeFetch === 0) { + this.reRender(); + + return; + } + + let $list = this.$el.find(this.listContainerEl); + + let rowCount = this.collection.length - lengthBeforeFetch; + + for (let i = rowCount - 1; i >= 0; i--) { + let model = this.collection.at(i); + + this.buildRow(i, model, view => { + view.getHtml(html => { + let $row = $(this.getRowContainerHtml(model.id)); + + $row.append(html); + $list.prepend($row); + + view._afterRender(); + + if (view.options.el) { + view.setElement(view.options.el); + } + }); + }); + } + }); }, buildRow: function (i, model, callback) { @@ -89,60 +126,19 @@ define('views/stream/record/list', 'views/record/list-expanded', function (Dep) } }); } - } - else { - if (typeof callback === 'function') { - callback(); - this.trigger('after:build-rows'); - } + return; + } + + if (typeof callback === 'function') { + callback(); + + this.trigger('after:build-rows'); } }, showNewRecords: function () { - var collection = this.collection; - var initialCount = collection.length; - - var $list = this.$el.find(this.listContainerEl); - - var success = () => { - if (initialCount === 0) { - this.reRender(); - - return; - } - - var rowCount = collection.length - initialCount; - var rowsReady = 0; - - for (var i = rowCount - 1; i >= 0; i--) { - var model = collection.at(i); - - this.buildRow(i, model, (view) => { - view.getHtml((html) => { - var $row = $(this.getRowContainerHtml(model.id)); - - $row.append(html); - $list.prepend($row); - - rowsReady++; - - view._afterRender(); - - if (view.options.el) { - view.setElement(view.options.el); - } - }); - }); - } - - this.noRebuild = true; - }; - - collection.fetchNew({ - success: success, - }); + this.collection.fetchNew(); }, - }); });