Convert `Discourse.Site` and `Discourse.ScreenTrack` to use `Discourse.Singleton`

This commit is contained in:
Robin Ward 2013-08-08 12:49:58 -04:00
parent 51f5cf77fb
commit 86246a64eb
22 changed files with 42 additions and 48 deletions

View File

@ -67,7 +67,7 @@ Discourse.AdminUser = Discourse.User.extend({
}, },
trustLevels: function() { trustLevels: function() {
return Discourse.Site.instance().get('trustLevels'); return Discourse.Site.currentProp('trustLevels');
}.property(), }.property(),
dirty: Discourse.computed.propertyNotEqual('originalTrustLevel', 'trustLevel.id'), dirty: Discourse.computed.propertyNotEqual('originalTrustLevel', 'trustLevel.id'),

View File

@ -198,7 +198,7 @@ Discourse = Ember.Application.createWithMixins(Discourse.Ajax, {
}), user.notification_channel_position); }), user.notification_channel_position);
bus.subscribe("/categories", function(data){ bus.subscribe("/categories", function(data){
var site = Discourse.Site.instance(); var site = Discourse.Site.current();
_.each(data.categories,function(c){ _.each(data.categories,function(c){
site.updateCategory(c); site.updateCategory(c);
}); });

View File

@ -165,23 +165,13 @@ Discourse.ScreenTrack = Ember.Object.extend({
}); });
Discourse.ScreenTrack.reopenClass({ Discourse.ScreenTrack.reopenClass(Discourse.Singleton, {
// Don't send events if we haven't scrolled in a long time // Don't send events if we haven't scrolled in a long time
PAUSE_UNLESS_SCROLLED: 1000 * 60 * 3, PAUSE_UNLESS_SCROLLED: 1000 * 60 * 3,
// After 6 minutes stop tracking read position on post // After 6 minutes stop tracking read position on post
MAX_TRACKING_TIME: 1000 * 60 * 6, MAX_TRACKING_TIME: 1000 * 60 * 6
/**
Returns a Screen Tracking singleton
**/
instance: function() {
if (this.screenTrack) { return this.screenTrack; }
this.screenTrack = Discourse.ScreenTrack.create();
return this.screenTrack;
}
}); });

View File

@ -13,7 +13,7 @@ Discourse.LoginController = Discourse.Controller.extend(Discourse.ModalFunctiona
loggingIn: false, loggingIn: false,
site: function() { site: function() {
return Discourse.Site.instance(); return Discourse.Site.current();
}.property(), }.property(),
/** /**

View File

@ -222,7 +222,7 @@ Discourse.TopicController = Discourse.ObjectController.extend(Discourse.Selected
}, },
resetRead: function() { resetRead: function() {
Discourse.ScreenTrack.instance().reset(); Discourse.ScreenTrack.current().reset();
this.unsubscribe(); this.unsubscribe();
var topicController = this; var topicController = this;

View File

@ -11,7 +11,7 @@ Discourse.Archetype = Discourse.Model.extend({
hasOptions: Em.computed.gt('options.length', 0), hasOptions: Em.computed.gt('options.length', 0),
site: function() { site: function() {
return Discourse.Site.instance(); return Discourse.Site.current();
}.property(), }.property(),
isDefault: Discourse.computed.propertyEqual('id', 'site.default_archetype'), isDefault: Discourse.computed.propertyEqual('id', 'site.default_archetype'),

View File

@ -125,7 +125,7 @@ Discourse.Category.reopenClass({
}, },
list: function() { list: function() {
return Discourse.Site.instance().get('categories'); return Discourse.Site.currentProp('categories');
}, },
findBySlugOrId: function(slugOrId) { findBySlugOrId: function(slugOrId) {

View File

@ -22,7 +22,7 @@ var CLOSED = 'closed',
Discourse.Composer = Discourse.Model.extend({ Discourse.Composer = Discourse.Model.extend({
archetypes: function() { archetypes: function() {
return Discourse.Site.instance().get('archetypes'); return Discourse.Site.currentProp('archetypes');
}.property(), }.property(),
creatingTopic: Em.computed.equal('action', CREATE_TOPIC), creatingTopic: Em.computed.equal('action', CREATE_TOPIC),
@ -254,7 +254,7 @@ Discourse.Composer = Discourse.Model.extend({
this._super(); this._super();
var val = Discourse.KeyValueStore.get('composer.showPreview') || 'true'; var val = Discourse.KeyValueStore.get('composer.showPreview') || 'true';
this.set('showPreview', val === 'true'); this.set('showPreview', val === 'true');
this.set('archetypeId', Discourse.Site.instance().get('default_archetype')); this.set('archetypeId', Discourse.Site.currentProp('default_archetype'));
}, },
/** /**
@ -329,7 +329,7 @@ Discourse.Composer = Discourse.Model.extend({
this.setProperties({ this.setProperties({
categoryName: opts.categoryName || this.get('topic.category.name'), categoryName: opts.categoryName || this.get('topic.category.name'),
archetypeId: opts.archetypeId || Discourse.Site.instance().get('default_archetype'), archetypeId: opts.archetypeId || Discourse.Site.currentProp('default_archetype'),
metaData: opts.metaData ? Em.Object.create(opts.metaData) : null, metaData: opts.metaData ? Em.Object.create(opts.metaData) : null,
reply: opts.reply || this.get("reply") || "" reply: opts.reply || this.get("reply") || ""
}); });
@ -453,7 +453,7 @@ Discourse.Composer = Discourse.Model.extend({
user_id: currentUser.get('id'), user_id: currentUser.get('id'),
metaData: this.get('metaData'), metaData: this.get('metaData'),
archetype: this.get('archetypeId'), archetype: this.get('archetypeId'),
post_type: Discourse.Site.instance().get('post_types.regular'), post_type: Discourse.Site.currentProp('post_types.regular'),
target_usernames: this.get('targetUsernames'), target_usernames: this.get('targetUsernames'),
actions_summary: Em.A(), actions_summary: Em.A(),
moderator: currentUser.get('moderator'), moderator: currentUser.get('moderator'),

View File

@ -23,7 +23,7 @@ Discourse.NavItem = Discourse.Model.extend({
categorySlug: function() { categorySlug: function() {
var split = this.get('name').split('/'); var split = this.get('name').split('/');
if (split[0] === 'category' && split[1]) { if (split[0] === 'category' && split[1]) {
var cat = Discourse.Site.instance().categories.findProperty('name', split[1]); var cat = Discourse.Site.current().categories.findProperty('name', split[1]);
return cat ? Discourse.Category.slugFor(cat) : null; return cat ? Discourse.Category.slugFor(cat) : null;
} }
return null; return null;

View File

@ -19,7 +19,7 @@ Discourse.Notification = Discourse.Model.extend({
}.property(), }.property(),
rendered: function() { rendered: function() {
var notificationName = Discourse.Site.instance().get('notificationLookup')[this.notification_type]; var notificationName = Discourse.Site.currentProp('notificationLookup')[this.notification_type];
return I18n.t("notifications." + notificationName, { return I18n.t("notifications." + notificationName, {
username: this.data.display_username, username: this.data.display_username,
link: "<a href='" + (this.get('url')) + "'>" + this.data.topic_title + "</a>" link: "<a href='" + (this.get('url')) + "'>" + this.data.topic_title + "</a>"

View File

@ -125,7 +125,7 @@ Discourse.Post = Discourse.Model.extend({
flagsAvailable: function() { flagsAvailable: function() {
var post = this, var post = this,
flags = Discourse.Site.instance().get('flagTypes').filter(function(item) { flags = Discourse.Site.currentProp('flagTypes').filter(function(item) {
return post.get("actionByName." + (item.get('name_key')) + ".can_act"); return post.get("actionByName." + (item.get('name_key')) + ".can_act");
}); });
return flags; return flags;
@ -155,7 +155,7 @@ Discourse.Post = Discourse.Model.extend({
}).then(function(result) { }).then(function(result) {
// If we received a category update, update it // If we received a category update, update it
self.set('version', result.post.version); self.set('version', result.post.version);
if (result.category) Discourse.Site.instance().updateCategory(result.category); if (result.category) Discourse.Site.current().updateCategory(result.category);
if (complete) complete(Discourse.Post.create(result.post)); if (complete) complete(Discourse.Post.create(result.post));
}, function(result) { }, function(result) {
// Post failed to update // Post failed to update
@ -295,7 +295,7 @@ Discourse.Post = Discourse.Model.extend({
_.each(obj.actions_summary,function(a) { _.each(obj.actions_summary,function(a) {
var actionSummary; var actionSummary;
a.post = post; a.post = post;
a.actionType = Discourse.Site.instance().postActionTypeById(a.id); a.actionType = Discourse.Site.current().postActionTypeById(a.id);
actionSummary = Discourse.ActionSummary.create(a); actionSummary = Discourse.ActionSummary.create(a);
post.get('actions_summary').pushObject(actionSummary); post.get('actions_summary').pushObject(actionSummary);
lookup.set(a.actionType.get('name_key'), actionSummary); lookup.set(a.actionType.get('name_key'), actionSummary);
@ -354,7 +354,7 @@ Discourse.Post.reopenClass({
var lookup = Em.Object.create(); var lookup = Em.Object.create();
result.actions_summary = result.actions_summary.map(function(a) { result.actions_summary = result.actions_summary.map(function(a) {
a.post = result; a.post = result;
a.actionType = Discourse.Site.instance().postActionTypeById(a.id); a.actionType = Discourse.Site.current().postActionTypeById(a.id);
var actionSummary = Discourse.ActionSummary.create(a); var actionSummary = Discourse.ActionSummary.create(a);
lookup.set(a.actionType.get('name_key'), actionSummary); lookup.set(a.actionType.get('name_key'), actionSummary);
return actionSummary; return actionSummary;

View File

@ -32,12 +32,16 @@ Discourse.Site = Discourse.Model.extend({
} }
}); });
Discourse.Site.reopenClass({ Discourse.Site.reopenClass(Discourse.Singleton, {
instance: function() { /**
if ( this._site ) return this._site; The current singleton will retrieve its attributes from the `PreloadStore`.
this._site = Discourse.Site.create(PreloadStore.get('site'));
return this._site; @method createCurrent
@returns {Discourse.Site} the site
**/
createCurrent: function() {
return Discourse.Site.create(PreloadStore.get('site'));
}, },
create: function(obj) { create: function(obj) {

View File

@ -130,7 +130,7 @@ Discourse.Topic = Discourse.Model.extend({
}.property('views'), }.property('views'),
archetypeObject: function() { archetypeObject: function() {
return Discourse.Site.instance().get('archetypes').findProperty('id', this.get('archetype')); return Discourse.Site.currentProp('archetypes').findProperty('id', this.get('archetype'));
}.property('archetype'), }.property('archetype'),
isPrivateMessage: Em.computed.equal('archetype', 'private_message'), isPrivateMessage: Em.computed.equal('archetype', 'private_message'),

View File

@ -109,7 +109,7 @@ Discourse.User = Discourse.Model.extend({
@type {Integer} @type {Integer}
**/ **/
trustLevel: function() { trustLevel: function() {
return Discourse.Site.instance().get('trustLevels').findProperty('id', parseInt(this.get('trust_level'), 10)); return Discourse.Site.currentProp('trustLevels').findProperty('id', parseInt(this.get('trust_level'), 10));
}.property('trust_level'), }.property('trust_level'),
/** /**

View File

@ -49,7 +49,7 @@ Discourse.ApplicationRoute = Em.Route.extend({
router.controllerFor('editCategory').set('selectedTab', 'general'); router.controllerFor('editCategory').set('selectedTab', 'general');
} else { } else {
Discourse.Category.findBySlugOrId(category.get('slug') || category.get('id')).then(function (c) { Discourse.Category.findBySlugOrId(category.get('slug') || category.get('id')).then(function (c) {
Discourse.Site.instance().updateCategory(c); Discourse.Site.current().updateCategory(c);
Discourse.Route.showModal(router, 'editCategory', c); Discourse.Route.showModal(router, 'editCategory', c);
router.controllerFor('editCategory').set('selectedTab', 'general'); router.controllerFor('editCategory').set('selectedTab', 'general');
}); });

View File

@ -14,7 +14,7 @@ Discourse.ListCategoriesRoute = Discourse.Route.extend({
createCategory: function() { createCategory: function() {
Discourse.Route.showModal(this, 'editCategory', Discourse.Category.create({ Discourse.Route.showModal(this, 'editCategory', Discourse.Category.create({
color: 'AB9364', text_color: 'FFFFFF', hotness: 5, group_permissions: [{group_name: "everyone", permission_type: 1}], color: 'AB9364', text_color: 'FFFFFF', hotness: 5, group_permissions: [{group_name: "everyone", permission_type: 1}],
available_groups: Discourse.Site.instance().group_names available_groups: Discourse.Site.current().group_names
})); }));
this.controllerFor('editCategory').set('selectedTab', 'general'); this.controllerFor('editCategory').set('selectedTab', 'general');
} }

View File

@ -88,7 +88,7 @@ Discourse.TopicRoute = Discourse.Route.extend({
topicController.set('multiSelect', false); topicController.set('multiSelect', false);
topicController.unsubscribe(); topicController.unsubscribe();
this.controllerFor('composer').set('topic', null); this.controllerFor('composer').set('topic', null);
Discourse.ScreenTrack.instance().stop(); Discourse.ScreenTrack.current().stop();
var headerController; var headerController;
if (headerController = this.controllerFor('header')) { if (headerController = this.controllerFor('header')) {
@ -112,7 +112,7 @@ Discourse.TopicRoute = Discourse.Route.extend({
controller.subscribe(); controller.subscribe();
// We reset screen tracking every time a topic is entered // We reset screen tracking every time a topic is entered
Discourse.ScreenTrack.instance().start(model.get('id')); Discourse.ScreenTrack.current().start(model.get('id'));
} }
}); });

View File

@ -11,11 +11,11 @@ Discourse.EmbeddedPostView = Discourse.GroupedView.extend({
classNames: ['reply'], classNames: ['reply'],
didInsertElement: function() { didInsertElement: function() {
Discourse.ScreenTrack.instance().track(this.get('elementId'), this.get('post.post_number')); Discourse.ScreenTrack.current().track(this.get('elementId'), this.get('post.post_number'));
}, },
willDestroyElement: function() { willDestroyElement: function() {
Discourse.ScreenTrack.instance().stopTracking(this.get('elementId')); Discourse.ScreenTrack.current().stopTracking(this.get('elementId'));
} }
}); });

View File

@ -16,7 +16,7 @@ Discourse.PostView = Discourse.GroupedView.extend({
postBinding: 'content', postBinding: 'content',
postTypeClass: function() { postTypeClass: function() {
return this.get('post.post_type') === Discourse.Site.instance().get('post_types.moderator_action') ? 'moderator' : 'regular'; return this.get('post.post_type') === Discourse.Site.currentProp('post_types.moderator_action') ? 'moderator' : 'regular';
}.property('post.post_type'), }.property('post.post_type'),
// If the cooked content changed, add the quote controls // If the cooked content changed, add the quote controls
@ -153,7 +153,7 @@ Discourse.PostView = Discourse.GroupedView.extend({
}, },
willDestroyElement: function() { willDestroyElement: function() {
Discourse.ScreenTrack.instance().stopTracking(this.$().prop('id')); Discourse.ScreenTrack.current().stopTracking(this.$().prop('id'));
}, },
didInsertElement: function() { didInsertElement: function() {
@ -162,7 +162,7 @@ Discourse.PostView = Discourse.GroupedView.extend({
this.showLinkCounts(); this.showLinkCounts();
// Track this post // Track this post
Discourse.ScreenTrack.instance().track(this.$().prop('id'), this.get('post.post_number')); Discourse.ScreenTrack.current().track(this.$().prop('id'), this.get('post.post_number'));
// Add syntax highlighting // Add syntax highlighting
Discourse.SyntaxHighlighting.apply($post); Discourse.SyntaxHighlighting.apply($post);

View File

@ -193,7 +193,7 @@ Discourse.TopicView = Discourse.View.extend(Discourse.Scrolling, {
}, },
throttledPositionUpdate: Discourse.debounce(function() { throttledPositionUpdate: Discourse.debounce(function() {
Discourse.ScreenTrack.instance().scrolled(); Discourse.ScreenTrack.current().scrolled();
var model = this.get('controller.model'); var model = this.get('controller.model');
if (model && this.get('nextPositionUpdate')) { if (model && this.get('nextPositionUpdate')) {
this.set('controller.currentPost', this.get('nextPositionUpdate')); this.set('controller.currentPost', this.get('nextPositionUpdate'));

View File

@ -1,7 +1,7 @@
module("Discourse.NavItem", { module("Discourse.NavItem", {
setup: function() { setup: function() {
this.site = Discourse.Site.instance(); this.site = Discourse.Site.current();
this.originalCategories = Discourse.Site.instance().get('categories') || []; this.originalCategories = Discourse.Site.currentProp('categories') || [];
this.site.set('categories', this.originalCategories.concat( [Discourse.Category.create({name: '确实是这样', id: 343434})] )); this.site.set('categories', this.originalCategories.concat( [Discourse.Category.create({name: '确实是这样', id: 343434})] ));
}, },

View File

@ -2,7 +2,7 @@ module("Discourse.Site");
test('instance', function(){ test('instance', function(){
var site = Discourse.Site.instance(); var site = Discourse.Site.current();
present(site, "We have a current site singleton"); present(site, "We have a current site singleton");
present(site.get('categories'), "The instance has a list of categories"); present(site.get('categories'), "The instance has a list of categories");