[wp-trac] [WordPress Trac] #30896: Backbone Views should use `listenTo` when listening to models

WordPress Trac noreply at wordpress.org
Sun Jan 4 05:22:29 UTC 2015


#30896: Backbone Views should use `listenTo` when listening to models
----------------------------+-----------------
 Reporter:  wonderboymusic  |      Owner:
     Type:  defect (bug)    |     Status:  new
 Priority:  normal          |  Milestone:  4.2
Component:  Media           |    Version:
 Severity:  normal          |   Keywords:
  Focuses:  javascript      |
----------------------------+-----------------
 I just read 2 books about Backbone. Chapter 1 of both says to never do
 this inside of a view:

 `this.model.on( 'whatever', this.boom, this )`

 You should do:

 `this.listenTo( this.model, 'whatever', this.boom )`

 Why you ask? The listeners won't be properly removed when the view is
 destroyed (creating Ghost Views), causing problems with JS garbage
 collection because the view is still shackled to the model. Using
 `listenTo` solves that problem.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/30896>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list