Permalink
Browse files

prettify the JavaScript a bit

Remove some CoffeeScript-isms like "return " before the last expression
of every function.
  • Loading branch information...
James A. Rosen
James A. Rosen committed Jan 13, 2013
1 parent 718f27c commit 45e4aebdeb6f6dbe10bca6f6f2bc9ba5c6813e4a
Showing with 129 additions and 66 deletions.
  1. +23 −7 lib/i18n.js
  2. +106 −59 spec/javascripts/i18nSpec.js
View
@@ -1,4 +1,4 @@
-(function() {
+(function(window) {
var I18n, assert, findTemplate, get, isBinding, isTranslatedAttribute, lookupKey, pluralForm;
isTranslatedAttribute = /(.+)Translation$/;
@@ -15,35 +15,41 @@
var firstKey, idx, remainingKeys, result;
result = hash[key];
idx = key.indexOf('.');
+
if (!result && idx !== -1) {
firstKey = key.substr(0, idx);
remainingKeys = key.substr(idx + 1);
hash = hash[firstKey];
if (hash) result = lookupKey(remainingKeys, hash);
}
+
return result;
};
- assert = Ember.assert != null ? Ember.assert : ember_assert;
+ assert = Ember.assert != null ? Ember.assert : window.ember_assert;
findTemplate = function(key, setOnMissing) {
- var result;
assert("You must provide a translation key string, not %@".fmt(key), typeof key === 'string');
- result = lookupKey(key, I18n.translations);
+ var result = lookupKey(key, I18n.translations);
+
if (setOnMissing) {
if (result == null) {
result = I18n.translations[key] = I18n.compile("Missing translation: " + key);
}
}
+
if ((result != null) && !jQuery.isFunction(result)) {
result = I18n.translations[key] = I18n.compile(result);
}
+
return result;
};
I18n = {
compile: Handlebars.compile,
+
translations: {},
+
template: function(key, count) {
var interpolatedKey, result, suffix;
if ((count != null) && (pluralForm != null)) {
@@ -53,16 +59,19 @@
}
return result != null ? result : result = findTemplate(key, true);
},
+
t: function(key, context) {
var template;
if (context == null) context = {};
template = I18n.template(key, context.count);
return template(context);
},
+
TranslateableAttributes: Em.Mixin.create({
didInsertElement: function() {
var attribute, isTranslatedAttributeMatch, key, path, result, translatedValue;
result = this._super.apply(this, arguments);
+
for (key in this) {
path = this[key];
isTranslatedAttributeMatch = key.match(isTranslatedAttribute);
@@ -72,13 +81,12 @@
this.$().attr(attribute, translatedValue);
}
}
+
return result;
}
})
};
- Em.I18n = I18n;
-
Ember.I18n = I18n;
isBinding = /(.+)Binding$/;
@@ -92,9 +100,11 @@
tagName = attrs.tagName || 'span';
delete attrs.tagName;
elementID = "i18n-" + (jQuery.uuid++);
+
Em.keys(attrs).forEach(function(property) {
var bindPath, currentValue, invoker, isBindingMatch, normalized, normalizedPath, observer, propertyName, root, _ref;
isBindingMatch = property.match(isBinding);
+
if (isBindingMatch) {
propertyName = isBindingMatch[1];
bindPath = attrs[property];
@@ -103,6 +113,7 @@
invoker = null;
normalized = Ember.Handlebars.normalizePath(context, bindPath, data);
_ref = [normalized.root, normalized.path], root = _ref[0], normalizedPath = _ref[1];
+
observer = function() {
var elem, newValue;
if (view.get('state') !== 'inDOM') {
@@ -114,12 +125,15 @@
attrs[propertyName] = newValue;
return elem.html(I18n.t(key, attrs));
};
+
invoker = function() {
return Em.run.once(observer);
};
+
return Em.addObserver(root, normalizedPath, invoker);
}
});
+
result = '<%@ id="%@">%@</%@>'.fmt(tagName, elementID, I18n.t(key, attrs), tagName);
return new Handlebars.SafeString(result);
});
@@ -128,12 +142,14 @@
var attrs, result;
attrs = options.hash;
result = [];
+
Em.keys(attrs).forEach(function(property) {
var translatedValue;
translatedValue = I18n.t(attrs[property]);
return result.push('%@="%@"'.fmt(property, translatedValue));
});
+
return new Handlebars.SafeString(result.join(' '));
});
-}).call(this);
+}).call(undefined, this);
Oops, something went wrong.

0 comments on commit 45e4aeb

Please sign in to comment.