admin管理员组文章数量:1388089
I am trying to update a global javascript variable with a value from jsrender template. How can I acplish it...
i have created a fiddle /
i need to have the last movie name into that javascript variable..
I am trying to update a global javascript variable with a value from jsrender template. How can I acplish it...
i have created a fiddle http://jsfiddle/4RH7n/8/
i need to have the last movie name into that javascript variable..
Share Improve this question asked Sep 11, 2012 at 18:24 quickLearnerquickLearner 1412 silver badges12 bronze badges 1- movies is an array you can use movies.push({ name: "any", releaseYear: "any"}) – Rupesh Patel Commented Oct 5, 2012 at 11:39
2 Answers
Reset to default 6Custom tags and helpers can easily be registered to access or modify global variables. There is a sample here (code is here) which shows both ways, and illustrates both setting and getting globals from within a template. Helpers/custom tags are better than using {{* ...}}
(since that way you have better separation of code and markup) - but if you do use {{*
syntax you have to set allowCode=true
. (See this example).
The above approaches are ways of using your own extension to be able to access ANY global. However, for most scenarios, you probably only have certain specific globals you want to access. In that case it is really easy to pass the global(s) in as options on the .render()
call (or the .link()
call if you are using JsViews) like this:
...render(myData, { foo: myFooGlobal, bar: myBarGlobal });
and then access them in any expression within the template, such as like this:
... {{:~foo}} ... {{for ~bar}} ... {{/for}} ...
If you want to be able to access those globals from all your templates, (or for all calls to a specific template) you can instead register them as global (or template specific) helpers/template parameters, and then you don't need to pass them in with the render/link call.
For example, to register them globally, for all templates:
$.views.helpers({
foo: myFooGlobal,
bar: myBarGlobal
});
You then access them in exactly the same way as above, using ~foo
(or foo()
for a function).
You'll find sample of all of these approaches on GitHub.
UPDATE:
In addition to the documentation and samples referenced above, there are much more recent documentation and samples on http://www.jsviews., and specifically here and here.
Not really sure what you are asking.. The data wouldn't be ing from the template, you already have the data in that object. Just use it directly:
window.abcfx = function() {
alert(movies[movies.length-1].name);
}
fiddle
本文标签: Accessing a global javascript variable inside jsrender template and updating itStack Overflow
版权声明:本文标题:Accessing a global javascript variable inside jsrender template and updating it - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744568249a2613182.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论