client-app/app/models/message-collection.js in logster-1.3.4 vs client-app/app/models/message-collection.js in logster-1.4.0.pre
- old
+ new
@@ -1,10 +1,12 @@
import { ajax, increaseTitleCount } from "client-app/lib/utilities";
import Message from "client-app/models/message";
import { compare } from "@ember/utils";
import { computed } from "@ember/object";
+const BATCH_SIZE = 50;
+
export default Em.Object.extend({
messages: Em.A(),
currentMessage: null,
total: 0,
@@ -59,11 +61,11 @@
if (opts.after) {
data.after = opts.after;
}
- ajax("/messages.json", {
+ return ajax("/messages.json", {
data: data
}).then(data => {
// guard against race: ensure the results we're trying to apply
// match the current search terms
if (compare(data.filter, this.get("filter")) != 0) {
@@ -96,20 +98,32 @@
increaseTitleCount(newRows.length);
}
}
}
this.set("total", data.total);
+ return data;
});
},
reload() {
this.set("total", 0);
this.get("messages").clear();
- this.load();
+ return this.load().then(data => this.updateCanLoadMore(data));
},
+ updateCanLoadMore(data) {
+ if (!data) {
+ return;
+ }
+ if (data.messages.length < BATCH_SIZE) {
+ this.set("canLoadMore", false);
+ } else {
+ this.set("canLoadMore", true);
+ }
+ },
+
loadMore() {
const messages = this.get("messages");
if (messages.length === 0) {
this.load({});
return;
@@ -119,24 +133,30 @@
this.load({
after: lastKey
});
},
- moreBefore: computed("totalBefore", function() {
- return this.get("totalBefore") > 0;
+ hideCountInLoadMore: computed("search", "filter", function() {
+ const search = this.get("search");
+ const filter = this.get("filter");
+ return (search && search.length > 0) || (filter && filter.length < 6);
}),
+ moreBefore: computed("messages.length", "canLoadMore", function() {
+ return this.get("messages.length") >= BATCH_SIZE && this.get("canLoadMore");
+ }),
+
totalBefore: computed("total", "messages.length", function() {
return this.get("total") - this.get("messages").length;
}),
showMoreBefore: function() {
const messages = this.get("messages");
const firstKey = messages[0].get("key");
this.load({
before: firstKey
- });
+ }).then(data => this.updateCanLoadMore(data));
},
regexSearch: computed("search", function() {
const search = this.get("search");
if (search && search.length > 2 && search[0] === "/") {