mirror of
https://github.com/discourse/discourse.git
synced 2024-12-02 13:39:36 -06:00
ES6: Uses proper normalize
method, includes support for namespaced
controllers.
This commit is contained in:
parent
44b2f82be8
commit
b98324a937
@ -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'),
|
||||||
|
|
@ -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']; }
|
||||||
}
|
}
|
||||||
|
@ -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",
|
||||||
|
Loading…
Reference in New Issue
Block a user