Skip to content

Commit

Permalink
Add support to glimmer-wrapper for MU namespaces
Browse files Browse the repository at this point in the history
resolver.resolve can accept a namespace denoted by a double colon in the first argument, for example: 
 - `service:other-namespace::i18n`
 - `template:components/other-namespace::my-component`

For example for:

```
{{ember-power-select::option}}
```

The lookup should be:

```
resolver.resolve('template:component/ember-power-select::option’)
``

And for a service example:

```
Ember.service.inject('auth-addon::main-service')
```

The lookup would be:

```
resolver.resolve('service:auth-addon::main-service’)
```

Refs: #214
Refs: emberjs/ember.js#15350 (comment)
  • Loading branch information
iezer committed Dec 14, 2017
1 parent cd6366e commit 6fbfa33
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 24 deletions.
38 changes: 16 additions & 22 deletions mu-trees/addon/resolvers/glimmer-wrapper/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ function slasherize(dotted) {

const TEMPLATE_TO_PARTIAL = /^template:(.*\/)?_([\w-]+)/;

const LOOKUP_REGEX = /^([^:]+):(components\/)?([^:]+::)?([^:]+)$/;

/*
* Wrap the @glimmer/resolver in Ember's resolver API. Although
* this code extends from the DefaultResolver, it should never
Expand All @@ -30,21 +32,18 @@ const Resolver = DefaultResolver.extend({

normalize: null,

resolve(lookupString, referrer, rawString) {
/*
* Ember namespaces are part of the raw invocation passed as a third
* argument, for example other-addon::some-service
*/
let rootName = this._configRootName;
let rawStringName = null;
if (rawString) {
let [namespace, name] = rawString.split('::');
rootName = namespace;
rawStringName = name;
}
resolve(lookupString, referrer) {
let type, name, rootName, fullMatch, componentPrefix;

let [type, lookupStringName] = lookupString.split(':');
let name = lookupStringName;
let lookupMatch = LOOKUP_REGEX.exec(lookupString);
if (lookupMatch) {
[ fullMatch, type, componentPrefix, rootName, name ] = lookupMatch;
rootName = (rootName && rootName.replace(/::/, '')) || this._configRootName;

if (componentPrefix) {
name = `${componentPrefix}${name}`;
}
}

/*
* Ember components require their lookupString to be massaged. Make this
Expand All @@ -55,16 +54,12 @@ const Resolver = DefaultResolver.extend({
let parts = referrer.split(':src/ui/');
referrer = `${parts[0]}:/${rootName}/${parts[1]}`;
referrer = referrer.split('/template.hbs')[0];
} else if (rawString) {
} else if (rootName !== this._configRootName) {
// This is only required because:
// https://github.com/glimmerjs/glimmer-di/issues/45
referrer = `${type}:/${rootName}/`;
}

/* If there is no name, fallback to the name passed in the rawString */
if (!name) {
name = rawStringName;
}
if (name) {
if (type === 'service') {
/* Services may be camelCased */
Expand All @@ -76,9 +71,8 @@ const Resolver = DefaultResolver.extend({
/* Controllers may have.dot.paths */
lookupString = `controller:${slasherize(name)}`;
} else if (type === 'template') {
if (lookupStringName && lookupStringName.indexOf('components/') === 0) {
let sliced = lookupStringName.slice(11);
lookupString = `template:${sliced.length ? sliced : rawStringName}`;
if (name.indexOf('components/') === 0) {
lookupString = `template:${name.slice(11)}`;
} else {
/*
* Ember partials are looked up as templates. Here we replace the template
Expand Down
4 changes: 2 additions & 2 deletions mu-trees/tests/unit/resolvers/glimmer-wrapper/basic-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,7 @@ test('Can resolve a namespaced service lookup', function(assert) {
});

assert.equal(
resolver.resolve('service', null, 'other-namespace::i18n'),
resolver.resolve('service:other-namespace::i18n'),
service,
'namespaced resolution resolved'
);
Expand All @@ -771,7 +771,7 @@ test('Can resolve a namespaced component template', function(assert) {
});

assert.equal(
resolver.resolve('template:components/', null, 'other-namespace::my-component'),
resolver.resolve('template:components/other-namespace::my-component'),
template,
'namespaced resolution resolved'
);
Expand Down

0 comments on commit 6fbfa33

Please sign in to comment.