From 978bc0ca4e7333f0c20d2dbf5259b095eebbf641 Mon Sep 17 00:00:00 2001 From: Sam Date: Mon, 3 Jun 2013 14:54:51 +1000 Subject: [PATCH] auto refresh suggested list --- .../discourse/models/topic_list.js | 50 +++++++++---------- .../discourse/models/topic_tracking_state.js | 4 +- .../javascripts/discourse/views/topic_view.js | 26 ++++++++++ 3 files changed, 51 insertions(+), 29 deletions(-) diff --git a/app/assets/javascripts/discourse/models/topic_list.js b/app/assets/javascripts/discourse/models/topic_list.js index 81b42dda3..703575fed 100644 --- a/app/assets/javascripts/discourse/models/topic_list.js +++ b/app/assets/javascripts/discourse/models/topic_list.js @@ -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) { diff --git a/app/assets/javascripts/discourse/models/topic_tracking_state.js b/app/assets/javascripts/discourse/models/topic_tracking_state.js index b9983130f..3958587c2 100644 --- a/app/assets/javascripts/discourse/models/topic_tracking_state.js +++ b/app/assets/javascripts/discourse/models/topic_tracking_state.js @@ -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); diff --git a/app/assets/javascripts/discourse/views/topic_view.js b/app/assets/javascripts/discourse/views/topic_view.js index 384c01622..a86d20e0a 100644 --- a/app/assets/javascripts/discourse/views/topic_view.js +++ b/app/assets/javascripts/discourse/views/topic_view.js @@ -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'));