Make WordPress Core

Opened 12 years ago

Closed 12 years ago

Last modified 12 years ago

#30896 closed defect (bug) (fixed)

Backbone Views should use `listenTo` when listening to models

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

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
12 years ago

  • Owner set to wonderboymusic
  • Resolutionfixed
  • Status newclosed

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
12 years ago

Which books?

#3 @wonderboymusic
12 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.