From 70fb2431a1b82b20f1a0b6e09db30f98331fb8cf Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Fri, 4 Nov 2016 11:32:12 -0400 Subject: [PATCH] Migrate `this.container` to `getOwner(this)` --- .../admin/components/customize-link.js.es6 | 4 ++- .../discourse-common/lib/deprecated.js.es6 | 3 ++ .../discourse-common/lib/get-owner.js.es6 | 32 +++++++++++++++++++ .../components/badge-selector.js.es6 | 3 +- .../components/category-selector.js.es6 | 3 +- .../components/composer-editor.js.es6 | 4 +-- .../components/composer-message.js.es6 | 3 +- .../discourse/components/d-editor.js.es6 | 30 ++++++++++------- .../components/group-selector.js.es6 | 3 +- .../components/header-extra-info.js.es6 | 4 ++- .../discourse/components/mount-widget.js.es6 | 9 ++++-- .../discourse/components/nav-item.js.es6 | 3 +- .../components/topic-list-item.js.es6 | 5 +-- .../discourse/components/user-selector.js.es6 | 3 +- .../discourse/controllers/composer.js.es6 | 7 ++-- .../discourse/controllers/header.js.es6 | 3 +- .../discourse/lib/emoji/toolbar.js.es6 | 2 +- .../javascripts/discourse/lib/mobile.js.es6 | 7 ++-- .../discourse/lib/page-tracker.js.es6 | 4 ++- .../javascripts/discourse/lib/url.js.es6 | 3 +- .../javascripts/discourse/models/store.js.es6 | 18 +++++++---- .../pre-initializers/map-routes.js.es6 | 5 ++- .../discourse/routes/application.js.es6 | 7 ++-- .../discourse/routes/full-page-search.js.es6 | 3 +- .../discourse/routes/topic-from-params.js.es6 | 6 ++-- .../discourse/templates/composer.hbs | 2 +- .../discourse/widgets/connector.js.es6 | 2 +- .../discourse/widgets/decorator-helper.js.es6 | 3 +- .../discourse/widgets/hamburger-menu.js.es6 | 2 +- .../discourse/widgets/header.js.es6 | 8 ++--- .../javascripts/discourse/widgets/link.js.es6 | 2 +- .../widgets/search-menu-controls.js.es6 | 2 +- .../discourse/widgets/search-menu.js.es6 | 2 +- .../discourse/widgets/widget.js.es6 | 28 ++++++++-------- .../components/wizard-field-image.js.es6 | 3 +- 35 files changed, 154 insertions(+), 74 deletions(-) create mode 100644 app/assets/javascripts/discourse-common/lib/deprecated.js.es6 create mode 100644 app/assets/javascripts/discourse-common/lib/get-owner.js.es6 diff --git a/app/assets/javascripts/admin/components/customize-link.js.es6 b/app/assets/javascripts/admin/components/customize-link.js.es6 index 79d8cc26f4a..0600f6b5cdb 100644 --- a/app/assets/javascripts/admin/components/customize-link.js.es6 +++ b/app/assets/javascripts/admin/components/customize-link.js.es6 @@ -1,6 +1,8 @@ +import { getOwner } from 'discourse-common/lib/get-owner'; + export default Ember.Component.extend({ router: function() { - return this.container.lookup('router:main'); + return getOwner(this).lookup('router:main'); }.property(), active: function() { diff --git a/app/assets/javascripts/discourse-common/lib/deprecated.js.es6 b/app/assets/javascripts/discourse-common/lib/deprecated.js.es6 new file mode 100644 index 00000000000..02daf6ebdac --- /dev/null +++ b/app/assets/javascripts/discourse-common/lib/deprecated.js.es6 @@ -0,0 +1,3 @@ +export default function deprecated(msg) { + console.warn(`DEPRECATION: ${msg}`); +} diff --git a/app/assets/javascripts/discourse-common/lib/get-owner.js.es6 b/app/assets/javascripts/discourse-common/lib/get-owner.js.es6 new file mode 100644 index 00000000000..ada334f0752 --- /dev/null +++ b/app/assets/javascripts/discourse-common/lib/get-owner.js.es6 @@ -0,0 +1,32 @@ +import deprecated from 'discourse-common/lib/deprecated'; + +export function getOwner(obj) { + if (Ember.getOwner) { + return Ember.getOwner(obj) || Discourse.__container__; + } + + return obj.container; +} + +// `this.container` is deprecated, but we can still build a container-like +// object for components to use +export function getRegister(obj) { + const owner = getOwner(obj); + const register = { + lookup: (...args) => owner.lookup(...args), + lookupFactory: (...args) => { + return owner.lookupFactory ? owner.lookupFactory(...args) : owner._lookupFactory(...args); + }, + + deprecateContainer(target) { + Object.defineProperty(target, 'container', { + get() { + deprecated("Use `this.register` or `getOwner` instead of `this.container`"); + return register; + } + }); + } + }; + + return register; +} diff --git a/app/assets/javascripts/discourse/components/badge-selector.js.es6 b/app/assets/javascripts/discourse/components/badge-selector.js.es6 index 14b1e5349ee..315f7398a78 100644 --- a/app/assets/javascripts/discourse/components/badge-selector.js.es6 +++ b/app/assets/javascripts/discourse/components/badge-selector.js.es6 @@ -1,4 +1,5 @@ import { on, observes, default as computed } from 'ember-addons/ember-computed-decorators'; +import { getOwner } from 'discourse-common/lib/get-owner'; export default Ember.Component.extend({ @computed('placeholderKey') @@ -17,7 +18,7 @@ export default Ember.Component.extend({ var self = this; var selectedBadges; - var template = this.container.lookup('template:badge-selector-autocomplete.raw'); + var template = getOwner(this).lookup('template:badge-selector-autocomplete.raw'); self.$('input').autocomplete({ allowAny: false, items: _.isArray(this.get('badgeNames')) ? this.get('badgeNames') : [this.get('badgeNames')], diff --git a/app/assets/javascripts/discourse/components/category-selector.js.es6 b/app/assets/javascripts/discourse/components/category-selector.js.es6 index 10a7c454ebd..460a1dfdd65 100644 --- a/app/assets/javascripts/discourse/components/category-selector.js.es6 +++ b/app/assets/javascripts/discourse/components/category-selector.js.es6 @@ -1,6 +1,7 @@ import { categoryBadgeHTML } from 'discourse/helpers/category-link'; import Category from 'discourse/models/category'; import { on, observes } from 'ember-addons/ember-computed-decorators'; +import { getOwner } from 'discourse-common/lib/get-owner'; export default Ember.Component.extend({ @observes('categories') @@ -12,7 +13,7 @@ export default Ember.Component.extend({ @on('didInsertElement') _initializeAutocomplete(opts) { const self = this, - template = this.container.lookup('template:category-selector-autocomplete.raw'), + template = getOwner(this).lookup('template:category-selector-autocomplete.raw'), regexp = new RegExp(`href=['\"]${Discourse.getURL('/c/')}([^'\"]+)`); this.$('input').autocomplete({ diff --git a/app/assets/javascripts/discourse/components/composer-editor.js.es6 b/app/assets/javascripts/discourse/components/composer-editor.js.es6 index 6503e051c29..4ba0c200574 100644 --- a/app/assets/javascripts/discourse/components/composer-editor.js.es6 +++ b/app/assets/javascripts/discourse/components/composer-editor.js.es6 @@ -6,7 +6,7 @@ import { linkSeenTagHashtags, fetchUnseenTagHashtags } from 'discourse/lib/link- import { load } from 'pretty-text/oneboxer'; import { ajax } from 'discourse/lib/ajax'; import InputValidation from 'discourse/models/input-validation'; - +import { getOwner } from 'discourse-common/lib/get-owner'; import { tinyAvatar, displayErrorForUpload, getUploadMarkdown, @@ -62,7 +62,7 @@ export default Ember.Component.extend({ @on('didInsertElement') _composerEditorInit() { const topicId = this.get('topic.id'); - const template = this.container.lookup('template:user-selector-autocomplete.raw'); + const template = getOwner(this).lookup('template:user-selector-autocomplete.raw'); const $input = this.$('.d-editor-input'); $input.autocomplete({ template, diff --git a/app/assets/javascripts/discourse/components/composer-message.js.es6 b/app/assets/javascripts/discourse/components/composer-message.js.es6 index 39307dfcccd..715afce98b3 100644 --- a/app/assets/javascripts/discourse/components/composer-message.js.es6 +++ b/app/assets/javascripts/discourse/components/composer-message.js.es6 @@ -1,11 +1,12 @@ import computed from 'ember-addons/ember-computed-decorators'; +import { getOwner } from 'discourse-common/lib/get-owner'; export default Ember.Component.extend({ classNameBindings: [':composer-popup', ':hidden', 'message.extraClass'], @computed('message.templateName') defaultLayout(templateName) { - return this.container.lookup(`template:composer/${templateName}`); + return getOwner(this).lookup(`template:composer/${templateName}`); }, didInsertElement() { diff --git a/app/assets/javascripts/discourse/components/d-editor.js.es6 b/app/assets/javascripts/discourse/components/d-editor.js.es6 index e6dd2804dde..11feda8e3f9 100644 --- a/app/assets/javascripts/discourse/components/d-editor.js.es6 +++ b/app/assets/javascripts/discourse/components/d-editor.js.es6 @@ -10,6 +10,8 @@ import { cook } from 'discourse/lib/text'; import { translations } from 'pretty-text/emoji/data'; import { emojiSearch } from 'pretty-text/emoji'; import { emojiUrlFor } from 'discourse/lib/text'; +import { getRegister } from 'discourse-common/lib/get-owner'; +import deprecated from 'discourse-common/lib/deprecated'; // Our head can be a static string or a function that returns a string // based on input (like for numbered lists). @@ -182,7 +184,7 @@ export function addToolbarCallback(func) { } export function onToolbarCreate(func) { - console.warn('`onToolbarCreate` is deprecated, use the plugin api instead.'); + deprecated('`onToolbarCreate` is deprecated, use the plugin api instead.'); addToolbarCallback(func); }; @@ -205,15 +207,18 @@ export default Ember.Component.extend({ this.set('ready', true); }, + init() { + this._super(); + this.register = getRegister(this); + }, + didInsertElement() { this._super(); - const container = this.get('container'), - $editorInput = this.$('.d-editor-input'); - - this._applyEmojiAutocomplete(container, $editorInput); - this._applyCategoryHashtagAutocomplete(container, $editorInput); + const $editorInput = this.$('.d-editor-input'); + this._applyEmojiAutocomplete($editorInput); + this._applyCategoryHashtagAutocomplete($editorInput); Ember.run.scheduleOnce('afterRender', this, this._readyNow); @@ -287,8 +292,8 @@ export default Ember.Component.extend({ } }, - _applyCategoryHashtagAutocomplete(container) { - const template = container.lookup('template:category-tag-autocomplete.raw'); + _applyCategoryHashtagAutocomplete() { + const template = this.register.lookup('template:category-tag-autocomplete.raw'); const siteSettings = this.siteSettings; this.$('.d-editor-input').autocomplete({ @@ -310,10 +315,11 @@ export default Ember.Component.extend({ }); }, - _applyEmojiAutocomplete(container, $editorInput) { + _applyEmojiAutocomplete($editorInput) { if (!this.siteSettings.enable_emoji) { return; } - const template = container.lookup('template:emoji-selector-autocomplete.raw'); + const register = this.register; + const template = this.register.lookup('template:emoji-selector-autocomplete.raw'); const self = this; $editorInput.autocomplete({ @@ -329,7 +335,7 @@ export default Ember.Component.extend({ } else { showSelector({ appendTo: self.$(), - container, + register, onSelect: title => { // Remove the previously type characters when a new emoji is selected from the selector. let selected = self._getSelected(); @@ -614,7 +620,7 @@ export default Ember.Component.extend({ emoji() { showSelector({ appendTo: this.$(), - container: this.container, + register: this.register, onSelect: title => this._addText(this._getSelected(), `:${title}:`) }); } diff --git a/app/assets/javascripts/discourse/components/group-selector.js.es6 b/app/assets/javascripts/discourse/components/group-selector.js.es6 index a2080ecda23..4f4fa0f6a5e 100644 --- a/app/assets/javascripts/discourse/components/group-selector.js.es6 +++ b/app/assets/javascripts/discourse/components/group-selector.js.es6 @@ -1,4 +1,5 @@ import { on, observes, default as computed } from 'ember-addons/ember-computed-decorators'; +import { getOwner } from 'discourse-common/lib/get-owner'; export default Ember.Component.extend({ @computed('placeholderKey') @@ -18,7 +19,7 @@ export default Ember.Component.extend({ var selectedGroups; var groupNames = this.get('groupNames'); - var template = this.container.lookup('template:group-selector-autocomplete.raw'); + var template = getOwner(this).lookup('template:group-selector-autocomplete.raw'); self.$('input').autocomplete({ allowAny: false, items: _.isArray(groupNames) ? groupNames : (Ember.isEmpty(groupNames)) ? [] : [groupNames], diff --git a/app/assets/javascripts/discourse/components/header-extra-info.js.es6 b/app/assets/javascripts/discourse/components/header-extra-info.js.es6 index 66aaf82cb91..1d769faa090 100644 --- a/app/assets/javascripts/discourse/components/header-extra-info.js.es6 +++ b/app/assets/javascripts/discourse/components/header-extra-info.js.es6 @@ -1,3 +1,5 @@ +import deprecated from 'discourse-common/lib/deprecated'; + export function needsSecondRowIf() { - Ember.warn("DEPRECATION: `needsSecondRowIf` is deprecated. Use widget hooks on `header-second-row`"); + deprecated("`needsSecondRowIf` is deprecated. Use widget hooks on `header-second-row`"); } diff --git a/app/assets/javascripts/discourse/components/mount-widget.js.es6 b/app/assets/javascripts/discourse/components/mount-widget.js.es6 index e62390e6940..3a4fb8bce5a 100644 --- a/app/assets/javascripts/discourse/components/mount-widget.js.es6 +++ b/app/assets/javascripts/discourse/components/mount-widget.js.es6 @@ -2,6 +2,7 @@ import { keyDirty } from 'discourse/widgets/widget'; import { diff, patch } from 'virtual-dom'; import { WidgetClickHook } from 'discourse/widgets/hooks'; import { renderedKey, queryRegistry } from 'discourse/widgets/widget'; +import { getRegister } from 'discourse-common/lib/get-owner'; const _cleanCallbacks = {}; export function addWidgetCleanCallback(widgetName, fn) { @@ -24,12 +25,15 @@ export default Ember.Component.extend({ (this.get('delegated') || []).forEach(m => this.set(m, m)); - this._widgetClass = queryRegistry(name) || this.container.lookupFactory(`widget:${name}`); + this.register = getRegister(this); + + this._widgetClass = queryRegistry(name) || this.register.lookupFactory(`widget:${name}`); if (!this._widgetClass) { console.error(`Error: Could not find widget: ${name}`); } + this._childEvents = []; this._connected = []; this._dispatched = []; @@ -97,13 +101,14 @@ export default Ember.Component.extend({ rerenderWidget() { Ember.run.cancel(this._timeout); + if (this._rootNode) { if (!this._widgetClass) { return; } const t0 = new Date().getTime(); const args = this.get('args') || this.buildArgs(); const opts = { model: this.get('model') }; - const newTree = new this._widgetClass(args, this.container, opts); + const newTree = new this._widgetClass(args, this.register, opts); newTree._emberView = this; const patches = diff(this._tree || this._rootNode, newTree); diff --git a/app/assets/javascripts/discourse/components/nav-item.js.es6 b/app/assets/javascripts/discourse/components/nav-item.js.es6 index 69c797bd309..3a4b40dddf1 100644 --- a/app/assets/javascripts/discourse/components/nav-item.js.es6 +++ b/app/assets/javascripts/discourse/components/nav-item.js.es6 @@ -1,6 +1,7 @@ /* You might be looking for navigation-item. */ import computed from "ember-addons/ember-computed-decorators"; +import { getOwner } from 'discourse-common/lib/get-owner'; export default Ember.Component.extend({ tagName: 'li', @@ -8,7 +9,7 @@ export default Ember.Component.extend({ @computed() router() { - return this.container.lookup('router:main'); + return getOwner(this).lookup('router:main'); }, @computed("path") diff --git a/app/assets/javascripts/discourse/components/topic-list-item.js.es6 b/app/assets/javascripts/discourse/components/topic-list-item.js.es6 index 1fc7cf53a6e..d9b8569bb87 100644 --- a/app/assets/javascripts/discourse/components/topic-list-item.js.es6 +++ b/app/assets/javascripts/discourse/components/topic-list-item.js.es6 @@ -1,5 +1,6 @@ import computed from 'ember-addons/ember-computed-decorators'; import { bufferedRender } from 'discourse-common/lib/buffered-render'; +import { getOwner } from 'discourse-common/lib/get-owner'; export function showEntrance(e) { let target = $(e.target); @@ -11,7 +12,7 @@ export function showEntrance(e) { target = target.end(); } } - this.container.lookup('controller:application').send("showTopicEntrance", {topic: this.get('topic'), position: target.offset()}); + getOwner(this).lookup('controller:application').send("showTopicEntrance", {topic: this.get('topic'), position: target.offset()}); return false; } } @@ -30,7 +31,7 @@ export default Ember.Component.extend(bufferedRender({ }, buildBuffer(buffer) { - const template = Discourse.__container__.lookup('template:list/topic-list-item.raw'); + const template = getOwner(this).lookup('template:list/topic-list-item.raw'); if (template) { buffer.push(template(this)); } diff --git a/app/assets/javascripts/discourse/components/user-selector.js.es6 b/app/assets/javascripts/discourse/components/user-selector.js.es6 index e124b43fb2f..94d6db9bfe4 100644 --- a/app/assets/javascripts/discourse/components/user-selector.js.es6 +++ b/app/assets/javascripts/discourse/components/user-selector.js.es6 @@ -1,6 +1,7 @@ import { observes } from 'ember-addons/ember-computed-decorators'; import TextField from 'discourse/components/text-field'; import userSearch from 'discourse/lib/user-search'; +import { getOwner } from 'discourse-common/lib/get-owner'; export default TextField.extend({ @observes('usernames') @@ -30,7 +31,7 @@ export default TextField.extend({ } this.$().val(this.get('usernames')).autocomplete({ - template: this.container.lookup('template:user-selector-autocomplete.raw'), + template: getOwner(this).lookup('template:user-selector-autocomplete.raw'), disabled: this.get('disabled'), single: this.get('single'), allowAny: this.get('allowAny'), diff --git a/app/assets/javascripts/discourse/controllers/composer.js.es6 b/app/assets/javascripts/discourse/controllers/composer.js.es6 index 30a5c7af93a..f5b506a9025 100644 --- a/app/assets/javascripts/discourse/controllers/composer.js.es6 +++ b/app/assets/javascripts/discourse/controllers/composer.js.es6 @@ -6,6 +6,7 @@ import { default as computed, observes } from 'ember-addons/ember-computed-decor import { relativeAge } from 'discourse/lib/formatter'; import { escapeExpression } from 'discourse/lib/utilities'; import InputValidation from 'discourse/models/input-validation'; +import { getOwner } from 'discourse-common/lib/get-owner'; function loadDraft(store, opts) { opts = opts || {}; @@ -85,8 +86,8 @@ export default Ember.Controller.extend({ }, showToolbar: Em.computed({ - get(){ - const keyValueStore = this.container.lookup('key-value-store:main'); + get() { + const keyValueStore = getOwner(this).lookup('key-value-store:main'); const storedVal = keyValueStore.get("toolbar-enabled"); if (this._toolbarEnabled === undefined && storedVal === undefined) { // iPhone 6 is 375, anything narrower and toolbar should @@ -97,7 +98,7 @@ export default Ember.Controller.extend({ return this._toolbarEnabled || storedVal === "true"; }, set(key, val){ - const keyValueStore = this.container.lookup('key-value-store:main'); + const keyValueStore = getOwner(this).lookup('key-value-store:main'); this._toolbarEnabled = val; keyValueStore.set({key: "toolbar-enabled", value: val ? "true" : "false"}); return val; diff --git a/app/assets/javascripts/discourse/controllers/header.js.es6 b/app/assets/javascripts/discourse/controllers/header.js.es6 index 2665f2afbd1..f5979bb36a7 100644 --- a/app/assets/javascripts/discourse/controllers/header.js.es6 +++ b/app/assets/javascripts/discourse/controllers/header.js.es6 @@ -1,6 +1,7 @@ import { addFlagProperty as realAddFlagProperty } from 'discourse/components/site-header'; +import deprecated from 'discourse-common/lib/deprecated'; export function addFlagProperty(prop) { - Ember.warn("importing `addFlagProperty` is deprecated. Use the PluginAPI instead"); + deprecated("importing `addFlagProperty` is deprecated. Use the PluginAPI instead"); realAddFlagProperty(prop); } diff --git a/app/assets/javascripts/discourse/lib/emoji/toolbar.js.es6 b/app/assets/javascripts/discourse/lib/emoji/toolbar.js.es6 index 68e95281849..d3b6e12f125 100644 --- a/app/assets/javascripts/discourse/lib/emoji/toolbar.js.es6 +++ b/app/assets/javascripts/discourse/lib/emoji/toolbar.js.es6 @@ -151,7 +151,7 @@ function render(page, offset, options) { }; $('.emoji-modal', options.appendTo).remove(); - const template = options.container.lookup('template:emoji-toolbar.raw'); + const template = options.register.lookup('template:emoji-toolbar.raw'); options.appendTo.append(template(model)); bindEvents(page, offset, options); diff --git a/app/assets/javascripts/discourse/lib/mobile.js.es6 b/app/assets/javascripts/discourse/lib/mobile.js.es6 index 940e18c44bc..2ceb13ba51b 100644 --- a/app/assets/javascripts/discourse/lib/mobile.js.es6 +++ b/app/assets/javascripts/discourse/lib/mobile.js.es6 @@ -1,3 +1,5 @@ +import deprecated from 'discourse-common/lib/deprecated'; + let mobileForced = false; // An object that is responsible for logic related to mobile devices. @@ -67,10 +69,9 @@ export function resetMobile() { mobileForced = false; } -// Backwards compatibiltity, deprecated Object.defineProperty(Discourse, 'Mobile', { - get: function() { - Ember.warn("DEPRECATION: `Discourse.Mobile` is deprecated, use `this.site.mobileView` instead"); + get() { + deprecated("`Discourse.Mobile` is deprecated, use `this.site.mobileView` instead"); return Mobile; } }); diff --git a/app/assets/javascripts/discourse/lib/page-tracker.js.es6 b/app/assets/javascripts/discourse/lib/page-tracker.js.es6 index 89deaafe03d..ddef4792680 100644 --- a/app/assets/javascripts/discourse/lib/page-tracker.js.es6 +++ b/app/assets/javascripts/discourse/lib/page-tracker.js.es6 @@ -1,3 +1,5 @@ +import deprecated from 'discourse-common/lib/deprecated'; + const PageTracker = Ember.Object.extend(Ember.Evented); let _pageTracker = PageTracker.create(); @@ -42,7 +44,7 @@ export function onPageChange(fn) { // backwards compatibility const BackwardsCompat = { current() { - console.warn(`Using PageTracker.current() is deprecated. Your plugin should use the PluginAPI`); + deprecated(`Using PageTracker.current() is deprecated. Your plugin should use the PluginAPI`); return _pageTracker; } }; diff --git a/app/assets/javascripts/discourse/lib/url.js.es6 b/app/assets/javascripts/discourse/lib/url.js.es6 index 7621ae4ce98..6aca579c130 100644 --- a/app/assets/javascripts/discourse/lib/url.js.es6 +++ b/app/assets/javascripts/discourse/lib/url.js.es6 @@ -332,7 +332,8 @@ const DiscourseURL = Ember.Object.extend({ const transition = router.handleURL(path); transition._discourse_intercepted = true; - transition.promise.then(() => jumpToElement(elementId)); + const promise = transition.promise || transition; + promise.then(() => jumpToElement(elementId)); } }).create(); diff --git a/app/assets/javascripts/discourse/models/store.js.es6 b/app/assets/javascripts/discourse/models/store.js.es6 index 87a589a5a33..b2f0225409c 100644 --- a/app/assets/javascripts/discourse/models/store.js.es6 +++ b/app/assets/javascripts/discourse/models/store.js.es6 @@ -1,6 +1,7 @@ import { ajax } from 'discourse/lib/ajax'; import RestModel from 'discourse/models/rest'; import ResultSet from 'discourse/models/result-set'; +import { getRegister } from 'discourse-common/lib/get-owner'; let _identityMap; @@ -41,6 +42,11 @@ export default Ember.Object.extend({ _plurals: {'post-reply': 'post-replies', 'post-reply-history': 'post_reply_histories'}, + init() { + this._super(); + this.register = getRegister(this); + }, + pluralize(thing) { return this._plurals[thing] || thing + "s"; }, @@ -196,11 +202,11 @@ export default Ember.Object.extend({ obj.__state = obj.id ? "created" : "new"; // TODO: Have injections be automatic - obj.topicTrackingState = this.container.lookup('topic-tracking-state:main'); - obj.keyValueStore = this.container.lookup('key-value-store:main'); - obj.siteSettings = this.container.lookup('site-settings:main'); + obj.topicTrackingState = this.register.lookup('topic-tracking-state:main'); + obj.keyValueStore = this.register.lookup('key-value-store:main'); + obj.siteSettings = this.register.lookup('site-settings:main'); - const klass = this.container.lookupFactory('model:' + type) || RestModel; + const klass = this.register.lookupFactory('model:' + type) || RestModel; const model = klass.create(obj); storeMap(type, obj.id, model); @@ -208,7 +214,7 @@ export default Ember.Object.extend({ }, adapterFor(type) { - return this.container.lookup('adapter:' + type) || this.container.lookup('adapter:rest'); + return this.register.lookup('adapter:' + type) || this.register.lookup('adapter:rest'); }, _lookupSubType(subType, type, id, root) { @@ -287,7 +293,7 @@ export default Ember.Object.extend({ if (existing) { delete obj.id; - const klass = this.container.lookupFactory('model:' + type) || RestModel; + const klass = this.register.lookupFactory('model:' + type) || RestModel; existing.setProperties(klass.munge(obj)); obj.id = id; return existing; diff --git a/app/assets/javascripts/discourse/pre-initializers/map-routes.js.es6 b/app/assets/javascripts/discourse/pre-initializers/map-routes.js.es6 index 94629c17baf..0a593c11bd9 100644 --- a/app/assets/javascripts/discourse/pre-initializers/map-routes.js.es6 +++ b/app/assets/javascripts/discourse/pre-initializers/map-routes.js.es6 @@ -10,7 +10,10 @@ export default { // HACK to fix: https://github.com/emberjs/ember.js/issues/10310 const originalBuildInstance = originalBuildInstance || Ember.Application.prototype.buildInstance; Ember.Application.prototype.buildInstance = function() { - this.registry = this.buildRegistry(); + const registry = this.buildRegistry(); + if (Ember.VERSION[0] === "1") { + this.registry = registry; + } return originalBuildInstance.apply(this); }; } diff --git a/app/assets/javascripts/discourse/routes/application.js.es6 b/app/assets/javascripts/discourse/routes/application.js.es6 index d506852e361..fab8595cd3d 100644 --- a/app/assets/javascripts/discourse/routes/application.js.es6 +++ b/app/assets/javascripts/discourse/routes/application.js.es6 @@ -6,6 +6,7 @@ import OpenComposer from "discourse/mixins/open-composer"; import Category from 'discourse/models/category'; import mobile from 'discourse/lib/mobile'; import { findAll } from 'discourse/models/login-method'; +import { getOwner } from 'discourse-common/lib/get-owner'; function unlessReadOnly(method, message) { return function() { @@ -40,7 +41,7 @@ const ApplicationRoute = Discourse.Route.extend(OpenComposer, { // Ember doesn't provider a router `willTransition` event so let's make one willTransition() { - var router = this.container.lookup('router:main'); + var router = getOwner(this).lookup('router:main'); Ember.run.once(router, router.trigger, 'willTransition'); return this._super(); }, @@ -150,7 +151,7 @@ const ApplicationRoute = Discourse.Route.extend(OpenComposer, { changeBulkTemplate(w) { const controllerName = w.replace('modal/', ''), - factory = this.container.lookupFactory('controller:' + controllerName); + factory = getOwner(this).lookupFactory('controller:' + controllerName); this.render(w, {into: 'modal/topic-bulk-actions', outlet: 'bulkOutlet', controller: factory ? controllerName : 'topic-bulk-actions'}); }, @@ -200,7 +201,7 @@ const ApplicationRoute = Discourse.Route.extend(OpenComposer, { _autoLogin(modal, modalClass, notAuto) { const methods = findAll(this.siteSettings, - this.container.lookup('capabilities:main'), + getOwner(this).lookup('capabilities:main'), this.site.isMobileDevice); if (!this.siteSettings.enable_local_logins && methods.length === 1) { diff --git a/app/assets/javascripts/discourse/routes/full-page-search.js.es6 b/app/assets/javascripts/discourse/routes/full-page-search.js.es6 index 805f1e1b918..eb0cc01092c 100644 --- a/app/assets/javascripts/discourse/routes/full-page-search.js.es6 +++ b/app/assets/javascripts/discourse/routes/full-page-search.js.es6 @@ -3,6 +3,7 @@ import { translateResults, getSearchKey, isValidSearchTerm } from "discourse/lib import Composer from 'discourse/models/composer'; import PreloadStore from 'preload-store'; import { getTransient, setTransient } from 'discourse/lib/page-tracker'; +import { getOwner } from 'discourse-common/lib/get-owner'; export default Discourse.Route.extend({ queryParams: { q: {}, expanded: false, context_id: {}, context: {}, skip_context: {} }, @@ -52,7 +53,7 @@ export default Discourse.Route.extend({ category = match[1]; } } - this.container.lookup('controller:composer').open({action: Composer.CREATE_TOPIC, draftKey: Composer.CREATE_TOPIC, topicCategory: category}); + getOwner(this).lookup('controller:composer').open({action: Composer.CREATE_TOPIC, draftKey: Composer.CREATE_TOPIC, topicCategory: category}); } } diff --git a/app/assets/javascripts/discourse/routes/topic-from-params.js.es6 b/app/assets/javascripts/discourse/routes/topic-from-params.js.es6 index 7c96c1a2643..2212da06b94 100644 --- a/app/assets/javascripts/discourse/routes/topic-from-params.js.es6 +++ b/app/assets/javascripts/discourse/routes/topic-from-params.js.es6 @@ -56,8 +56,10 @@ export default Discourse.Route.extend({ ignoreIfChanged: true }); } - }).catch(function(e) { - Ember.warn('Could not view topic', e); + }).catch(e => { + if (!Ember.testing) { + console.log('Could not view topic', e); + } }); } diff --git a/app/assets/javascripts/discourse/templates/composer.hbs b/app/assets/javascripts/discourse/templates/composer.hbs index 2551444d598..2c47fc8fcd2 100644 --- a/app/assets/javascripts/discourse/templates/composer.hbs +++ b/app/assets/javascripts/discourse/templates/composer.hbs @@ -17,7 +17,7 @@
{{#if site.mobileView}} - + {{/if}} diff --git a/app/assets/javascripts/discourse/widgets/connector.js.es6 b/app/assets/javascripts/discourse/widgets/connector.js.es6 index c132666a412..d197914af2d 100644 --- a/app/assets/javascripts/discourse/widgets/connector.js.es6 +++ b/app/assets/javascripts/discourse/widgets/connector.js.es6 @@ -21,7 +21,7 @@ export default class Connector { } const view = Ember.View.create({ - container: widget.container, + container: widget.register, templateName: opts.templateName, context }); diff --git a/app/assets/javascripts/discourse/widgets/decorator-helper.js.es6 b/app/assets/javascripts/discourse/widgets/decorator-helper.js.es6 index a949d0a6281..2ee8df8723d 100644 --- a/app/assets/javascripts/discourse/widgets/decorator-helper.js.es6 +++ b/app/assets/javascripts/discourse/widgets/decorator-helper.js.es6 @@ -8,7 +8,8 @@ class DecoratorHelper { this.widget = widget; this.attrs = attrs; this.state = state; - this.container = widget.container; + this.register = widget.register; + this.register.deprecateContainer(this); } /** diff --git a/app/assets/javascripts/discourse/widgets/hamburger-menu.js.es6 b/app/assets/javascripts/discourse/widgets/hamburger-menu.js.es6 index f0e5319eb61..5fa48a84137 100644 --- a/app/assets/javascripts/discourse/widgets/hamburger-menu.js.es6 +++ b/app/assets/javascripts/discourse/widgets/hamburger-menu.js.es6 @@ -63,7 +63,7 @@ export default createWidget('hamburger-menu', { }, lookupCount(type) { - const tts = this.container.lookup('topic-tracking-state:main'); + const tts = this.register.lookup('topic-tracking-state:main'); return tts ? tts.lookupCount(type) : 0; }, diff --git a/app/assets/javascripts/discourse/widgets/header.js.es6 b/app/assets/javascripts/discourse/widgets/header.js.es6 index 80dc5b0f92e..5f1f1e21337 100644 --- a/app/assets/javascripts/discourse/widgets/header.js.es6 +++ b/app/assets/javascripts/discourse/widgets/header.js.es6 @@ -190,7 +190,7 @@ export default createWidget('header', { updateHighlight() { if (!this.state.searchVisible) { - const service = this.container.lookup('search-service:main'); + const service = this.register.lookup('search-service:main'); service.set('highlightTerm', ''); } }, @@ -208,7 +208,7 @@ export default createWidget('header', { toggleSearchMenu() { if (this.site.mobileView) { - const searchService = this.container.lookup('search-service:main'); + const searchService = this.register.lookup('search-service:main'); const context = searchService.get('searchContext'); var params = ""; @@ -240,7 +240,7 @@ export default createWidget('header', { state.contextEnabled = false; - const currentPath = this.container.lookup('controller:application').get('currentPath'); + const currentPath = this.register.lookup('controller:application').get('currentPath'); const blacklist = [ /^discovery\.categories/ ]; const whitelist = [ /^topic\./ ]; const check = function(regex) { return !!currentPath.match(regex); }; @@ -249,7 +249,7 @@ export default createWidget('header', { // If we're viewing a topic, only intercept search if there are cloaked posts if (showSearch && currentPath.match(/^topic\./)) { showSearch = ($('.topic-post .cooked, .small-action:not(.time-gap)').length < - this.container.lookup('controller:topic').get('model.postStream.stream.length')); + this.register.lookup('controller:topic').get('model.postStream.stream.length')); } if (state.searchVisible) { diff --git a/app/assets/javascripts/discourse/widgets/link.js.es6 b/app/assets/javascripts/discourse/widgets/link.js.es6 index 7eabce3975f..9ba519fff4d 100644 --- a/app/assets/javascripts/discourse/widgets/link.js.es6 +++ b/app/assets/javascripts/discourse/widgets/link.js.es6 @@ -10,7 +10,7 @@ export default createWidget('link', { href(attrs) { const route = attrs.route; if (route) { - const router = this.container.lookup('router:main'); + const router = this.register.lookup('router:main'); if (router && router.router) { const params = [route]; if (attrs.model) { diff --git a/app/assets/javascripts/discourse/widgets/search-menu-controls.js.es6 b/app/assets/javascripts/discourse/widgets/search-menu-controls.js.es6 index 5be3ce35430..70d046f7d59 100644 --- a/app/assets/javascripts/discourse/widgets/search-menu-controls.js.es6 +++ b/app/assets/javascripts/discourse/widgets/search-menu-controls.js.es6 @@ -30,7 +30,7 @@ createWidget('search-context', { tagName: 'div.search-context', html(attrs) { - const service = this.container.lookup('search-service:main'); + const service = this.register.lookup('search-service:main'); const ctx = service.get('searchContext'); const result = []; diff --git a/app/assets/javascripts/discourse/widgets/search-menu.js.es6 b/app/assets/javascripts/discourse/widgets/search-menu.js.es6 index 9ed3b8c7d4f..f8d626f4568 100644 --- a/app/assets/javascripts/discourse/widgets/search-menu.js.es6 +++ b/app/assets/javascripts/discourse/widgets/search-menu.js.es6 @@ -134,7 +134,7 @@ export default createWidget('search-menu', { searchService() { if (!this._searchService) { - this._searchService = this.container.lookup('search-service:main'); + this._searchService = this.register.lookup('search-service:main'); } return this._searchService; }, diff --git a/app/assets/javascripts/discourse/widgets/widget.js.es6 b/app/assets/javascripts/discourse/widgets/widget.js.es6 index 851ad09bd90..76f8c60fba5 100644 --- a/app/assets/javascripts/discourse/widgets/widget.js.es6 +++ b/app/assets/javascripts/discourse/widgets/widget.js.es6 @@ -127,12 +127,14 @@ export function createWidget(name, opts) { } export default class Widget { - constructor(attrs, container, opts) { + constructor(attrs, register, opts) { opts = opts || {}; this.attrs = attrs || {}; this.mergeState = opts.state; - this.container = container; this.model = opts.model; + this.register = register; + + register.deprecateContainer(this); this.key = this.buildKey ? this.buildKey(attrs) : null; @@ -146,13 +148,13 @@ export default class Widget { } } - this.site = container.lookup('site:main'); - this.siteSettings = container.lookup('site-settings:main'); - this.currentUser = container.lookup('current-user:main'); - this.capabilities = container.lookup('capabilities:main'); - this.store = container.lookup('store:main'); - this.appEvents = container.lookup('app-events:main'); - this.keyValueStore = container.lookup('key-value-store:main'); + this.site = register.lookup('site:main'); + this.siteSettings = register.lookup('site-settings:main'); + this.currentUser = register.lookup('current-user:main'); + this.capabilities = register.lookup('capabilities:main'); + this.store = register.lookup('store:main'); + this.appEvents = register.lookup('app-events:main'); + this.keyValueStore = register.lookup('key-value-store:main'); if (this.name) { const custom = _customSettings[this.name]; @@ -223,15 +225,15 @@ export default class Widget { let WidgetClass = _registry[widgetName]; if (!WidgetClass) { - if (!this.container) { - console.error("couldn't find container"); + if (!this.register) { + console.error("couldn't find register"); return; } - WidgetClass = this.container.lookupFactory(`widget:${widgetName}`); + WidgetClass = this.register.lookupFactory(`widget:${widgetName}`); } if (WidgetClass) { - const result = new WidgetClass(attrs, this.container, opts); + const result = new WidgetClass(attrs, this.register, opts); result.parentWidget = this; return result; } else { diff --git a/app/assets/javascripts/wizard/components/wizard-field-image.js.es6 b/app/assets/javascripts/wizard/components/wizard-field-image.js.es6 index bb5f796ced8..370cf12c752 100644 --- a/app/assets/javascripts/wizard/components/wizard-field-image.js.es6 +++ b/app/assets/javascripts/wizard/components/wizard-field-image.js.es6 @@ -1,6 +1,7 @@ import getUrl from 'discourse-common/lib/get-url'; import computed from 'ember-addons/ember-computed-decorators'; import { getToken } from 'wizard/lib/ajax'; +import { getOwner } from 'discourse-common/lib/get-owner'; export default Ember.Component.extend({ classNames: ['wizard-image-row'], @@ -9,7 +10,7 @@ export default Ember.Component.extend({ @computed('field.id') previewComponent(id) { const componentName = `image-preview-${Ember.String.dasherize(id)}`; - const exists = this.container.lookup(`component:${componentName}`); + const exists = getOwner(this).lookup(`component:${componentName}`); return exists ? componentName : 'wizard-image-preview'; },