Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement knownForType for RFC#58. #95

Merged
merged 2 commits into from
Jun 12, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dist/ember-resolver-spade.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/ember-resolver-tests.js

Large diffs are not rendered by default.

59 changes: 51 additions & 8 deletions dist/ember-resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ define("ember/resolver",
}

function makeDictionary() {
var cache = Object.create(null);
var cache = Ember.create(null);
cache['_dict'] = null;
delete cache['_dict'];
return cache;
Expand Down Expand Up @@ -96,7 +96,7 @@ define("ember/resolver",

function resolveOther(parsedName) {
/*jshint validthis:true */

// Temporarily disabling podModulePrefix deprecation
/*
if (!this._deprecatedPodModulePrefix) {
Expand All @@ -115,23 +115,22 @@ define("ember/resolver",
var normalizedModuleName = this.findModuleName(parsedName);

if (normalizedModuleName) {
var module = require(normalizedModuleName, null, null, true /* force sync */);
var defaultExport = this._extractDefaultExport(normalizedModuleName, parsedName);

if (module && module['default']) { module = module['default']; }

if (module === undefined) {
if (defaultExport === undefined) {
throw new Error(" Expected to find: '" + parsedName.fullName + "' within '" + normalizedModuleName + "' but got 'undefined'. Did you forget to `export default` within '" + normalizedModuleName + "'?");
}

if (this.shouldWrapInClassFactory(module, parsedName)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should module be renamed to defaultExport here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only if we would prefer to avoid an error 😜

module = classFactory(module);
defaultExport = classFactory(defaultExport);
}

return module;
return defaultExport;
} else {
return this._super(parsedName);
}
}

// Ember.DefaultResolver docs:
// https://github.com/emberjs/ember.js/blob/master/packages/ember-application/lib/system/resolver.js
var Resolver = Ember.DefaultResolver.extend({
Expand Down Expand Up @@ -337,6 +336,50 @@ define("ember/resolver",
}

Ember.Logger.info(symbol, parsedName.fullName, padding, description);
},

knownForType: function(type) {
var moduleEntries = requirejs.entries;
var moduleKeys = Ember.keys(moduleEntries);

var items = makeDictionary();
for (var index = 0, length = moduleKeys.length; index < length; index++) {
var moduleName = moduleKeys[index];
var fullname = this.translateToContainerFullname(type, moduleName);

if (fullname) {
items[fullname] = true;
}
}

return items;
},

translateToContainerFullname: function(type, moduleName) {
var prefix = this.prefix({ type: type });
var pluralizedType = this.pluralize(type);
var nonPodRegExp = new RegExp('^' + prefix + '/' + pluralizedType + '/(.+)$');
var podRegExp = new RegExp('^' + prefix + '/(.+)/' + type + '$');
var matches;


if ((matches = moduleName.match(podRegExp))) {
return type + ':' + matches[1];
}

if ((matches = moduleName.match(nonPodRegExp))) {
return type + ':' + matches[1];
}
},

_extractDefaultExport: function(normalizedModuleName) {
var module = require(normalizedModuleName, null, null, true /* force sync */);

if (module && module['default']) {
module = module['default'];
}

return module;
}
});

Expand Down
2 changes: 1 addition & 1 deletion dist/ember-resolver.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 51 additions & 8 deletions dist/ember-resolver.prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ define("ember/resolver",
}

function makeDictionary() {
var cache = Object.create(null);
var cache = Ember.create(null);
cache['_dict'] = null;
delete cache['_dict'];
return cache;
Expand Down Expand Up @@ -96,7 +96,7 @@ define("ember/resolver",

function resolveOther(parsedName) {
/*jshint validthis:true */

// Temporarily disabling podModulePrefix deprecation
/*
if (!this._deprecatedPodModulePrefix) {
Expand All @@ -115,23 +115,22 @@ define("ember/resolver",
var normalizedModuleName = this.findModuleName(parsedName);

if (normalizedModuleName) {
var module = require(normalizedModuleName, null, null, true /* force sync */);
var defaultExport = this._extractDefaultExport(normalizedModuleName, parsedName);

if (module && module['default']) { module = module['default']; }

if (module === undefined) {
if (defaultExport === undefined) {
throw new Error(" Expected to find: '" + parsedName.fullName + "' within '" + normalizedModuleName + "' but got 'undefined'. Did you forget to `export default` within '" + normalizedModuleName + "'?");
}

if (this.shouldWrapInClassFactory(module, parsedName)) {
module = classFactory(module);
defaultExport = classFactory(defaultExport);
}

return module;
return defaultExport;
} else {
return this._super(parsedName);
}
}

// Ember.DefaultResolver docs:
// https://github.com/emberjs/ember.js/blob/master/packages/ember-application/lib/system/resolver.js
var Resolver = Ember.DefaultResolver.extend({
Expand Down Expand Up @@ -337,6 +336,50 @@ define("ember/resolver",
}

Ember.Logger.info(symbol, parsedName.fullName, padding, description);
},

knownForType: function(type) {
var moduleEntries = requirejs.entries;
var moduleKeys = Ember.keys(moduleEntries);

var items = makeDictionary();
for (var index = 0, length = moduleKeys.length; index < length; index++) {
var moduleName = moduleKeys[index];
var fullname = this.translateToContainerFullname(type, moduleName);

if (fullname) {
items[fullname] = true;
}
}

return items;
},

translateToContainerFullname: function(type, moduleName) {
var prefix = this.prefix({ type: type });
var pluralizedType = this.pluralize(type);
var nonPodRegExp = new RegExp('^' + prefix + '/' + pluralizedType + '/(.+)$');
var podRegExp = new RegExp('^' + prefix + '/(.+)/' + type + '$');
var matches;


if ((matches = moduleName.match(podRegExp))) {
return type + ':' + matches[1];
}

if ((matches = moduleName.match(nonPodRegExp))) {
return type + ':' + matches[1];
}
},

_extractDefaultExport: function(normalizedModuleName) {
var module = require(normalizedModuleName, null, null, true /* force sync */);

if (module && module['default']) {
module = module['default'];
}

return module;
}
});

Expand Down
44 changes: 44 additions & 0 deletions dist/modules/ember-resolver-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,50 @@ test("can lookup templates via Ember.TEMPLATES", function() {
ok(template, 'template should resolve');
});

test('it provides eachForType which invokes the callback for each item found', function() {

function orange() { }
define('appkit/fruits/orange', [], function() {
return { default: orange };
});

function apple() { }
define('appkit/fruits/apple', [], function() {
return {default: apple };
});

function other() {}
define('appkit/stuffs/other', [], function() {
return { default: other };
});

var items = resolver.knownForType('fruit');

deepEqual(items, {
'fruit:orange': true,
'fruit:apple': true
});
});

test('eachForType can find both pod and non-pod factories', function() {
function orange() { }
define('appkit/fruits/orange', [], function() {
return { default: orange };
});

function lemon() { }
define('appkit/lemon/fruit', [], function() {
return { default: lemon };
});

var items = resolver.knownForType('fruit');

deepEqual(items, {
'fruit:orange': true,
'fruit:lemon': true
});
});

module("Logging", {
setup: function() {
originalLog = Ember.Logger.info;
Expand Down
Loading