ES6: Uses proper `normalize` method, includes support for namespaced
controllers.
This commit is contained in:
parent
eee5f85654
commit
5191acfd9f
|
@ -6,7 +6,7 @@
|
|||
@namespace Discourse
|
||||
@module Discourse
|
||||
**/
|
||||
Discourse.BadgesShowController = Discourse.ObjectController.extend({
|
||||
export default Discourse.ObjectController.extend({
|
||||
grantDates: Em.computed.mapBy('userBadges', 'grantedAt'),
|
||||
minGrantedAt: Em.computed.min('grantDates'),
|
||||
|
|
@ -7,31 +7,50 @@
|
|||
@namespace 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({
|
||||
|
||||
/**
|
||||
For our ES6 modules, Discourse standardizes on dashed, lower case names.
|
||||
This method converts camelCase to that format.
|
||||
parseName: parseName,
|
||||
|
||||
@method normalizeName
|
||||
@param {String} name to convert
|
||||
@returns {String} the converted name
|
||||
|
||||
**/
|
||||
normalizeName: function(name) {
|
||||
return name.replace(/([a-z])([A-Z])/g, '$1-$2').replace('_', '-').toLowerCase();
|
||||
normalize: function(fullName) {
|
||||
var split = fullName.split(':');
|
||||
if (split.length > 1) {
|
||||
var dashed = Ember.String.dasherize(split[1].replace(/\./g, '/')),
|
||||
moduleName = 'discourse/' + split[0] + 's/' + dashed;
|
||||
if (requirejs.entries[moduleName]) {
|
||||
return split[0] + ":" + dashed;
|
||||
}
|
||||
}
|
||||
return this._super(fullName);
|
||||
},
|
||||
|
||||
resolveController: function(parsedName) {
|
||||
var normalized = this.normalizeName(parsedName.fullNameWithoutType),
|
||||
moduleName = "discourse/controllers/" + normalized,
|
||||
var moduleName = 'discourse/controllers/' + parsedName.fullNameWithoutType,
|
||||
module = requirejs.entries[moduleName];
|
||||
|
||||
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 */);
|
||||
if (module && module['default']) { module = module['default']; }
|
||||
}
|
||||
|
|
|
@ -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", {
|
||||
setup: function() {
|
||||
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() {
|
||||
setTemplates([
|
||||
"foobar",
|
||||
|
|
Loading…
Reference in New Issue