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 7429191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:55:47+00:00 2026-05-29T08:55:47+00:00

My application is structured like this: There is a Sidebar which contains many items

  • 0

My application is structured like this: There is a Sidebar which contains many items and is generated by a SidebarView. The SidebarView invokes an ItemView for every item in the sidebar:

render: ->
  view = new ItemView({model: the_item})
  $(@el).append(view.render().el)

Then there is a ShowView which displays the item in the main div. There is also a button, which is used to delete the item.

events:
  "click #destroy-button" : "destroy"

destroy: () ->
  @model.destroy()
  this.remove()

  return false

It removes the ShowView from the DOM tree and sends a DELETE request to the server. But what is the best way to remove the ItemView from the sidebar? Adding IDs like <div class="item" data-index="123"></div> and then remove the items via the data-index? I have seen somebody using jQuery.data to bind data to the DOM tree. But both solutions look a bit smelly. Is there an elegant way to accomplish this?

  • 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-05-29T08:55:48+00:00Added an answer on May 29, 2026 at 8:55 am

    Your ItemView should handle the “remove” button. The sequence goes like this:

    1. You hit the remove button.
    2. That triggers an event on the appropriate ItemView.
    3. The ItemView destroys the model.
    4. Destroying the model triggers a 'destroy' event from the model.
    5. The ItemView listens for the 'destroy' event and removes itself when it happens.

    So, your ItemView would look something like this:

    class ItemView extends Backbone.View
        events:
            'click .del': -> @model.destroy()
        initialize: ->
            @model.on('destroy', @remove)
        render: ->
            # ...
            @
        remove: =>
            $(@el).remove()
            # And whatever other cleanup tasks you have...
    

    That way your views will respond appropriately if one of your Item models is destroyed by someone else.

    Demo: http://jsfiddle.net/ambiguous/KMT74/1/

    And if someone else renders the delete button then you’d just need to call destroy on the appropriate model instance and the ItemView would remove itself. See the kill first button in the demo for an example. You could use a data-id attribute on the ItemView’s el to associate models with their views and then do something like:

    your_destroy_button_handler: (ev) ->
        item = @collection.get($(ev.target).data('id'))
        item.destroy()
    

    but it would be cleaner for the ItemView to render its own delete button.

    Also, this:

    events:
      "click #destroy-button" : "destroy"
    

    is going to be a problem as you’ll have duplicate id attributes, use a class for the button instead:

    events:
      "click .destroy-button" : "destroy"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Right now I've got a mod_wsgi script that's structured like this.. def application(environ, start_response):
I wrote a daemon which was structured like this: while( true ) { //
Our application is structured something like: UI <--> REST API <--> Workflow <--> Business
The XML file structure is like this. <?xml version=1.0 encoding=UTF-8?> <Application> <Tabs> <Tab name=1>
There is an interesting post over here about this, in relation to cross-application flow
I have a JSON.NET JObject with data structured like this: { foo : {
I am building an MVVM application. I'm trying to structure my application like this:
Our Application deployment structure is something like this: User can download the application form
The flow for my application, for non-logined users, looks like this: 1) non-logined user
I'm looking to build a tree-like structure for my Java application. I've tried using

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.