Passing variable notation in helpers
ronkpunk opened this issue · comments
Hello and thanks for your (wonderful) plugin.
I'm working on a dynamic dialog that will popup using jsrender template and helpers.
I've this (simplified) code
<script type="text/x-jsrender" id="mytmpl">
<p>{{:~GetLabel('label_key', '{{:my_variable}}')}}</p>
</script>
<script>
$.views.helpers({
GetLabel: function (key, args) { return GetLabel(key, args, true); }
});
function GetLabel(key, args) {
var value = null;
$.ajax({
/* ajax stuff */
}).done(function (response) {
value = response;
});
return value;
}
</script>
I've a "Label" controller in my WebAPI that search for "key" and, if match, try to perform a String.Format passing "args"
I try passing to my helper function either {{:my_variable}} and {{:my_variable}} with no luck
What I'm trying to obtain is to call server side label and put print command in that when I call template render
Is it possible in some way?
Thank you in advance
Stefano
If my_variable
is a property of your contextual data object, then you can pass it to a helper like this
{{:~GetLabel('label_key', my_variable)}}
You never need to nest {{:...}}
inside itself. The ... is where you put a code expression such as myProp + 'x'
, or ~GetLabel(1, 33, 'somestring', some.object.path)
.
But GetLabel() needs to return its result synchronously, so your ajax fetch result won't be returned correctly. You need to do the ajax fetch before rendering the template.... - or else use JsViews data-linking to dynamically update the label once your done() returns....
Sure that GetLabel returns results synchronously.
It works perfectly!
Thank you