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

Handlebars cannot see the added view helpers #2

Open
revelfire opened this issue Jun 28, 2013 · 4 comments
Open

Handlebars cannot see the added view helpers #2

revelfire opened this issue Jun 28, 2013 · 4 comments

Comments

@revelfire
Copy link

The view sample presented isn't working for me.

here's my test:

var User = Backbone.Model.extend();
var HelloView = Backbone.HandlebarsView.extend({
template: 'Hello {{name}}'
})
var AppView = Backbone.HandlebarsView.extend({
template: '

Hello

{{view "HelloView" model=this}}'
});
var app = new AppView({model: new User({name: 'Foli'})});
console.log(app.render().$el.html());

I am using the current version of the plugin, the latest backbone and jquery. I am using requirejs. Here's the entire code dump:

https://gist.github.com/revelfire/5881952

@loicfrering
Copy link
Owner

Hi, looks to be an issue with the latest versions of Handlebars: your example works for me with Handlebars 1.0.0-rc.3. Can you confirm me that you encounter this issue with a newer version (1.0.0.rc.4 or 1.0.0)?

In the meantime, I'm gonna investigate. Thanks for reporting!

@revelfire
Copy link
Author

Yep! Thanks for your response.

#Snippet:

Handlebars.VERSION = "1.0.0";
Handlebars.COMPILER_REVISION = 4;

Handlebars.REVISION_CHANGES = {
    1: '<= 1.0.rc.2', // 1.0.rc.2 is actually rev2 but doesn't report it
    2: '== 1.0.0-rc.3',
    3: '== 1.0.0-rc.4',
    4: '>= 1.0.0'
};

@revelfire
Copy link
Author

Loicfrering, thanks for any support :)

Additional research here. Per you comment I punted back to 1.0.0-rc3. It 'partially' works. Result is:

Hello

testTemplate.html:54 GET http://localhost:8080/js/HelloView.js 404 (Not Found) require-with-comments.js:1872 Uncaught Error: Script error for: HelloView http://requirejs.org/docs/errors.html#scripterror require-with-comments.js:163

So it seems to partially work - we get through the first render but die trying to get HelloView inserted in the subview render.

Given that result I thought maybe something weird was happening with the AMD/require stuff, so I did a trimmed down version. Same code, but no AMD. Technically same result - cannot find HelloView.

<script src="js/lib/jquery/jquery.min.js"></script> <script src="js/lib/lodash/lodash.min.js"></script> <script src="js/lib/backbone/backbone.js"></script> <script src="js/lib/view/handlebars-1.0.0-rc3.js"></script> <script src="js/lib/view/backbone.handlebars.js"></script>
<script type="text/javascript">
    $(function() {
        var User = Backbone.Model.extend();
        var HelloView = Backbone.HandlebarsView.extend({
            template: 'Hello {{name}}'
        })
        var AppView = Backbone.HandlebarsView.extend({
            template: '<h1>Hello</h1>{{view "HelloView" model=this}}'
        });
        var app = new AppView({model: new User({name: 'Fool'})});
        console.log(app.render().$el.html());
    });

</script>

RESULT:
Uncaught Error: Cannot resolve view "HelloView" backbone.handlebars.js:65

This example is still pretty much exactly what is in the docs so I'm not sure what's going on.

Thanks,
Chris

@loicfrering
Copy link
Owner

The HelloView must be accessible outside of your module pattern function in order to be resolved by Backbone.Handlebars, you can either:

  • attach it to the window global object: window.HelloView = ...
  • attach it to some sort of global namespace: App.HelloView = ... and then {{view "App.HelloView" ...}}
  • if Backbone.Handlebars detects require.js, it will try to load the HelloView module, so you just have to declare this HelloView in a dedicated module

FYI, here is the view resolution related code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants