backbone icon indicating copy to clipboard operation
backbone copied to clipboard

Remove method removes the element from the DOM

Open tsadoklf opened this issue 13 years ago • 6 comments

Remove method removes the container element ($el) from the DOM.

This behaviour is wrong since it does not allow for reuse of the same DOM element (as in creating a view, removing it and creating another view - all in the same DOM element).

I suppose that using jquery's empty method would do a better work albeit, when trying it, I still could not get rid of the events of the removed views (so when I triggered an event on the current view the event was triggered on the removed views - which were of the same view type).

tsadoklf avatar Oct 29 '13 06:10 tsadoklf

Have a look at #2525 #2168

As for using view.$el.empty() you will need to call undelegateEvents on the view to remove the event binding.

I can't comment on whether it is a good practice or not to reuse DOM element for binding events but if that's the behaviour you looking for then you are free to patch Backbone the way you want it.

j03w avatar Oct 29 '13 09:10 j03w

Please have a look at my suggestion for View.removeSelf() method in backbone.layoutmanager/issues/400.

Backbone's View.remove() has an unexpected behaviour as it removes something that logically should not have been removed.

It seems to me that when writing View.remove(), Backbone's developers wanted to follow the logic of jQuery remove(), but while jQuery's remove() removes the element(s) on which it is applied, Backbone's View.remove() removes not only the View object but the container DOM element as well. In my view this is an unexpected behaviour and therefore should have been avoided.

Backbone's View.remove() method should have followed the same logic of jQuery and removed only the view on which it was applied and not any other DOM elements, including the container element.

tsadoklf avatar Oct 29 '13 12:10 tsadoklf

A Backbone View's this.el is the element that represents the view.

Feel free to just empty it, if you like. There's nothing stopping you...

jashkenas avatar Oct 29 '13 12:10 jashkenas

You're right. There's nothing stopping me but still, look at it this way: the container DOM element was part of the web page before inserting/injecting a View into it. Why should it be removed if I just remove the View?

tsadoklf avatar Oct 29 '13 13:10 tsadoklf

the container DOM element was part of the web page before inserting/injecting a View into it

It can be, but if you're doing your views "right", then it certainly isn't. ;)

jashkenas avatar Oct 29 '13 19:10 jashkenas

Yeah, this is more of a reason to remove the facility to provide a pre-existing el, than to encourage this form.

lukesargeant avatar Oct 30 '13 19:10 lukesargeant