Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8760331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:00:59+00:00 2026-06-13T15:00:59+00:00

EDIT1: I have tried many different strategies for deleting the element including this.model.destroy(), this.collection.remove(this.model),

  • 0

EDIT1: I have tried many different strategies for deleting the element including this.model.destroy(), this.collection.remove(this.model), etc. I have also bound the events using the events: syntax and using a direct binding strategy in both initialize and render.

If I change the code to use this.collection.add instead of remove it adds an additional X notes where X is the number of notes currently in the collection.

I am writing a basic list app (to be fleshed out eventually) and I want each element in the list to have a button that will delete it from the list.
I am using

  • require.js
  • backbone
  • lo-dash
  • jquery

My code snippets below show my view code for the Notes View, Note View and some supporting into including the templates being used to render each.

At the moment, the code is not functioning as desired because clicking ANY Note’s “hide” button causes all elements in the collection to be removed one at a time. I know that is what is happening because I can insert alerts at the end of the “deletenote” method which allows me to view the deletion piece-wise.

I also know that the parent render method is not the cause of the problem because I can turn off the event callback to re-render the parent NotesView and all Note views are still deleted.

<script type="text/template" id="noteslist-template">
    <ul id="noteslist" style="list-style: none;">
    </ul>
</script>

<script type="text/template" id="note-template">
<li>
    <button type="button" class="notesbutton" id="hidenote">hide</button>
    <div id="note"><%= text %></div>
</li>
</script>

NotesView.js

define
([  'jquery',
'underscore',
'backbone',
'notes/sample/sampletext',      //included to populate a collection for rendering *REMOVE ONCE DONE TESTING*
'notes/collections/Notes',      //included to create a new instance of the Notes collection during the initialize method of this view
'notes/models/Note',            //included to reference the model when creating NoteView instances for rendering
'notes/views/NoteView'      ],  //included to call render functions on each Note model in the collection
function($,_,Backbone,SampleText,Notes,Note,NoteView)
{
    var NotesView = Backbone.View.extend
    ({
        initialize: function() 
        {
            this.template = _.template($('#noteslist-template').html());
            _.bindAll(this,'render','rendernote');
            this.collection.bind('add',this.render);
            this.collection.bind('remove',this.render);
        },
        render: function() 
        {
            console.log('collection render');
            this.$el.html(this.template({}));       //change to this.notelist = THISLINE
            this.collection.each(this.rendernote);
            //add call to render notelist to DOM
            return this;
        },
        rendernote: function(note)      //add notelist variable
        {
            console.log('collection rendernote');
            var noteview = new NoteView(
                                {   model:note,
                                    collection:this.collection} );
            //add notelist  +=  LINEBELOW
            noteview.setElement(this.$('#noteslist'));
            noteview.render();
            //change noteview.render to NOT write to DOM
        }
    });
    return NotesView;
}
);

NoteView.js

define
(   [   'jquery',
    'underscore',
    'backbone',
    'notes/models/Note',    ],  //include the Note model to reference as the model for the collection
function($,_,Backbone,Note)
{
    var NoteView = Backbone.View.extend
    ({
        tagName: "li",
        className: "note",

        events:
        {
            'click #hidenote':'deletenote',

        },

        initialize: function()
        {
            _.bindAll(this,'render','remove','deletenote');
            //this.model.bind('change',this.render);
            this.template = _.template($('#note-template').html());
            this.model.bind('remove', this.remove);
        },
        render: function()
        {
            this.notetorender = this.template(this.model.toJSON());
            this.$el.append(this.notetorender);
            return this;
        },
        deletenote: function()
        {
            this.options.collection.remove(this.model);
        }
    });
    return NoteView;
}
);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T15:01:00+00:00Added an answer on June 13, 2026 at 3:01 pm

    noteview.setElement(this.$('#noteslist')); causes event delegation.
    So eventhough you wrote ‘click #hideNotes : deleteNode’ inside NoteView, after the setElement call it works like that code is present inside NotesListView.

    So when you click hide button of single li and expect only that li to be removed from ul however the click event is received by all li’s hide button.That’s why all items in collection are deleted .

    JsFiddle here i do the same thing without using setElement

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tried many different online wysiwyg editors. And many of them have HTML
I have tried to find a solution to this issue for many hours to
I have tried many other questions on this site today but none of them
I've tried so many different possibilities to achieve what I feel like should be
I've tried many different ways of accessing the name of an attribute, but just
For some reason...maybe looking at too many different examples...this is one of those things
I have many different nodes connected with relationships. On those relationships, I use some
I am pretty new to jqm i have tried about 20 different things and
I need to monitor console output in Java, I have tried several different ways
EDIT i have something like this in a file: imagecolor=0 arrayimagecolorcopy=0 arrayimagecolorcopy3d=0 when i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.