Don't try injecting into the Resolver. Use options instead.

This commit is contained in:
Robin Ward 2016-11-03 14:15:26 -04:00
parent c8282e4ca1
commit e18ae34493
3 changed files with 14 additions and 7 deletions

View File

@ -9,6 +9,13 @@ var LOADING_WHITELIST = ['badges', 'userActivity', 'userPrivateMessages', 'admin
var _dummyRoute; var _dummyRoute;
var _loadingView; var _loadingView;
const _options = {};
export function setResolverOption(name, value) {
_options[name] = value;
}
function loadingResolver(cb) { function loadingResolver(cb) {
return function(parsedName) { return function(parsedName) {
var fullNameWithoutType = parsedName.fullNameWithoutType; var fullNameWithoutType = parsedName.fullNameWithoutType;
@ -158,14 +165,14 @@ export function buildResolver(baseName) {
}, },
findPluginMobileTemplate(parsedName) { findPluginMobileTemplate(parsedName) {
if (this.mobileView) { if (_options.mobileView) {
var pluginParsedName = this.parseName(parsedName.fullName.replace("template:", "template:javascripts/mobile/")); var pluginParsedName = this.parseName(parsedName.fullName.replace("template:", "template:javascripts/mobile/"));
return this.findTemplate(pluginParsedName); return this.findTemplate(pluginParsedName);
} }
}, },
findMobileTemplate(parsedName) { findMobileTemplate(parsedName) {
if (this.mobileView) { if (_options.mobileView) {
var mobileParsedName = this.parseName(parsedName.fullName.replace("template:", "template:mobile/")); var mobileParsedName = this.parseName(parsedName.fullName.replace("template:", "template:mobile/"));
return this.findTemplate(mobileParsedName); return this.findTemplate(mobileParsedName);
} }

View File

@ -1,18 +1,18 @@
import Mobile from 'discourse/lib/mobile'; import Mobile from 'discourse/lib/mobile';
import { setResolverOption } from 'discourse-common/resolver';
// Initializes the `Mobile` helper object. // Initializes the `Mobile` helper object.
export default { export default {
name: 'mobile', name: 'mobile',
after: 'inject-objects', after: 'inject-objects',
initialize(container, app) { initialize(container) {
Mobile.init(); Mobile.init();
const site = container.lookup('site:main'); const site = container.lookup('site:main');
site.set('mobileView', Mobile.mobileView); site.set('mobileView', Mobile.mobileView);
site.set('isMobileDevice', Mobile.isMobileDevice); site.set('isMobileDevice', Mobile.isMobileDevice);
// This is a bit weird but you can't seem to inject into the resolver? setResolverOption('mobileView', Mobile.mobileView);
app.registry.resolver.__resolver__.mobileView = Mobile.mobileView;
} }
}; };

View File

@ -1,4 +1,4 @@
import { buildResolver } from 'discourse-common/resolver'; import { setResolverOption, buildResolver } from 'discourse-common/resolver';
let originalTemplates; let originalTemplates;
let resolver; let resolver;
@ -92,7 +92,7 @@ test("resolves mobile templates to 'mobile/' namespace", function() {
"baz" "baz"
]); ]);
resolver.mobileView = true; setResolverOption('mobileView', true);
lookupTemplate("template:foo", "mobile/foo", "finding mobile version even if normal one is not present"); lookupTemplate("template:foo", "mobile/foo", "finding mobile version even if normal one is not present");
lookupTemplate("template:bar", "mobile/bar", "preferring mobile version when both mobile and normal versions are present"); lookupTemplate("template:bar", "mobile/bar", "preferring mobile version when both mobile and normal versions are present");