auto refresh suggested list

This commit is contained in:
Sam 2013-06-03 14:54:51 +10:00
parent 3ef16f292d
commit 978bc0ca4e
3 changed files with 51 additions and 29 deletions

View file

@ -55,45 +55,41 @@ Discourse.TopicList = Discourse.Model.extend({
// loads topics with these ids "before" the current topics
loadBefore: function(topic_ids){
// filter out any existing topics
var _this = this;
var url = Discourse.getURL("/") + (this.get('filter')) + "?topic_ids=" + topic_ids.join(",");
return Discourse.ajax({url: url}).then(function (result) {
if (result) {
// the new topics loaded from the server
var newTopics = Discourse.TopicList.topicsFrom(result);
Discourse.TopicList.loadTopics(topic_ids, this.get('filter'))
.then(function(topics){
var mapped = topic_ids.map(function(id){
return newTopics.find(function(t){ return t.id === id; });
});
var topics = _this.get("topics");
// add new topics to the list of current topics if not already present
_this.forEachNew(mapped, function(t) {
_this.forEachNew(topics, function(t) {
// highlight the first of the new topics so we can get a visual feedback
t.set('highlight', true);
topics.insertAt(0,t);
});
Discourse.set('transient.topicsList', _this);
}
});
});
}
});
Discourse.TopicList.reopenClass({
decodeTopic: function(result) {
var categories, topic, users;
categories = this.extractByKey(result.categories, Discourse.Category);
users = this.extractByKey(result.users, Discourse.User);
topic = result.topic_list_item;
topic.category = categories[topic.category];
topic.posters.each(function(p) {
p.user = users[p.user_id] || users[p.user];
});
return Discourse.Topic.create(topic);
loadTopics: function(topic_ids, filter) {
var defer = new Ember.Deferred();
var url = Discourse.getURL("/") + filter + "?topic_ids=" + topic_ids.join(",");
Discourse.ajax({url: url}).then(function (result) {
if (result) {
// the new topics loaded from the server
var newTopics = Discourse.TopicList.topicsFrom(result);
defer.resolve(topic_ids.map(function(id){
return newTopics.find(function(t){ return t.id === id; });
}));
} else {
defer.reject();
}
}).then(null, function(){ defer.reject(); });
return defer;
},
topicsFrom: function(result) {

View file

@ -35,10 +35,10 @@ Discourse.TopicTrackingState = Discourse.Model.extend({
notify: function(data){
if (!this.newIncoming) { return; }
if ((this.filter === "latest" || this.filter === "new") && data.message_type === "new_topic" ) {
if ((this.filter === "all" ||this.filter === "latest" || this.filter === "new") && data.message_type === "new_topic" ) {
this.newIncoming.push(data.topic_id);
}
if (this.filter === "unread" && data.message_type === "unread") {
if ((this.filter === "all" || this.filter === "unread") && data.message_type === "unread") {
var old = this.states["t" + data.topic_id];
if(!old) {
this.newIncoming.push(data.topic_id);

View file

@ -136,8 +136,34 @@ Discourse.TopicView = Discourse.View.extend(Discourse.Scrolling, {
});
this.updatePosition(true);
// Watch all incoming topic changes
this.get('topicTrackingState').trackIncoming("all");
},
hasNewSuggested: function(){
var incoming = this.get('topicTrackingState.newIncoming');
var suggested = this.get('topic.suggested_topics');
if(suggested) {
var lookup = incoming.slice(-5).reverse().unique();
if(lookup.length < 5) {
suggested.each(function(topic){
if (topic) {
lookup.push(topic.get('id'));
lookup = lookup.unique();
return lookup.length < 5;
}
});
}
Discourse.TopicList.loadTopics(lookup, "").then(function(topics){
suggested.clear();
suggested.pushObjects(topics);
});
}
}.observes('topicTrackingState.incomingCount'),
// Triggered whenever any posts are rendered, debounced to save over calling
postsRendered: Discourse.debounce(function() {
this.set('renderedPosts', $('.topic-post'));