문제

I have a Handlerbar that has a button, like this:

<button id="addLine" type="image" class="AddButtonClass" title="Add New Line" onclick = "addNewBlock('{{blockId}}');"></button>

I then use this template to create objects dynamically, like this:

    var template = window.app.getTemplate('myHandlebar');       
    var budgetBlock = $(template({blockId: guid}));  
    $("#BudgetTable tbody").append(budgetBlock);

I then have a function like this:

addNewBlock: function(getId){
    alert(getId);
},

The problem is that the button on the Handlebar event never fires. Instead, I get an error "Uncaught ReferenceError: addNewBlock is not defined "

Why cant I assign an onclick event to a button in the handlebars? Is there a way to add an event to a button, dynamically, when the template is created?

thanks

도움이 되었습니까?

해결책

try this instead:

<button id="addLine" type="image" class="AddButtonClass" title="Add New Line" data-id="{{blockId}}"></button>

and in your backbone for example in your initialize function:

$('#addLine').click(function(){
    alert($(this).data('id'));
});

or you can create an event in backbone

events: {
 'click #addLine' : 'addNewBlock'

},
addNewBlock: function(e){
     console.log( $(e.currentTarget).data("id"));
}        
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top