Note that there are some explanatory texts on larger screens.

plurals
  1. POIn Backbone Collection, delete a model by link on itself
    primarykey
    data
    text
    <p>Im just trying to delete a model from a collection, with a link on itself. I've attach the event to the "Eliminar button" but it seems Im losing the reference to the model element that contains it... and can't find it.. can you?:</p> <pre><code>(function ($) { //Model Pelicula = Backbone.Model.extend({ name: "nulo", link: "#", description:"nulo" }); //Colection Peliculas = Backbone.Collection.extend({ initialize: function (models, options) { this.bind("add", options.view.addPeliculaLi); this.bind("remove", options.view.delPeliculaLi); } }); //View AppView = Backbone.View.extend({ el: $("body"), initialize: function () { this.peliculas = new Peliculas( null, { view: this }); //here I add a couple of models this.peliculas.add([ {name: "Flying Dutchman", link:"#", description:"xxxxxxxxxxxx"}, {name: "Black Pearl", link: "#", description:"yyyyyyyyyyyyyy"} ]) }, events: {"click #add-movie":"addPelicula", "click .eliminar":"delPelicula"}, addPelicula: function () { var pelicula_name = $("#movieName").val(); var pelicula_desc = $("#movieDesc").val(); var pelicula_model = new Pelicula({ name: pelicula_name },{ description: pelicula_desc }); this.peliculas.add( pelicula_model ); }, addPeliculaLi: function (model) { var str= model.get('name').replace(/\s+/g, ''); elId = str.toLowerCase(); $("#movies-list").append("&lt;li id="+ elId +"&gt; &lt;a href="+ model.get('link')+"&gt;" + model.get('name') + "&lt;/a&gt; &lt;a class='eliminar' href='#'&gt;Eliminar&lt;/a&gt; &lt;/li&gt;"); }, delPelicula: function (model) { this.peliculas.remove(); console.log("now should be triggered the -delPeliculaLi- event bind in the collection") }, delPeliculaLi: function (model) { console.log(model.get('name')); $("#movies-list").remove(elId); } }); var appview = new AppView; })(jQuery); </code></pre> <hr> <p>And my html is:</p> <pre><code>&lt;div id="addMovie"&gt; &lt;input id="movieName" type="text" value="Movie Name"&gt; &lt;input id="movieDesc" type="text" value="Movie Description"&gt; &lt;button id="add-movie"&gt;Add Movie&lt;/button&gt; &lt;/div&gt; &lt;div id="lasMovies"&gt; &lt;ul id="movies-list"&gt;&lt;/ul&gt; &lt;/div&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload