ES6: Uses proper normalize method, includes support for namespaced

controllers.
This commit is contained in:
Robin Ward 2014-05-01 12:45:52 -04:00
parent 44b2f82be8
commit b98324a937
3 changed files with 36 additions and 28 deletions

View File

@ -6,7 +6,7 @@
@namespace Discourse @namespace Discourse
@module Discourse @module Discourse
**/ **/
Discourse.BadgesShowController = Discourse.ObjectController.extend({ export default Discourse.ObjectController.extend({
grantDates: Em.computed.mapBy('userBadges', 'grantedAt'), grantDates: Em.computed.mapBy('userBadges', 'grantedAt'),
minGrantedAt: Em.computed.min('grantDates'), minGrantedAt: Em.computed.min('grantDates'),

View File

@ -7,31 +7,50 @@
@namespace Discourse @namespace Discourse
@module Discourse @module Discourse
**/ **/
var classify = Ember.String.classify;
var get = Ember.get;
function parseName(fullName) {
/*jshint validthis:true */
var nameParts = fullName.split(":"),
type = nameParts[0], fullNameWithoutType = nameParts[1],
name = fullNameWithoutType,
namespace = get(this, 'namespace'),
root = namespace;
return {
fullName: fullName,
type: type,
fullNameWithoutType: fullNameWithoutType,
name: name,
root: root,
resolveMethodName: "resolve" + classify(type)
};
}
Discourse.Resolver = Ember.DefaultResolver.extend({ Discourse.Resolver = Ember.DefaultResolver.extend({
/** parseName: parseName,
For our ES6 modules, Discourse standardizes on dashed, lower case names.
This method converts camelCase to that format.
@method normalizeName normalize: function(fullName) {
@param {String} name to convert var split = fullName.split(':');
@returns {String} the converted name if (split.length > 1) {
var dashed = Ember.String.dasherize(split[1].replace(/\./g, '/')),
**/ moduleName = 'discourse/' + split[0] + 's/' + dashed;
normalizeName: function(name) { if (requirejs.entries[moduleName]) {
return name.replace(/([a-z])([A-Z])/g, '$1-$2').replace('_', '-').toLowerCase(); return split[0] + ":" + dashed;
}
}
return this._super(fullName);
}, },
resolveController: function(parsedName) { resolveController: function(parsedName) {
var normalized = this.normalizeName(parsedName.fullNameWithoutType), var moduleName = 'discourse/controllers/' + parsedName.fullNameWithoutType,
moduleName = "discourse/controllers/" + normalized,
module = requirejs.entries[moduleName]; module = requirejs.entries[moduleName];
if (module) { if (module) {
if (normalized !== parsedName.fullNameWithoutType) {
Em.Logger.error(parsedName.fullNameWithoutType + " was used to look up an ES6 module. You should use " + normalized+ " instead.");
}
module = require(moduleName, null, null, true /* force sync */); module = require(moduleName, null, null, true /* force sync */);
if (module && module['default']) { module = module['default']; } if (module && module['default']) { module = module['default']; }
} }

View File

@ -14,11 +14,6 @@ function setTemplates(lookupStrings) {
}); });
} }
function normalized(input, expected, desc) {
var resolver = Discourse.Resolver.create({namespace: Discourse});
equal(resolver.normalizeName(input), expected, desc);
}
module("Discourse.Resolver", { module("Discourse.Resolver", {
setup: function() { setup: function() {
originalTemplates = Ember.TEMPLATES; originalTemplates = Ember.TEMPLATES;
@ -34,12 +29,6 @@ module("Discourse.Resolver", {
} }
}); });
test("normalizeName", function() {
normalized('header', 'header', 'a single word stays the same');
normalized('avatarSelector', 'avatar-selector', 'camel case is converted to dashed');
normalized('avatar_selector', 'avatar-selector', 'underscores are converted to dashes');
});
test("finds templates in top level dir", function() { test("finds templates in top level dir", function() {
setTemplates([ setTemplates([
"foobar", "foobar",