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() {
return Discourse.Site.instance().get('trustLevels');
return Discourse.Site.currentProp('trustLevels');
}.property(),
dirty: Discourse.computed.propertyNotEqual('originalTrustLevel', 'trustLevel.id'),

View File

@ -198,7 +198,7 @@ Discourse = Ember.Application.createWithMixins(Discourse.Ajax, {
}), user.notification_channel_position);
bus.subscribe("/categories", function(data){
var site = Discourse.Site.instance();
var site = Discourse.Site.current();
_.each(data.categories,function(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
PAUSE_UNLESS_SCROLLED: 1000 * 60 * 3,
// After 6 minutes stop tracking read position on post
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;
}
MAX_TRACKING_TIME: 1000 * 60 * 6
});

View File

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

View File

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

View File

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

View File

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

View File

@ -22,7 +22,7 @@ var CLOSED = 'closed',
Discourse.Composer = Discourse.Model.extend({
archetypes: function() {
return Discourse.Site.instance().get('archetypes');
return Discourse.Site.currentProp('archetypes');
}.property(),
creatingTopic: Em.computed.equal('action', CREATE_TOPIC),
@ -254,7 +254,7 @@ Discourse.Composer = Discourse.Model.extend({
this._super();
var val = Discourse.KeyValueStore.get('composer.showPreview') || '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({
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,
reply: opts.reply || this.get("reply") || ""
});
@ -453,7 +453,7 @@ Discourse.Composer = Discourse.Model.extend({
user_id: currentUser.get('id'),
metaData: this.get('metaData'),
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'),
actions_summary: Em.A(),
moderator: currentUser.get('moderator'),

View File

@ -23,7 +23,7 @@ Discourse.NavItem = Discourse.Model.extend({
categorySlug: function() {
var split = this.get('name').split('/');
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 null;

View File

@ -19,7 +19,7 @@ Discourse.Notification = Discourse.Model.extend({
}.property(),
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, {
username: this.data.display_username,
link: "<a href='" + (this.get('url')) + "'>" + this.data.topic_title + "</a>"

View File

@ -125,7 +125,7 @@ Discourse.Post = Discourse.Model.extend({
flagsAvailable: function() {
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 flags;
@ -155,7 +155,7 @@ Discourse.Post = Discourse.Model.extend({
}).then(function(result) {
// If we received a category update, update it
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));
}, function(result) {
// Post failed to update
@ -295,7 +295,7 @@ Discourse.Post = Discourse.Model.extend({
_.each(obj.actions_summary,function(a) {
var actionSummary;
a.post = post;
a.actionType = Discourse.Site.instance().postActionTypeById(a.id);
a.actionType = Discourse.Site.current().postActionTypeById(a.id);
actionSummary = Discourse.ActionSummary.create(a);
post.get('actions_summary').pushObject(actionSummary);
lookup.set(a.actionType.get('name_key'), actionSummary);
@ -354,7 +354,7 @@ Discourse.Post.reopenClass({
var lookup = Em.Object.create();
result.actions_summary = result.actions_summary.map(function(a) {
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);
lookup.set(a.actionType.get('name_key'), 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;
this._site = Discourse.Site.create(PreloadStore.get('site'));
return this._site;
/**
The current singleton will retrieve its attributes from the `PreloadStore`.
@method createCurrent
@returns {Discourse.Site} the site
**/
createCurrent: function() {
return Discourse.Site.create(PreloadStore.get('site'));
},
create: function(obj) {

View File

@ -130,7 +130,7 @@ Discourse.Topic = Discourse.Model.extend({
}.property('views'),
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'),
isPrivateMessage: Em.computed.equal('archetype', 'private_message'),

View File

@ -109,7 +109,7 @@ Discourse.User = Discourse.Model.extend({
@type {Integer}
**/
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'),
/**

View File

@ -49,7 +49,7 @@ Discourse.ApplicationRoute = Em.Route.extend({
router.controllerFor('editCategory').set('selectedTab', 'general');
} else {
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);
router.controllerFor('editCategory').set('selectedTab', 'general');
});

View File

@ -14,7 +14,7 @@ Discourse.ListCategoriesRoute = Discourse.Route.extend({
createCategory: function() {
Discourse.Route.showModal(this, 'editCategory', Discourse.Category.create({
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');
}

View File

@ -88,7 +88,7 @@ Discourse.TopicRoute = Discourse.Route.extend({
topicController.set('multiSelect', false);
topicController.unsubscribe();
this.controllerFor('composer').set('topic', null);
Discourse.ScreenTrack.instance().stop();
Discourse.ScreenTrack.current().stop();
var headerController;
if (headerController = this.controllerFor('header')) {
@ -112,7 +112,7 @@ Discourse.TopicRoute = Discourse.Route.extend({
controller.subscribe();
// 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'],
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() {
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',
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'),
// If the cooked content changed, add the quote controls
@ -153,7 +153,7 @@ Discourse.PostView = Discourse.GroupedView.extend({
},
willDestroyElement: function() {
Discourse.ScreenTrack.instance().stopTracking(this.$().prop('id'));
Discourse.ScreenTrack.current().stopTracking(this.$().prop('id'));
},
didInsertElement: function() {
@ -162,7 +162,7 @@ Discourse.PostView = Discourse.GroupedView.extend({
this.showLinkCounts();
// 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
Discourse.SyntaxHighlighting.apply($post);

View File

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

View File

@ -1,7 +1,7 @@
module("Discourse.NavItem", {
setup: function() {
this.site = Discourse.Site.instance();
this.originalCategories = Discourse.Site.instance().get('categories') || [];
this.site = Discourse.Site.current();
this.originalCategories = Discourse.Site.currentProp('categories') || [];
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(){
var site = Discourse.Site.instance();
var site = Discourse.Site.current();
present(site, "We have a current site singleton");
present(site.get('categories'), "The instance has a list of categories");