Merge pull request #1513 from velesin/computed_self_refactoring
refactors Discourse.Computed to bind context to self
This commit is contained in:
commit
b490fd2f88
|
@ -40,9 +40,9 @@ Discourse.computed = {
|
||||||
var args = Array.prototype.slice.call(arguments, 0);
|
var args = Array.prototype.slice.call(arguments, 0);
|
||||||
var format = args.pop();
|
var format = args.pop();
|
||||||
var computed = Ember.computed(function() {
|
var computed = Ember.computed(function() {
|
||||||
var context = this;
|
var self = this;
|
||||||
return I18n.t(format.fmt.apply(format, args.map(function (a) {
|
return I18n.t(format.fmt.apply(format, args.map(function (a) {
|
||||||
return context.get(a);
|
return self.get(a);
|
||||||
})));
|
})));
|
||||||
});
|
});
|
||||||
return computed.property.apply(computed, args);
|
return computed.property.apply(computed, args);
|
||||||
|
@ -61,9 +61,9 @@ Discourse.computed = {
|
||||||
var args = Array.prototype.slice.call(arguments, 0);
|
var args = Array.prototype.slice.call(arguments, 0);
|
||||||
var format = args.pop();
|
var format = args.pop();
|
||||||
var computed = Ember.computed(function() {
|
var computed = Ember.computed(function() {
|
||||||
var context = this;
|
var self = this;
|
||||||
return format.fmt.apply(format, args.map(function (a) {
|
return format.fmt.apply(format, args.map(function (a) {
|
||||||
return context.get(a);
|
return self.get(a);
|
||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
return computed.property.apply(computed, args);
|
return computed.property.apply(computed, args);
|
||||||
|
@ -82,9 +82,9 @@ Discourse.computed = {
|
||||||
var args = Array.prototype.slice.call(arguments, 0);
|
var args = Array.prototype.slice.call(arguments, 0);
|
||||||
var format = args.pop();
|
var format = args.pop();
|
||||||
var computed = Ember.computed(function() {
|
var computed = Ember.computed(function() {
|
||||||
var context = this;
|
var self = this;
|
||||||
return Discourse.getURL(format.fmt.apply(format, args.map(function (a) {
|
return Discourse.getURL(format.fmt.apply(format, args.map(function (a) {
|
||||||
return context.get(a);
|
return self.get(a);
|
||||||
})));
|
})));
|
||||||
});
|
});
|
||||||
return computed.property.apply(computed, args);
|
return computed.property.apply(computed, args);
|
||||||
|
|
|
@ -1,10 +1,22 @@
|
||||||
module("Discourse.Computed");
|
module("Discourse.Computed", {
|
||||||
|
setup: function() {
|
||||||
|
sinon.stub(I18n, "t", function(scope) {
|
||||||
|
return "%@ translated: " + scope;
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
teardown: function() {
|
||||||
|
I18n.t.restore();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
var testClass = Em.Object.extend({
|
var testClass = Em.Object.extend({
|
||||||
same: Discourse.computed.propertyEqual('cookies', 'biscuits'),
|
same: Discourse.computed.propertyEqual('cookies', 'biscuits'),
|
||||||
diff: Discourse.computed.propertyNotEqual('cookies', 'biscuits'),
|
diff: Discourse.computed.propertyNotEqual('cookies', 'biscuits'),
|
||||||
exclaimyUsername: Discourse.computed.fmt('username', "!!! %@ !!!"),
|
exclaimyUsername: Discourse.computed.fmt('username', "!!! %@ !!!"),
|
||||||
multiple: Discourse.computed.fmt('username', 'mood', "%@ is %@"),
|
multiple: Discourse.computed.fmt('username', 'mood', "%@ is %@"),
|
||||||
|
translatedExclaimyUsername: Discourse.computed.i18n('username', "!!! %@ !!!"),
|
||||||
|
translatedMultiple: Discourse.computed.i18n('username', 'mood', "%@ is %@"),
|
||||||
userUrl: Discourse.computed.url('username', "/users/%@")
|
userUrl: Discourse.computed.url('username', "/users/%@")
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -38,12 +50,28 @@ test("fmt", function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
equal(t.get('exclaimyUsername'), '!!! eviltrout !!!', "it inserts the string");
|
equal(t.get('exclaimyUsername'), '!!! eviltrout !!!', "it inserts the string");
|
||||||
equal(t.get('multiple'), "eviltrout is happy");
|
equal(t.get('multiple'), "eviltrout is happy", "it inserts multiple strings");
|
||||||
|
|
||||||
t.set('username', 'codinghorror');
|
t.set('username', 'codinghorror');
|
||||||
equal(t.get('multiple'), "codinghorror is happy", "supports changing proerties");
|
equal(t.get('multiple'), "codinghorror is happy", "it supports changing properties");
|
||||||
t.set('mood', 'ecstatic');
|
t.set('mood', 'ecstatic');
|
||||||
equal(t.get('multiple'), "codinghorror is ecstatic", "supports changing another property");
|
equal(t.get('multiple'), "codinghorror is ecstatic", "it supports changing another property");
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
test("i18n", function() {
|
||||||
|
var t = testClass.create({
|
||||||
|
username: 'eviltrout',
|
||||||
|
mood: "happy"
|
||||||
|
});
|
||||||
|
|
||||||
|
equal(t.get('translatedExclaimyUsername'), '%@ translated: !!! eviltrout !!!', "it inserts the string and then translates");
|
||||||
|
equal(t.get('translatedMultiple'), "%@ translated: eviltrout is happy", "it inserts multiple strings and then translates");
|
||||||
|
|
||||||
|
t.set('username', 'codinghorror');
|
||||||
|
equal(t.get('translatedMultiple'), "%@ translated: codinghorror is happy", "it supports changing properties");
|
||||||
|
t.set('mood', 'ecstatic');
|
||||||
|
equal(t.get('translatedMultiple'), "%@ translated: codinghorror is ecstatic", "it supports changing another property");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
@ -56,5 +84,4 @@ test("url with a prefix", function() {
|
||||||
Discourse.BaseUri = "/prefixed/";
|
Discourse.BaseUri = "/prefixed/";
|
||||||
var t = testClass.create({ username: 'eviltrout' });
|
var t = testClass.create({ username: 'eviltrout' });
|
||||||
equal(t.get('userUrl'), "/prefixed/users/eviltrout");
|
equal(t.get('userUrl'), "/prefixed/users/eviltrout");
|
||||||
|
});
|
||||||
});
|
|
||||||
|
|
Loading…
Reference in New Issue