From b98324a937f372cfef2295401d180f33e8e6eeb3 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Thu, 1 May 2014 12:45:52 -0400 Subject: [PATCH] ES6: Uses proper `normalize` method, includes support for namespaced controllers. --- .../show.js.es6} | 2 +- .../javascripts/discourse/ember/resolver.js | 51 +++++++++++++------ test/javascripts/ember/resolver_test.js | 11 ---- 3 files changed, 36 insertions(+), 28 deletions(-) rename app/assets/javascripts/discourse/controllers/{badges_show_controller.js => badges/show.js.es6} (89%) diff --git a/app/assets/javascripts/discourse/controllers/badges_show_controller.js b/app/assets/javascripts/discourse/controllers/badges/show.js.es6 similarity index 89% rename from app/assets/javascripts/discourse/controllers/badges_show_controller.js rename to app/assets/javascripts/discourse/controllers/badges/show.js.es6 index 98c2e18fb3e..9ccd077be0d 100644 --- a/app/assets/javascripts/discourse/controllers/badges_show_controller.js +++ b/app/assets/javascripts/discourse/controllers/badges/show.js.es6 @@ -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'), diff --git a/app/assets/javascripts/discourse/ember/resolver.js b/app/assets/javascripts/discourse/ember/resolver.js index 0f6f594595e..0ae8bfd322f 100644 --- a/app/assets/javascripts/discourse/ember/resolver.js +++ b/app/assets/javascripts/discourse/ember/resolver.js @@ -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']; } } diff --git a/test/javascripts/ember/resolver_test.js b/test/javascripts/ember/resolver_test.js index a0ebd40a8fe..9044182231d 100644 --- a/test/javascripts/ember/resolver_test.js +++ b/test/javascripts/ember/resolver_test.js @@ -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",