New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove method removes the element from the DOM #2834

Closed
tsadoklf opened this Issue Oct 29, 2013 · 6 comments

Comments

Projects
None yet
4 participants
@tsadoklf

tsadoklf commented Oct 29, 2013

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).

@j03w

This comment has been minimized.

Show comment
Hide comment
@j03w

j03w Oct 29, 2013

Contributor

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.

Contributor

j03w commented Oct 29, 2013

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.

@jashkenas jashkenas closed this Oct 29, 2013

@tsadoklf

This comment has been minimized.

Show comment
Hide comment
@tsadoklf

tsadoklf Oct 29, 2013

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 commented Oct 29, 2013

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.

@jashkenas

This comment has been minimized.

Show comment
Hide comment
@jashkenas

jashkenas Oct 29, 2013

Owner

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...

Owner

jashkenas commented Oct 29, 2013

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...

@tsadoklf

This comment has been minimized.

Show comment
Hide comment
@tsadoklf

tsadoklf Oct 29, 2013

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 commented Oct 29, 2013

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?

@jashkenas

This comment has been minimized.

Show comment
Hide comment
@jashkenas

jashkenas Oct 29, 2013

Owner

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. ;)

Owner

jashkenas commented Oct 29, 2013

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. ;)

@lukesargeant

This comment has been minimized.

Show comment
Hide comment
@lukesargeant

lukesargeant Oct 30, 2013

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

lukesargeant commented Oct 30, 2013

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment