Handlebras: Is there a way for custom helper to get the name of the partial calling it? - handlebars.js

In my custom helper, the options object has the loc property where it returns the start and end position of the parsed code where the helper is called. Is there a way to also get the partial name from the helper?
Handlebars.registerHelper("helper", function(options) {
// get the partial name "part" here
});
Handlebars.registerPartial("part", `
{{helper}}
`);
Handlebars.compile(`
{{> part}}
`)();

Related

How to pass values to the function on click event, in polymer-3.x?

I am looping an array of objects. each object has name and address for example. On click event, i want to send the clicked name to the function.
Example: on-click="myFunc('[[item.name]]')"
My current code:
static get template() {
...
<template is="dom-repeat" items="[[users]]">
<a name="[[item.name]]" on-click="myFunc">[[item.name]]</a>
</template>
}
myFunc(name) {
console.log('Clicked name is ', name);
}
How to get the clicked name in the function myFunc ??
The easiest way is to use the API:
When you add a declarative event handler inside the dom-repeat
template, the repeater adds a model property to each event sent to the
listener. The model object contains the scope data used to generate
the template instance, so the item data is model.item
Using that information, you can access your item using
myFunc(event) {
const name = event.model.item.name;
console.log('Clicked name is', name);
}

Select a css class from an item value

In a cshtml page I'm able to select a class in function of a database value. Example:
<div class="alert #item.State" role="alert">#item.State</div>
"State" is an enum of my table defined as follow:
public enum States
{
Ready,
Setup,
Pause,
Error,
Maintenance,
Emergency,
Disconnected
}
the Razor syntax above will add a class to my alert in function of the item value. Good!
But now I need to "translate" this class to Bootstrap's one. Example:
Ready: alert-primary
Pause or Disconnected: alert-secondary
Error: alert-danger
etc...
I'm able to this manually with Razor, but I wonder if there's a convenient way to this directly in css, something like (metacode):
.Ready {
return alert-primary
}
.Pause, .Disconnect {
return alert-secondary
}
You can declare an array of strings which will contain the class names:
string[] ClassList = new string[]
{
"ReadyClass", "SetupClass", "PauseClass", "ErrorClass", "MaintenanceClass", "EmergencyClass", "DisconnectedClass"
};
Now you can display the equivalent class name using the following line
#ClassList[(int)item.State]
I did not test it but it should work.

Function call from aspx design time error "name does not exist in the current context"

I want to change the styles of certain elements on my page depending on the role of the logged in user.
To achieve this, I'm calling a function to execute some server code that does the evaluation and returns a string being the name of the style rule:
<li class="<%=CheckAllowed("admin"); %>">
In my code file I have the following:
public string CheckAllowed(string role)
{
if (HttpContext.Current.User.IsRole(role))
{
return "visible-menu-item";
}
else
{
return "hidden-menu-item";
}
}
The function name in the markup is underlined red with the following error:
The name 'CheckAllowed' does not exist in the current context.
Am I calling the function correctly? If not, how would this be done correctly?
In your sample code, your function is named CheckAddlowed, not CheckAllowed...
Try this syntax:
<% Response.Write(CheckAllowed("admin")); %>
From the use of HttpContext.Current in the method, it looks like it is located in a separate class than the page you are using it on. So provide the full namespace.
<%= Project1.ClassName.CheckAllowed("admin") %>
You probably need to make it a static class to work. But the intellisense should tell you that.
And it's IsInRole not IsRole

Create variable in view and assign

I need to create a string variable in my View and assign to a hidden input which is bound to a property on my strongly typed model
I guess it is something like
#Html.HiddenFor(m=>m.nodelist, new {#value = myVar})
however not working
Don't try to be setting the #value attribute on the html helper. That's not how those helpers have been designed to work. They have been designed to bind to the value of the corresponding property on the view model you passed to this view.
So it's as simple as:
#Html.HiddenFor(m => m.nodelist)
and in the controller action rendering this view you would assign the corresponding property on the view model to the desired value:
public ActionResult Index()
{
MyViewModel model = ...
model.nodelist = someVariable;
return View(model);
}
If on the other hand this variable is actually a javascript variable then you should obviously use javascript to assign it to the hidden field:
<script type="text/javascript">
var myVar = 'some value';
$('#nodelist').val(myVar);
</script>
or if you are not using jQuery:
<script type="text/javascript">
var myVar = 'some value';
document.getElementById('nodelist').value = myVar;
</script>
use simple html hidden field and keep it's name same as property name.
This works perfectly
#{
string val=myVar;
}
<input type="hidden" name="nodelist" value="#val" />
Is myVar a javascript variable?
You could assign your hidden field an ID property.
#Html.HiddenFor(m=>m.nodelist, new {#id = "hdnNodeList"})
and using javascript or jquery you could do the following
$('#hdnNodeList').val(myVar);
or if myVar is a C# variable you could try the following (this will render an html output, although you could do it here, its better to assign a value to m.nodelist in your controller if possible)
$('#hdnNodeList').val('#myVar');

Passing data to controller while Using URL.Action in jQuery template in Asp.Net MVC dynamic views

I have a dynamic view created using jQuery template. The UI is basically a grid with last column being a hyper-linked image. On click on that image i want to pass some data to the controller and that data is present in some other column of the same row. Following is the markup of the code
UI :
{{each Results}}
<tr>
<td>${UserName}</td>
{{if IsActive === 'True'}}
<td><a href=<%: Url.Action("Test","User",new {userName=???,mode="DeActivate"}) %>><img></img><a></td>
{{/if}}
</tr>
{{/each}}
Controller :
public ActionResult Test(string userName,string mode)
{
}
Basically I want to associate the ${UserName} value to userName field in Url.Action's parameter list.
Most of the examples I have seen on Url.Action or Html's helper action methods have string data as parameters. I want to know whether it is possible to read data from other controls in the page inside Url.Action parameter list.
Try like this:
<a href=<%: Url.Action("Test", "User", new { mode = "DeActivate" }) %>&userName=${$item.urlEncodeUserName()}>
...
</a>
where you have defined:
$('#someTemplate').tmpl(contacts, {
urlEncodeUserName: function () {
return encodeURIComponent(this.data.UserName);
}
}).appendTo('#someContainer');

Resources