Make WordPress Core

Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#30896 closed defect (bug) (fixed)

Backbone Views should use `listenTo` when listening to models

Reported by: wonderboymusic's profile wonderboymusic Owned by: wonderboymusic's profile wonderboymusic
Milestone: 4.2 Priority: normal
Severity: normal Version:
Component: Media Keywords:
Focuses: javascript Cc:

Description

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.

Change History (4)

#1 @wonderboymusic
9 years ago

  • Owner set to wonderboymusic
  • Resolution set to fixed
  • Status changed from new to closed

In 31045:

In Media Views, use this.listenTo( this.model, .... ) instead of this.model.on( .... ) to fix garbage collection and to avoid "ghost views."

Fixes #30896.

#2 @nacin
9 years ago

Which books?

#3 @wonderboymusic
9 years ago

Addy Osmani: "Developing Backbone.js Applications"
Patrick Mulder: "Full Stack Web Development with Backbone.js"

Note: See TracTickets for help on using tickets.