In Sugarcrm (backbone.js) I am trying to get custom attribute (user_id) from List (in .hbs file)
<div class='dropdown usersLegend'>
<ul class='dropdown-menu'></ul>
</div>
and bind data dynamically like
_.each(data.users, function (user) {
list += '<li user_id="' + user.id + '"> ... </li>';
});
this.$('.usersLegend ul').html(list);
I made custom event in initialize
this.events = {
'click li': 'getselectedUser',
};
and in method, i tried the following code
let currentTarget = JSON.stringify(e.currentTarget);
if (currentTarget != null) {
var doc = new DOMParser().parseFromString(currentTarget, "text/xml");
var tmpDiv = doc.createElement('div');
tmpDiv.innerHTML = currentTarget;
var links = tmpDiv.getElementsByTagName('li');
[...links].forEach((link) => {
console.log(link.getAttribute('user_id'));
});
}
this way I am not getting user_id value, how can i bind user_id in custom event
The
.hbsextension refers to Handlebars, which is a template language. This means that you can insert the user IDs right in the.hbsfile:... without having to write the
_.eachloop, if you write therendermethod of your view as follows:How this works:
dataobject that contains theusersarray is passed to the compiled template.usersarray inside the{{#users}}...{{/users}}. (This is a notation that Handlebars inherited from Mustache (more up-to-date language reference here). You could also use the notation{{#each users}}...{{/each}}, which is specific to Handlebars.)<li>tag with the user ID inserted in an attribute. Note that I usedata-user-idrather thanuser_idas the attribute name; thedata−prefix has a special meaning to the browser and to jQuery, so this notation is more likely to work. The{{{id}}}notation means "take theidattribute of the current object (which is the current user) and interpolate it in the template output without escaping" ({{id}}would do the same thing but with escaping).this.$el.html(). I presume this last part is currently already happening in your code.Now, what remains is to take out the user ID after the
'click'event. For this,event.target(notevent.currentTarget) and jQuery's.datamethod are your friends: