FIX: navigation item counters weren't updating properly

This commit is contained in:
Régis Hanol 2017-08-10 12:22:15 +02:00
parent f7d3702454
commit 05a74d2534
3 changed files with 20 additions and 7 deletions

View File

@ -80,8 +80,9 @@ const NavItem = Discourse.Model.extend({
}
},
@computed("topicTrackingState", "name", "category")
count(state, name, category) {
@computed("name", "category", "topicTrackingState.messageCount")
count(name, category) {
const state = this.get("topicTrackingState");
if (state) {
return state.lookupCount(name, category);
}

View File

@ -179,8 +179,7 @@ const TopicTrackingState = Discourse.Model.extend({
delete this.states["t" + topic_id];
},
// If we have a cached topic list, we can update it from our tracking
// information.
// If we have a cached topic list, we can update it from our tracking information.
updateTopics(topics) {
if (Em.isEmpty(topics)) { return; }
@ -283,7 +282,7 @@ const TopicTrackingState = Discourse.Model.extend({
},
incrementMessageCount() {
this.set("messageCount", this.get("messageCount") + 1);
this.incrementProperty("messageCount");
},
countNew(category_id) {
@ -358,8 +357,7 @@ const TopicTrackingState = Discourse.Model.extend({
const states = this.states;
const idMap = Discourse.Category.idMap();
// I am taking some shortcuts here to avoid 500 gets for
// a large list
// I am taking some shortcuts here to avoid 500 gets for a large list
if (data) {
_.each(data,topic => {
var category = idMap[topic.category_id];

View File

@ -1,3 +1,5 @@
import createStore from 'helpers/create-store';
QUnit.module("Discourse.NavItem", {
beforeEach() {
Ember.run(function() {
@ -19,3 +21,15 @@ QUnit.test('href', assert =>{
href('category/bug', '/c/bug', 'English category name');
href('category/确实是这样', '/c/343434-category', 'Chinese category name');
});
QUnit.test("count", assert => {
const navItem = createStore().createRecord("nav-item", { name: "new" });
assert.equal(navItem.get("count"), 0, "it has no count by default");
const tracker = navItem.get("topicTrackingState");
tracker.states["t1"] = { topic_id: 1, last_read_post_number: null };
tracker.incrementMessageCount();
assert.equal(navItem.get("count"), 1, "it updates when a new message arrives");
});