Skip to content

Commit

Permalink
View helper learns about the container
Browse files Browse the repository at this point in the history
```javascript
// stays the same
{{view App.FuView}}

// uses the container
{{view 'fu'}}
```
  • Loading branch information
stefanpenner committed Jul 8, 2013
1 parent 96ae9fa commit ae116fa
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 1 deletion.
15 changes: 14 additions & 1 deletion packages/ember-handlebars/lib/helpers/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ require("ember-handlebars");

var get = Ember.get, set = Ember.set;
var EmberHandlebars = Ember.Handlebars;
var LOWERCASE_A_Z = /^[a-z]/;
var VIEW_PREFIX = /^view\./;

EmberHandlebars.ViewHelper = Ember.Object.create({

Expand Down Expand Up @@ -121,7 +123,18 @@ EmberHandlebars.ViewHelper = Ember.Object.create({
newView;

if ('string' === typeof path) {
newView = EmberHandlebars.get(thisContext, path, options);

// TODO: this is a lame conditional, this should likely change

This comment has been minimized.

Copy link
@jayphelps

jayphelps Aug 17, 2013

Contributor

How about moving it into Ember.Handlebars.get so other places using that getter function can benefit as well? This seems to work and passes all the current tests:

var handlebarsGet = Ember.Handlebars.get = function(root, path, options) {
  var data = options && options.data,
      normalizedPath = normalizePath(root, path, data),
      LOWERCASE_A_Z = /^[a-z]/,
      value;

  // 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;

  value = Ember.get(root, path);

  if (value === undefined && root && root.container && LOWERCASE_A_Z.test(path)) {
    value = root.container.lookup('view:' + path);
  }

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

I excluded the view. prefix check, but if it's important obviously keep it.

// but something along these lines will likely need to be added
// as deprecation warnings
//
if (options.types[0] === 'STRING' && LOWERCASE_A_Z.test(path) && !VIEW_PREFIX.test(path)) {
Ember.assert("View requires a container", !!data.view.container);
newView = data.view.container.lookupFactory('view:' + path);
} else {
newView = EmberHandlebars.get(thisContext, path, options);
}

Ember.assert("Unable to find view at path '" + path + "'", !!newView);
} else {
newView = path;
Expand Down
87 changes: 87 additions & 0 deletions packages/ember-handlebars/tests/helpers/view_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
var view, originalLookup;

var container = {
lookupFactory: function(){ }
};

function viewClass(options){
options.container = options.container || container;
return Ember.View.extend(options);
}

module("Handlebars {{#view}} helper", {
setup: function() {
originalLookup = Ember.lookup;

},

teardown: function() {
Ember.lookup = originalLookup;
Ember.run(view, 'destroy');
}
});


test("View lookup - App.FuView", function(){
Ember.lookup = {
App: {
FuView: viewClass({
elementId: "fu",
template: Ember.Handlebars.compile("bro")
})
}
};

view = viewClass({
template: Ember.Handlebars.compile("{{view App.FuView}}")
}).create();

Ember.run(view, 'appendTo', '#qunit-fixture');

equal(Ember.$('#fu').text(), 'bro');
});

test("View lookup - 'App.FuView'", function(){
Ember.lookup = {
App: {
FuView: viewClass({
elementId: "fu",
template: Ember.Handlebars.compile("bro")
})
}
};

view = viewClass({
template: Ember.Handlebars.compile("{{view 'App.FuView'}}")
}).create();

Ember.run(view, 'appendTo', '#qunit-fixture');

equal(Ember.$('#fu').text(), 'bro');
});

test("View lookup - 'fu'", function(){
var FuView = viewClass({
elementId: "fu",
template: Ember.Handlebars.compile("bro")
});

var container = {
lookupFactory: lookupFactory
};

view = Ember.View.extend({
template: Ember.Handlebars.compile("{{view 'fu'}}"),
container: container
}).create();

Ember.run(view, 'appendTo', '#qunit-fixture');

equal(Ember.$('#fu').text(), 'bro');

function lookupFactory(fullName) {
equal(fullName, 'view:fu');

return FuView;
}
});

0 comments on commit ae116fa

Please sign in to comment.