REFACTOR: user-posts-stream model (#7657)

This commit is contained in:
Joffrey JAFFEUX 2019-05-31 13:02:15 +02:00 committed by GitHub
parent 388433ec06
commit 63264158cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,3 +1,4 @@
import { on } from "ember-addons/ember-computed-decorators";
import { ajax } from "discourse/lib/ajax";
import { url } from "discourse/lib/computed";
import UserAction from "discourse/models/user-action";
@ -5,13 +6,14 @@ import UserAction from "discourse/models/user-action";
export default Discourse.Model.extend({
loaded: false,
_initialize: function() {
@on("init")
_initialize() {
this.setProperties({
itemsLoaded: 0,
canLoadMore: true,
content: []
});
}.on("init"),
},
url: url(
"user.username_lower",
@ -40,7 +42,6 @@ export default Discourse.Model.extend({
},
findItems() {
const self = this;
if (this.loading || !this.canLoadMore) {
return Ember.RSVP.reject();
}
@ -48,21 +49,17 @@ export default Discourse.Model.extend({
this.set("loading", true);
return ajax(this.url, { cache: false })
.then(function(result) {
.then(result => {
if (result) {
const posts = result.map(function(post) {
return UserAction.create(post);
});
self.get("content").pushObjects(posts);
self.setProperties({
const posts = result.map(post => UserAction.create(post));
this.content.pushObjects(posts);
this.setProperties({
loaded: true,
itemsLoaded: self.get("itemsLoaded") + posts.length,
itemsLoaded: this.itemsLoaded + posts.length,
canLoadMore: posts.length > 0
});
}
})
.finally(function() {
self.set("loading", false);
});
.finally(() => this.set("loading", false));
}
});