Skip to content

Commit

Permalink
Merge pull request #220 from 201-created/isaac/namespace-argument
Browse files Browse the repository at this point in the history
Glimmer Resolver gets target namespace as third argument
  • Loading branch information
mixonic committed Jan 25, 2018
2 parents 1228d7d + b57afa8 commit 7f3fbcf
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 28 deletions.
4 changes: 2 additions & 2 deletions mu-trees/addon/resolvers/fallback/index.js
Expand Up @@ -9,8 +9,8 @@ export default Resolver.extend({
namespace: { modulePrefix: this.config.app.name }
}, options));
},
resolve(name, referrer, rawString) {
let result = this._super(name, referrer, rawString);
resolve(name, referrer, targetNamespace) {
let result = this._super(name, referrer, targetNamespace);
return result || this._fallback.resolve(name);
}
});
31 changes: 8 additions & 23 deletions mu-trees/addon/resolvers/glimmer-wrapper/index.js
Expand Up @@ -30,21 +30,10 @@ 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, targetNamespace) {
let rootName = targetNamespace ||this._configRootName;

let [type, lookupStringName] = lookupString.split(':');
let name = lookupStringName;
let [type, name] = lookupString.split(':');

/*
* Ember components require their lookupString to be massaged. Make this
Expand All @@ -55,18 +44,14 @@ 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 (targetNamespace) {
// 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 === 'component' && rawString && name) {
if (type === 'component' && name) {
lookupString = `${type}:${name}`;
} else if (type === 'service') {
/* Services may be camelCased */
Expand All @@ -78,9 +63,9 @@ 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 && name.indexOf('components/') === 0) {
let sliced = name.slice(11);
lookupString = `template:${sliced}`;
} else {
/*
* Ember partials are looked up as templates. Here we replace the template
Expand Down
85 changes: 82 additions & 3 deletions mu-trees/tests/unit/resolvers/glimmer-wrapper/basic-test.js
Expand Up @@ -744,7 +744,7 @@ test('Can resolve a namespaced service lookup', function(assert) {
});

assert.equal(
resolver.resolve('service', null, 'other-namespace::i18n'),
resolver.resolve('service:i18n', null, 'other-namespace'),
service,
'namespaced resolution resolved'
);
Expand All @@ -770,7 +770,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/my-component', null, 'other-namespace'),
template,
'namespaced resolution resolved'
);
Expand All @@ -796,7 +796,86 @@ test('Can resolve a namespaced component object', function(assert) {
});

assert.equal(
resolver.resolve('component', null, 'other-namespace::my-component'),
resolver.resolve('component:my-component', null, 'other-namespace'),
component,
'namespaced resolution resolved'
);
});

// Main addon component and service

test('Can resolve a namespaced main service lookup', function(assert) {
let service = {};
let resolver = this.resolverForEntries({
app: {
name: 'example-app'
},
types: {
service: { definitiveCollection: 'services' }
},
collections: {
services: {
types: [ 'service' ]
}
}
}, {
'service:/other-namespace/services/main': service
});

assert.equal(
resolver.resolve('service:other-namespace', null),
service,
'namespaced resolution resolved'
);
});

test('Can resolve a namespaced main component template', function(assert) {
let template = {};
let resolver = this.resolverForEntries({
app: {
name: 'example-app'
},
types: {
template: { definitiveCollection: 'components' }
},
collections: {
components: {
group: 'ui',
types: [ 'template' ]
}
}
}, {
'template:/other-namespace/components/main': template
});

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

test('Can resolve a namespaced component object', function(assert) {
let component = {};
let resolver = this.resolverForEntries({
app: {
name: 'example-app'
},
types: {
component: { definitiveCollection: 'components' }
},
collections: {
components: {
group: 'ui',
types: [ 'component' ]
}
}
}, {
'component:/other-namespace/components/main': component
});

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

0 comments on commit 7f3fbcf

Please sign in to comment.