diff --git a/app/assets/javascripts/discourse.js b/app/assets/javascripts/discourse.js index 03f79618618..a243ec161a8 100644 --- a/app/assets/javascripts/discourse.js +++ b/app/assets/javascripts/discourse.js @@ -10,9 +10,6 @@ Discourse = Ember.Application.createWithMixins({ rootElement: '#main', - // Data we want to remember for a short period - transient: Em.Object.create(), - // Whether the app has focus or not hasFocus: true, @@ -69,8 +66,7 @@ Discourse = Ember.Application.createWithMixins({ }), titleChanged: function() { - var title; - title = ""; + var title = ""; if (this.get('title')) { title += "" + (this.get('title')) + " - "; } diff --git a/app/assets/javascripts/discourse/mixins/load_more.js b/app/assets/javascripts/discourse/mixins/load_more.js index 388fc2e938a..f698478bf20 100644 --- a/app/assets/javascripts/discourse/mixins/load_more.js +++ b/app/assets/javascripts/discourse/mixins/load_more.js @@ -31,7 +31,7 @@ Discourse.LoadMore = Em.Mixin.create(Discourse.Scrolling, { this.bindScrolling(); }, - willRemoveElement: function() { + willDestroyElement: function() { this._super(); this.unbindScrolling(); } diff --git a/app/assets/javascripts/discourse/models/session.js b/app/assets/javascripts/discourse/models/session.js new file mode 100644 index 00000000000..de7147772cb --- /dev/null +++ b/app/assets/javascripts/discourse/models/session.js @@ -0,0 +1,35 @@ +/** + A data model representing current session data. You can put transient + data here you might want later. + + @class Session + @extends Discourse.Model + @namespace Discourse + @module Discourse +**/ +Discourse.Session = Discourse.Model.extend({}); + +Discourse.Session.reopenClass({ + /** + Returns the current session. + + @method current + @returns {Discourse.Session} the current session singleton + **/ + current: function(property, value) { + if (!this.currentSession) { + this.currentSession = Discourse.Session.create(); + } + + // If we found the current session + if (property) { + if (value) { + this.currentSession.set(property, value); + } else { + return this.currentSession.get(property); + } + } + + return property ? this.currentSession.get(property) : this.currentSession; + } +}); diff --git a/app/assets/javascripts/discourse/models/topic_list.js b/app/assets/javascripts/discourse/models/topic_list.js index 5c94661e05e..0155bd536b5 100644 --- a/app/assets/javascripts/discourse/models/topic_list.js +++ b/app/assets/javascripts/discourse/models/topic_list.js @@ -45,7 +45,7 @@ Discourse.TopicList = Discourse.Model.extend({ }); topicList.set('more_topics_url', result.topic_list.more_topics_url); - Discourse.set('transient.topicsList', topicList); + Discourse.Session.current('topicList', topicList); topicList.set('loadingMore', false); return result.topic_list.more_topics_url; @@ -60,8 +60,8 @@ Discourse.TopicList = Discourse.Model.extend({ // loads topics with these ids "before" the current topics loadBefore: function(topic_ids){ - var _this = this; - var topics = this.get('topics'); + var topicList = this, + topics = this.get('topics'); // refresh dupes topics.removeObjects(topics.filter(function(topic){ @@ -70,13 +70,12 @@ Discourse.TopicList = Discourse.Model.extend({ Discourse.TopicList.loadTopics(topic_ids, this.get('filter')) .then(function(newTopics){ - _this.forEachNew(newTopics, function(t) { + topicList.forEachNew(newTopics, 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.Session.current('topicList', topicList); }); } }); @@ -128,17 +127,17 @@ Discourse.TopicList.reopenClass({ list: function(menuItem) { var filter = menuItem.get('name'); - var list = Discourse.get('transient.topicsList'); + var session = Discourse.Session.current(); + var list = session.get('topicList'); if (list) { if ((list.get('filter') === filter) && window.location.pathname.indexOf('more') > 0) { list.set('loaded', true); - return Ember.Deferred.promise(function(promise) { - promise.resolve(list); - }); + return Ember.RSVP.resolve(list); } } - Discourse.set('transient.topicsList', null); - Discourse.set('transient.topicListScrollPos', null); + + session.set('topicList', null); + session.set('topicListScrollPos', null); return Discourse.TopicList.find(filter, menuItem.get('excludeCategory')); } diff --git a/app/assets/javascripts/discourse/routes/filtered_list_route.js b/app/assets/javascripts/discourse/routes/filtered_list_route.js index 163a08400a9..1f7b5703a06 100644 --- a/app/assets/javascripts/discourse/routes/filtered_list_route.js +++ b/app/assets/javascripts/discourse/routes/filtered_list_route.js @@ -40,6 +40,14 @@ Discourse.FilteredListRoute = Discourse.Route.extend({ listController.set('category', null); listController.set('canCreateTopic', topicList.get('can_create_topic')); listTopicsController.set('model', topicList); + + var scrollPos = Discourse.Session.current('topicListScrollPosition'); + if (scrollPos) { + Em.run.next(function() { + $('html, body').scrollTop(scrollPos); + }); + Discourse.Session.current().set('topicListScrollPosition', null); + } }); } }); diff --git a/app/assets/javascripts/discourse/routes/topic_route.js b/app/assets/javascripts/discourse/routes/topic_route.js index 6b3ac7f0bd3..40e4a19907c 100644 --- a/app/assets/javascripts/discourse/routes/topic_route.js +++ b/app/assets/javascripts/discourse/routes/topic_route.js @@ -71,9 +71,7 @@ Discourse.TopicRoute = Discourse.Route.extend({ this._super(); var topic = this.modelFor('topic'); - Discourse.set('transient.lastTopicIdViewed', parseInt(topic.get('id'), 10)); - - // Set the search context + Discourse.Session.current('lastTopicIdViewed', parseInt(topic.get('id'), 10)); this.controllerFor('search').set('searchContext', topic.get('searchContext')); }, diff --git a/app/assets/javascripts/discourse/routes/user_routes.js b/app/assets/javascripts/discourse/routes/user_routes.js index f3977032db1..f5a98b1fb8b 100644 --- a/app/assets/javascripts/discourse/routes/user_routes.js +++ b/app/assets/javascripts/discourse/routes/user_routes.js @@ -162,7 +162,6 @@ Discourse.UserPrivateMessagesSentRoute = Discourse.UserActivityStreamRoute.exten userActionType: Discourse.UserAction.TYPES.messages_sent }); -//Discourse.UserTopicsListView = Em.View.extend({ templateName: 'user/topics_list' }); Discourse.UserTopicListRoute = Discourse.Route.extend({ renderTemplate: function() { diff --git a/app/assets/javascripts/discourse/views/list/list_topics_view.js b/app/assets/javascripts/discourse/views/list/list_topics_view.js index 9bf5e51a5cc..37f49f8d1c2 100644 --- a/app/assets/javascripts/discourse/views/list/list_topics_view.js +++ b/app/assets/javascripts/discourse/views/list/list_topics_view.js @@ -22,16 +22,9 @@ Discourse.ListTopicsView = Discourse.View.extend(Discourse.LoadMore, { didInsertElement: function() { this._super(); - var scrollPos = Discourse.get('transient.topicListScrollPos'); - if (scrollPos) { - Em.run.schedule('afterRender', function() { - $('html, body').scrollTop(scrollPos); - }); - } else { - Em.run.schedule('afterRender', function() { - $('html, body').scrollTop(0); - }); - } + Em.run.schedule('afterRender', function() { + $('html, body').scrollTop(0); + }); }, hasTopics: Em.computed.gt('list.topics.length', 0), @@ -46,7 +39,7 @@ Discourse.ListTopicsView = Discourse.View.extend(Discourse.LoadMore, { Discourse.notifyTitle(0); listTopicsView.get('controller').loadMore().then(function (hasMoreResults) { Em.run.schedule('afterRender', function() { - listTopicsView.saveScrollPos(); + listTopicsView.saveScrollPosition(); }); if (!hasMoreResults) { listTopicsView.get('eyeline').flushRest(); @@ -55,14 +48,14 @@ Discourse.ListTopicsView = Discourse.View.extend(Discourse.LoadMore, { }, // Remember where we were scrolled to - saveScrollPos: function() { - return Discourse.set('transient.topicListScrollPos', $(window).scrollTop()); + saveScrollPosition: function() { + Discourse.Session.current('topicListScrollPosition', $(window).scrollTop()); }, // When the topic list is scrolled scrolled: function(e) { this._super(); - this.saveScrollPos(); + this.saveScrollPosition(); } diff --git a/app/assets/javascripts/discourse/views/list/topic_list_item_view.js b/app/assets/javascripts/discourse/views/list/topic_list_item_view.js index ca3d19f82f2..5a69bd02281 100644 --- a/app/assets/javascripts/discourse/views/list/topic_list_item_view.js +++ b/app/assets/javascripts/discourse/views/list/topic_list_item_view.js @@ -16,7 +16,7 @@ Discourse.TopicListItemView = Discourse.View.extend({ init: function() { this._super(); - return this.set('context', this.get('content')); + this.set('context', this.get('content')); }, highlight: function() { @@ -30,13 +30,14 @@ Discourse.TopicListItemView = Discourse.View.extend({ }, didInsertElement: function() { - // highligth the last topic viewed - if (Discourse.get('transient.lastTopicIdViewed') === this.get('content.id')) { - Discourse.set('transient.lastTopicIdViewed', null); + var session = Discourse.Session.current(); + + // // highligth the last topic viewed + if (session.get('lastTopicIdViewed') === this.get('content.id')) { + session.set('lastTopicIdViewed', null); this.highlight(); - } - // highlight new topics that have been loaded from the server or the one we just created - else if (this.get('content.highlight')) { + } else if (this.get('content.highlight')) { + // highlight new topics that have been loaded from the server or the one we just created this.set('content.highlight', false); this.highlight(); } diff --git a/test/javascripts/models/session_test.js b/test/javascripts/models/session_test.js new file mode 100644 index 00000000000..61a95be0161 --- /dev/null +++ b/test/javascripts/models/session_test.js @@ -0,0 +1,16 @@ +module("Discourse.Session"); + +test('current', function(){ + var session = Discourse.Session.current(); + + present(session, "We have a current site session"); + equal(session, Discourse.Session.current(), "Calling it a second time returns the same instance"); + + blank(Discourse.Session.current('orange'), "by default properties are nil"); + session.set('orange', 'newBlack'); + equal(Discourse.Session.current('orange'), "newBlack", "it remembers values"); + + Discourse.Session.current('orange', 'juice'); + equal(session.get('orange'), "juice", "it can be updated"); + +}); \ No newline at end of file