Skip to content

Instantly share code, notes, and snippets.

@wycats
Forked from anonymous/gist:2588727
Created May 3, 2012 19:42
Show Gist options
  • Save wycats/2588728 to your computer and use it in GitHub Desktop.
Save wycats/2588728 to your computer and use it in GitHub Desktop.
diff --git a/packages/ember-handlebars/lib/helpers/binding.js b/packages/ember-handlebars/lib/helpers/binding.js
index dd4fc0f..c0d28a7 100644
--- a/packages/ember-handlebars/lib/helpers/binding.js
+++ b/packages/ember-handlebars/lib/helpers/binding.js
@@ -13,7 +13,6 @@ var forEach = Ember.ArrayUtils.forEach;
var EmberHandlebars = Ember.Handlebars, helpers = EmberHandlebars.helpers;
-(function() {
// Binds a property into the DOM. This will create a hook in DOM that the
// KVO system will look for and update if the property changes.
var bind = function(property, options, preserveContext, shouldDisplay, valueNormalizer) {
@@ -147,7 +146,6 @@ var EmberHandlebars = Ember.Handlebars, helpers = EmberHandlebars.helpers;
return bind.call(context, property, fn, true, func, func);
});
-})();
/**
@name Handlebars.helpers.with
@@ -156,10 +154,27 @@ var EmberHandlebars = Ember.Handlebars, helpers = EmberHandlebars.helpers;
@returns {String} HTML string
*/
EmberHandlebars.registerHelper('with', function(context, options) {
- Ember.assert("You must pass exactly one argument to the with helper", arguments.length === 2);
+ if (arguments.length === 4) {
+ var keywordName, path;
+
+ Ember.assert("If you pass more than one argument to the with helper, it must be in the form #with foo as bar", arguments[1] === "as");
+ options = arguments[3];
+ preserveContext = true;
+ keywordName = arguments[2];
+ path = arguments[0];
+
Ember.assert("You must pass a block to the with helper", options.fn && options.fn !== Handlebars.VM.noop);
+ options.data.keywords[keywordName] = getPath(this, path);
+
+ return bind.call(this, path, options.fn, true, function(result) {
+ return !Ember.none(result);
+ });
+ } else {
+ Ember.assert("You must pass exactly one argument to the with helper", arguments.length === 2);
+ Ember.assert("You must pass a block to the with helper", options.fn && options.fn !== Handlebars.VM.noop);
return helpers.bind.call(options.contexts[0], context, options);
+ }
});
diff --git a/packages/ember-handlebars/tests/helpers/with_test.js b/packages/ember-handlebars/tests/helpers/with_test.js
new file mode 100644
index 0000000..cdf98f9
--- /dev/null
+++ b/packages/ember-handlebars/tests/helpers/with_test.js
@@ -0,0 +1,17 @@
+var appendView = function(view) {
+ Ember.run(function() { view.appendTo('#qunit-fixture'); });
+};
+
+module("Handlebars {{#with}} helper");
+
+test("it should support #with foo as bar", function() {
+ var view = Ember.View.create({
+ template: Ember.Handlebars.compile("{{#with person as tom}}{{title}}: {{tom.name}}{{/with}}"),
+ title: "Señor Engineer",
+ person: { name: "Tom Dale" }
+ });
+
+ appendView(view);
+
+ equal(view.$().text(), "Señor Engineer: Tom Dale", "should be properly scoped");
+});
diff --git a/packages/ember-views/lib/views/view.js b/packages/ember-views/lib/views/view.js
index 63ce13c..43b1b76 100644
--- a/packages/ember-views/lib/views/view.js
+++ b/packages/ember-views/lib/views/view.js
@@ -743,20 +743,23 @@ Ember.View = Ember.Object.extend(Ember.Evented,
templateData = this.get('templateData'),
controller = this.get('controller');
+ var keywords = templateData ? Ember.copy(templateData.keywords) : {};
+ keywords.view = get(this, 'concreteView');
+
+ // If the view has a controller specified, make it available to the
+ // template. If not, pass along the parent template's controller,
+ // if it exists.
+ if (controller) {
+ keywords.controller = controller;
+ }
+
var data = {
view: this,
buffer: buffer,
isRenderData: true,
- keywords: {
- view: get(this, 'concreteView')
- }
+ keywords: keywords
};
- // If the view has a controller specified, make it available to the
- // template. If not, pass along the parent template's controller,
- // if it exists.
- data.keywords.controller = controller || (templateData && templateData.keywords.controller);
-
// Invoke the template with the provided template context, which
// is the view by default. A hash of data is also passed that provides
// the template with access to the view and render buffer.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment