Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

backbone.js fetch changed event not firing after a successful fetch

Tags:

backbone.js

I have the following backbone view:

class Observation extends Backbone.Model

class Observations extends Backbone.Collection
  model: Observation

  constructor: ->
    @url = _observationsUrl

class ObservationsView extends Backbone.View
  el: $('#observations')

  initialize: ->
    _.bindAll @    
    @model.bind 'changed', @render
    @model.view = @
    that = @
    @model.fetch {
      success: ->
        alert('success')
        that.model.trigger 'changed'
    }

    render: =>
      alert('rendering baby')

class ObservationsController extends Backbone.Controller
  initialize: ->
    observations = new Observations()
    observationsView = new ObservationsView(model: observations)   

I am binding the model's changed event to the render method of the ObservationsView. The model is a backbone collection.

The fetch is working successfully but the changed event is not being fired. I am trying the manual trigger out of desperation.

Can anyone see what I am doing wrong?

like image 857
dagda1 Avatar asked Jun 30 '11 13:06

dagda1


1 Answers

The event isn't called 'changed'. The event triggered after the model's collection has been refreshed from the server is 'refresh'.

The 'change' event is actually more complicated. It's an event on a model that's triggered whenever you call .set(), and it always includes the attribute, so you'd write things like:

this.model.bind('change:username', _.bind(this.update_username_display, this))

As always, the backbone.js source code is eminently readable.

like image 50
Elf Sternberg Avatar answered Oct 05 '22 13:10

Elf Sternberg