Drew Dara-Abrams
Drew Dara-Abrams

Reputation: 8054

Backbone.js TypeError thrown on model.save

In my current Backbone app, I have no problems creating or destroying models. The changes propagate to my server without any problems. But whenever I try to do a model.save(attributes), I see a TypeError thrown at this line: https://github.com/documentcloud/backbone/blob/master/backbone.js#L117

The model is updated on the (Rails) server, but something breaks afterward. Here's the stack trace. (Sorry, line numbers refer to concatenated JS files.)

Backbone.Events.trigger() at application.js:11574 
_.extend._onModelEvent() at application.js:12092 
d() at (internal script):1426Backbone.Events.trigger() at 
application.js:11574 
_.extend.change() at application.js:11808 
_.extend.set() at application.js:11680 
_.extend.save() at application.js:11753 
Map.mapMoveGeoPointMode() at app.js:741 
(anonymous function)() at app.js:894 
jQuery.event.handle() at application.js:2966 
jQuery.event.add.elemData.handle.eventHandle() at application.js:2600 

In previous applications, I've had no little problems on saving models like this. Any suggestions for what to look at?

Note: I don't think I'm repeating this fellow's problem of trying to save a model with no URL.

Upvotes: 0

Views: 229

Answers (2)

Skylar Anderson
Skylar Anderson

Reputation: 5703

It looks like you are binding to an event on the model and passing in something that isn't a valid function. Make sure all of your bind statements pass in functions:

var FUNC = function() {};
MyModel.bind('reset' FUNC);

Upvotes: 1

nrabinowitz
nrabinowitz

Reputation: 55688

Given that the model is updated properly on the server, I don't think this has anything to do with the save() function itself, but rather with the change event it's triggering - I think you could test this simply by calling myModel.trigger('change') and seeing if that throws the same error.

My best guess as to why this is happening is that somewhere along the way you're binding something other than a function - e.g. an undefined variable - to the change event. I'd look for an error like this:

myModel.bind('change', this.misspelledMethod, this);

but it's pretty much impossible to debug further without seeing actual code, not just a stack trace.

Upvotes: 3

Related Questions