SlexAxton / require-handlebars-plugin

A plugin for handlebars in require.js (both in dev and build)
804 stars 202 forks source link

Optimized code: Partial can’t be found when template is invoked with _.map #247

Open Haraldson opened 8 years ago

Haraldson commented 8 years ago

Optimized: Partials works nicely, unless they’re invoked from a template that is in turn invoked from _.map. Unoptimized: Partials works no matter the context they’re in.

Am I supposed to bind my compiled template somehow, and if so, to what context?

Example given below

view.js

define(
    ['marionette', 'hbs!./template', 'hbs!./accepted-sources-template'],
    function(Marionette, Template, AcceptedSourcesTemplate)
    {
        return Marionette.ItemView.extend(
        {
            template: Template,

            setAcceptedSources: function()
            {
                // This here doesn’t work when code has been optimized
                var acceptedSources = _.chain(this.wrapper.sources)
                    .map(AcceptedSourcesTemplate)
                    .compact()
                    .value()
                    .join(',');

                this.$el.trigger('droppable:options:set', { accept: acceptedSources });
            }
        });
    }
);

accepted-sources-template.hbs

{{~#each operations~}}
    #panel-right .aggregate[data-source*="{{>type-sub-type-operation type=../type sub_type=../sub_type operation=this}}"] > div,#panel-right .results[data-source*="{{>type-sub-type-operation type=../type sub_type=../sub_type operation=this}}"] > ol > li{{~#unless @last~}},{{~/unless~}}
{{~/each~}}
SlexAxton commented 8 years ago

This definitely sounds like a context problem. Maybe you can do a {{log this}} in each mode to figure out what the context is?

Haraldson commented 8 years ago

{{log this}} returns the correct attributes in all the templates.

The weird thing is that I can use other partials without any problems at all.

{{! The one causing problems – always called from inside a loop, thus the ../ and the this }}
{{>type-subtype-operation type=../type sub_type=../sub_type operation=this}}

{{! This one’s playing along nicely }}
{{>image image=image alt=title}}