| Page 1 of 1 | [ 6 posts ] |
MarcYUI Contributor
|
Some of the variables I use in my Handlebars templates are global. They tend to be ones I store in YUI_config / Y.config
What I do is that I set this object as part of the model passed to the Handlebars template. Not very elegant. Code: render:function(){ var container = this.get('container'), data = {}; data.YUI_config = Y.config; data.messages = this.get("model").toJSON(); container.setHTML(this.template(data)); return this; } I have a few questions about this: 1. Whenever I am in the context of a helper, I lose access to the model elements passed to the template. The helper only "knows" the variables passed to it through the helper interface. This does not happen in my Java implementation of Handlebars and it doesn't make sense to me either. I'm not sure which is right and what is the suggested way for dealing with global variables. 2. Is there perhaps a way to global variables at a central level so that they will always be included as part of the data-model? Kind regards, Marc |
MarcYUI Contributor
|
Seems to be a known issue. https://github.com/wycats/handlebars.js ... nt-4206666
|
MarcYUI Contributor
|
So, according to the Handlebars.js documentation, you should be able to access the parent scope (which seems to suggest the root template scope) from withing a Partial or Helper by using {{../myVar}}.
When I try this I get an error because depth1 is undefined. So, it seems that the ../myVar is parsed correctly to look up a scope one level up. But that level is empty. Any suggestions here? Kind regards, Marc |
|
Are you saying that your Handlebars helper functions are not being called with full context which was passed to the original template function? And this situation is arising when a helper is invoked inside of a partial?
|
MarcYUI Contributor
|
Yes, I think that is what I'm saying.
I'll give you an example Code: Handlebars messages.hbs {{#stripeRows messages "message"}} {{#mugshot message.from "sma" "sqr"}} {{> mugshot}} {{/mugshot}} {{/stripeRows}} mugshot.hbs {{../YUI_config.serverUrl}}/img/{{myIgm}} Code: Javascript code var inbox = Y.Base.create('inbox', Y.App,[],{ initializer: function () { Y.Handlebars.registerPartial("mugshot",templates.template('common-mugshot-hbs')); } [...] var ListView = Y.Base.create( 'listView', Y.View, [],{ render:function(){ var container = this.get('container'), data = {}; data.YUI_config = YUI_config data.messages = this.get("model").toJSON(); container.setHTML(this.template(data)); return this; } [...] I don't see the undefined depth1 error anymore. Could be the 3.6.0pr4->3.6.0 upgrade. But yes, I don't have access to the original context when I use the Path ../YUI_config.serverUrl within a registered partial template. Based on the Handlebars.js Path docs I think you should be able to. If not, than it would be useful to be able to register a mixin object when you register a partial to be able to expose some global variables. |
|
Encountered this issue myself, have raised a ticket as I'm pretty sure its a bug (and it used to work in YUI 3.5): http://yuilibrary.com/projects/yui3/ticket/2532995
|
| Page 1 of 1 | [ 6 posts ] |
| You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum |
© 2006-2013 Yahoo! Inc. All rights reserved.
All code on this site is licensed under the BSD License unless stated otherwise.
About This Site · Security Contact Info
Powered by phpBB® Forum Software © phpBB Group