André Alves
André Alves

Reputation: 6765

Backbone.js + Handlebars.js - {{#each}} instead of model view?

I have a Collection called Projects that contains several models of type Project. When I want to list all the projects, I use a ListProjectsView. Its render function generates a ProjectView for each project in the collection, making it possible to delete one single project by binding an event to its name.

However, now I want to use Handlebars for creating templates. With handlebars it easier to display all the collection models, with the each helper, like this:

<ul>
  {{#each projects}}
  <li>{{this.name}}</li>
  {{/each}}
</ul> 

This works fine but now I am wondering how can I delete one project, since I am not using ProjectView anymore. Do I need to add an id to each <li> so I can bind an event? Or do I have to use ProjectView to do this?

Thanks in advance.

Upvotes: 0

Views: 247

Answers (1)

Evan
Evan

Reputation: 6115

the logic here will have to run through ListProjectsView. One way is to do as you say and attach an data-id to each li and then in your event handler get the target

deleteProjectHandler : function(evt){
    var $target = $(evt.target), id = $target.data('id');

    this.deleteProject(id);
}

Or something of the like.

Upvotes: 1

Related Questions