earnold
earnold

Reputation: 1462

using an ember-rails handlebars template outside the context of an ember view

I have a rails app that is using the ember-rails gem.

There is a section of my site that is not on ember, but where it would be convenient to use one of the handlebar templates served via the asset pipeline. However, something seems to be going wrong. Specifically, my template is returned like so:

Ember.TEMPLATES["views/wanderlists/templates/gallery"] = Handlebars.template(function anonymous(Handlebars,depth0,helpers,partials,data) { helpers = helpers || Ember.Handlebars.helpers;   var self=this;


  data.buffer.push("<h1>Gallery!</h2>\n"); });

However, if I try to use this template:

Ember.TEMPLATES["views/wanderlists/templates/gallery"]({})
TypeError: Cannot read property 'buffer' of undefined

Any idea why the generated template would be having trouble?

Upvotes: 4

Views: 997

Answers (1)

Mike Grassotti
Mike Grassotti

Reputation: 19050

Any idea why the generated template would be having trouble?

You can't call handlebars templates compiled by the ember handlebars compiler as if they were normal handlebars templates. They expect a completely different set of arguments. Specifically, they expect to be passed (context, options) where options has a data.buffer that output will be written to. So for example, if you try:

Ember.TEMPLATES["views/wanderlists/templates/gallery"](this, {data: {buffer: 'NOT-A-BUFFER'}})

console should output TypeError: Object NOT-A-BUFFER has no method 'push'

There is a section of my site that is not on ember, but where it would be convenient to use one of the handlebar templates served via the asset pipeline.

OK. This is really easy to do, just not by accessing Ember.TEMPLATES directly. Instead use an Ember.View, and call appendTo() directly to render. For example:

App = Ember.Application.create({});
var view = Ember.View.create({
  templateName: "views/wanderlists/templates/gallery",
  name: "Bob"
});
view.appendTo("#message");

Working example here: http://jsfiddle.net/mgrassotti/VWmFq/1/

For more details see Ember Guides: Defining a view

Upvotes: 6

Related Questions