ES6: AvatarSelector and HeaderController - also includes support for controllers with

camelcase via an error message.
This commit is contained in:
Robin Ward 2014-04-29 21:16:57 -04:00
parent 0327f469c1
commit 44b2f82be8
7 changed files with 40 additions and 12 deletions

View File

@ -7,7 +7,7 @@
@uses Discourse.ModalFunctionality @uses Discourse.ModalFunctionality
@module Discourse @module Discourse
**/ **/
Discourse.AvatarSelectorController = Discourse.Controller.extend(Discourse.ModalFunctionality, { export default Discourse.Controller.extend(Discourse.ModalFunctionality, {
actions: { actions: {
useUploadedAvatar: function() { this.set("use_uploaded_avatar", true); }, useUploadedAvatar: function() { this.set("use_uploaded_avatar", true); },

View File

@ -6,7 +6,7 @@
@namespace Discourse @namespace Discourse
@module Discourse @module Discourse
**/ **/
Discourse.HeaderController = Discourse.Controller.extend({ export default Discourse.Controller.extend({
topic: null, topic: null,
showExtraInfo: null, showExtraInfo: null,
notifications: null, notifications: null,

View File

@ -9,11 +9,29 @@
**/ **/
Discourse.Resolver = Ember.DefaultResolver.extend({ Discourse.Resolver = Ember.DefaultResolver.extend({
/**
For our ES6 modules, Discourse standardizes on dashed, lower case names.
This method converts camelCase to that format.
@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();
},
resolveController: function(parsedName) { resolveController: function(parsedName) {
var moduleName = "discourse/controllers/" + parsedName.fullNameWithoutType, var normalized = this.normalizeName(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

@ -18,18 +18,17 @@ Discourse.PreferencesRoute = Discourse.RestrictedUserRoute.extend({
actions: { actions: {
showAvatarSelector: function() { showAvatarSelector: function() {
Discourse.Route.showModal(this, 'avatarSelector'); Discourse.Route.showModal(this, 'avatar-selector');
// all the properties needed for displaying the avatar selector modal // all the properties needed for displaying the avatar selector modal
var avatarSelector = this.modelFor('user').getProperties( this.controllerFor('avatar-selector').setProperties(this.modelFor('user').getProperties(
'username', 'email', 'username', 'email',
'has_uploaded_avatar', 'use_uploaded_avatar', 'has_uploaded_avatar', 'use_uploaded_avatar',
'gravatar_template', 'uploaded_avatar_template'); 'gravatar_template', 'uploaded_avatar_template'));
this.controllerFor('avatarSelector').setProperties(avatarSelector);
}, },
saveAvatarSelection: function() { saveAvatarSelection: function() {
var user = this.modelFor('user'); var user = this.modelFor('user');
var avatarSelector = this.controllerFor('avatarSelector'); var avatarSelector = this.controllerFor('avatar-selector');
// sends the information to the server if it has changed // sends the information to the server if it has changed
if (avatarSelector.get('use_uploaded_avatar') !== user.get('use_uploaded_avatar')) { if (avatarSelector.get('use_uploaded_avatar') !== user.get('use_uploaded_avatar')) {
user.toggleAvatarSelection(avatarSelector.get('use_uploaded_avatar')); user.toggleAvatarSelection(avatarSelector.get('use_uploaded_avatar'));

View File

@ -4,10 +4,10 @@ var avatarSelector = Em.Object.create({
uploaded_avatar_template: "//cdn.discourse.org/uploads/meta_discourse/avatars/093/607/185cff113e/{size}.jpg" uploaded_avatar_template: "//cdn.discourse.org/uploads/meta_discourse/avatars/093/607/185cff113e/{size}.jpg"
}); });
module("Discourse.AvatarSelectorController"); module("controller:avatar-selector");
test("avatarTemplate", function() { test("avatarTemplate", function() {
var avatarSelectorController = testController(Discourse.AvatarSelectorController); var avatarSelectorController = controllerFor('avatar-selector');
avatarSelectorController.setProperties(avatarSelector); avatarSelectorController.setProperties(avatarSelector);
equal(avatarSelectorController.get("avatarTemplate"), equal(avatarSelectorController.get("avatarTemplate"),

View File

@ -1,4 +1,4 @@
module("Discourse.HeaderController"); module("controller:header", "Header Controller");
test("showNotifications action", function() { test("showNotifications action", function() {
var resolveRequestWith; var resolveRequestWith;
@ -7,7 +7,7 @@ test("showNotifications action", function() {
}); });
var controller = Discourse.HeaderController.create(); var controller = controllerFor('header');
var viewSpy = { var viewSpy = {
showDropdownBySelector: sinon.spy() showDropdownBySelector: sinon.spy()
}; };

View File

@ -14,6 +14,11 @@ 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;
@ -29,6 +34,12 @@ 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",