2

I have a simple Backbone.js model:

class Avia.Student extends Backbone.Model

Told you it was simple :-) Anyhow, I'm saving it in the following view (snipped for clarity):

class Avia.StudentView extends Backbone.View

  render: =>
    html = JST['views/student_view_template'](model: @model)
    @el.html(html)
    Backbone.ModelBinding.bind(@)
    $('#save').bind('click', @save)

  save: (e) =>
    e.preventDefault()
    @model.save(
        success: =>,
        error: =>
    )

When I click the save button, @save() is called, but fails with the following error (again, snipped for clarity as obviously it continues for a long time):

Uncaught RangeError: Maximum call stack size exceeded
Backbone.Events.trigger:117
_.extend._onModelEvent:635
Backbone.Events.trigger:117
_.extend._onModelEvent:635
Backbone.Events.trigger:117
_.extend._onModelEvent:635
Backbone.Events.trigger:117

Could someone please tell me what I'm doing wrong? I don't understand why this is happening ...

4
  • Yea, try commenting out the Backbone.ModelBinding.bind(@) line and see what happens Commented Dec 17, 2011 at 12:51
  • Is #save an element within @el? Are you sure? Because if it's elsewhere, then you're binding another click event to it on each render... Commented Dec 17, 2011 at 17:18
  • Thanks - I've commented out Backbone.ModelBinding.bind(@) and ensured #save is unique by renaming it to #wooble and renaming the associated bindings. I'm still encountering the same problem :-/ Commented Dec 17, 2011 at 23:26
  • I've also tried using the very latest backbone.js from GitHub, and the problem persists. Commented Dec 17, 2011 at 23:29

2 Answers 2

2

You need to call super() in your collection's and/or model's constructor (if you have defined any). I had the same issue a couple of days ago: Omitting calling super() fails to bind the _onModelEvent object to the collection, which causes it to be invoked in an incorrect context (this is pointing to the model instead of the collection [and vice versa]).

Sign up to request clarification or add additional context in comments.

Comments

0

You need to bind the save function to the view.

You do this by calling _.bindAll in the view's initialize method

class Avia.StudentView extends Backbone.View

  initialize: =>
    _.bindAll(@, 'save');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.