Remove Discourse.transient. Use a singleton for session data, it's a lot cleaner.
This commit is contained in:
parent
fca83cb185
commit
773823c41f
|
@ -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')) + " - ";
|
||||
}
|
||||
|
|
|
@ -31,7 +31,7 @@ Discourse.LoadMore = Em.Mixin.create(Discourse.Scrolling, {
|
|||
this.bindScrolling();
|
||||
},
|
||||
|
||||
willRemoveElement: function() {
|
||||
willDestroyElement: function() {
|
||||
this._super();
|
||||
this.unbindScrolling();
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
});
|
|
@ -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'));
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
|
|
@ -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'));
|
||||
},
|
||||
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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");
|
||||
|
||||
});
|
Loading…
Reference in New Issue