Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert #3218 and deprecate accessing global paths from Handlebars #4358

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions FEATURES.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@ for a detailed explanation.

Added in [#3655](https://github.com/emberjs/ember.js/pull/3655).

* `ember-handlebars-caps-lookup`
Forces Handlebars values starting with capital letters, like `{{CONSTANT}}`,
to always be looked up on `Ember.lookup`. Previously, these values would be
looked up on the controller in certain cases.

Added in [#3218](https://github.com/emberjs/ember.js/pull/3218)

* `ember-testing-simple-setup`
Removes the need for most of the ceremony of setting up an application for testing. The following
examples are equivalent:
Expand Down
1 change: 0 additions & 1 deletion features.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"query-params-new": null,
"string-parameterize": null,
"ember-routing-named-substates": null,
"ember-handlebars-caps-lookup": null,
"ember-handlebars-log-primitives": true,
"ember-testing-simple-setup": null,
"ember-testing-routing-helpers": true,
Expand Down
30 changes: 9 additions & 21 deletions packages/ember-handlebars/lib/ext.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,29 +61,17 @@ var handlebarsGet = Ember.Handlebars.get = function(root, path, options) {
normalizedPath = normalizePath(root, path, data),
value;

if (Ember.FEATURES.isEnabled("ember-handlebars-caps-lookup")) {
// In cases where the path begins with a keyword, change the
// root to the value represented by that keyword, and ensure
// the path is relative to it.
root = normalizedPath.root;
path = normalizedPath.path;

// If the path starts with a capital letter, look it up on Ember.lookup,
// which defaults to the `window` object in browsers.
if (Ember.isGlobalPath(path)) {
value = Ember.get(Ember.lookup, path);
} else {

// In cases where the path begins with a keyword, change the
// root to the value represented by that keyword, and ensure
// the path is relative to it.
value = Ember.get(normalizedPath.root, normalizedPath.path);
}
value = Ember.get(root, path);

} else {
root = normalizedPath.root;
path = normalizedPath.path;

value = Ember.get(root, path);

if (value === undefined && root !== Ember.lookup && Ember.isGlobalPath(path)) {
value = Ember.get(Ember.lookup, path);
}
if (value === undefined && root !== Ember.lookup && Ember.isGlobalPath(path)) {
Ember.deprecate("Global lookup from a Handlebars template is deprecated.");
value = Ember.get(Ember.lookup, path);
}

return value;
Expand Down
2 changes: 0 additions & 2 deletions packages/ember-handlebars/lib/helpers/each.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,8 +401,6 @@ Ember.Handlebars.registerHelper('each', function eachHelper(path, options) {
}

options.hash.dataSourceBinding = path;
// Set up emptyView as a metamorph with no tag
//options.hash.emptyViewClass = Ember._MetamorphView;

if (options.data.insideGroup && !options.hash.groupedRows && !options.hash.itemViewClass) {
new Ember.Handlebars.GroupedEach(this, path, options).render();
Expand Down
32 changes: 32 additions & 0 deletions packages/ember-handlebars/tests/helpers/each_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,38 @@ test("it works with the controller keyword", function() {
equal(view.$().text(), "foobarbaz");
});

test("it looks up paths with capital letters on the controller", function() {
var controller = Ember.Object.create({
CONSTANT: Ember.A(["foo", "bar", "baz"])
});

Ember.run(function() { view.destroy(); });

view = Ember.View.create({
controller: controller,
template: templateFor("{{#view}}{{#each CONSTANT}}{{this}}{{/each}}{{/view}}")
});

append(view);

equal(view.$().text(), "foobarbaz");
});

test("it falls back to Ember.lookup for paths with capital letters", function() {
Ember.lookup.CONSTANT = Ember.A(["foo", "bar", "baz"]);

Ember.run(function() { view.destroy(); });

view = Ember.View.create({
controller: {},
template: templateFor("{{#view}}{{#each CONSTANT}}{{this}}{{/each}}{{/view}}")
});

append(view);

equal(view.$().text(), "foobarbaz");
});

module("{{#each foo in bar}}", {
teardown: function() {
Ember.run(function() {
Expand Down
23 changes: 15 additions & 8 deletions packages/ember-handlebars/tests/lookup_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,23 @@ test("ID parameters should be looked up on the context", function() {
deepEqual(params, ["Mr", "Tom", "Dale"]);
});

if (Ember.FEATURES.isEnabled("ember-handlebars-caps-lookup")) {
test("ID parameters that start with capital letters use Ember.lookup as their context", function() {
Ember.lookup.FOO = "BAR";
test("ID parameters that start with capital letters fall back to Ember.lookup as their context", function() {
Ember.lookup.FOO = "BAR";

var context = { FOO: "BAZ" };
var context = {};

var params = Ember.Handlebars.resolveParams(context, ["FOO"], { types: ["ID"] });
deepEqual(params, ["BAR"]);
});
}
var params = Ember.Handlebars.resolveParams(context, ["FOO"], { types: ["ID"] });
deepEqual(params, ["BAR"]);
});

test("ID parameters that start with capital letters look up on given context first", function() {
Ember.lookup.FOO = "BAR";

var context = { FOO: "BAZ" };

var params = Ember.Handlebars.resolveParams(context, ["FOO"], { types: ["ID"] });
deepEqual(params, ["BAZ"]);
});

test("ID parameters can look up keywords", function() {
var controller = {
Expand Down
20 changes: 16 additions & 4 deletions packages/ember-metal/lib/binding.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,13 @@ var isGlobalPath = Ember.isGlobalPath = function(path) {
};

function getWithGlobals(obj, path) {
return get(isGlobalPath(path) ? Ember.lookup : obj, path);
var value = get(obj, path);

if (value === undefined && isGlobalPath(path)) {
value = get(Ember.lookup, path);
}

return value;
}

// ..........................................................
Expand Down Expand Up @@ -250,7 +256,7 @@ Binding.prototype = {
if (this._oneWay) {
Ember.trySet(obj, toPath, fromValue);
} else {
Ember._suspendObserver(obj, toPath, this, this.toDidChange, function () {
Ember._suspendObserver(obj, toPath, this, this.toDidChange, function() {
Ember.trySet(obj, toPath, fromValue);
});
}
Expand All @@ -260,8 +266,14 @@ Binding.prototype = {
if (log) {
Ember.Logger.log(' ', this.toString(), '<-', toValue, obj);
}
Ember._suspendObserver(obj, fromPath, this, this.fromDidChange, function () {
Ember.trySet(Ember.isGlobalPath(fromPath) ? Ember.lookup : obj, fromPath, toValue);
Ember._suspendObserver(obj, fromPath, this, this.fromDidChange, function() {
try {
Ember.set(obj, fromPath, toValue);
} catch (e) {
if (Ember.isGlobalPath(fromPath)) {
Ember.trySet(Ember.lookup, fromPath, toValue);
}
}
});
}
}
Expand Down
12 changes: 12 additions & 0 deletions packages/ember-metal/tests/binding/connect_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ testBoth('Connecting a binding between two objects', function(get, set) {
performTest(binding, a, b, get, set);
});

testBoth('Connecting a binding to path with capital letters', function(get, set) {
var b = { bar: 'BAR' };
var a = { foo: 'FOO', B: b };

Ember.lookup.B = { bar: 'wrong' };

// b.bar -> a.foo
var binding = new Ember.Binding('foo', 'B.bar');

performTest(binding, a, b, get, set);
});

testBoth('Connecting a binding to path', function(get, set) {
var a = { foo: 'FOO' };
GlobalB = {
Expand Down