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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With