0

I am working on developing a Backbone + require app. Things are working somewhat, but after updating a model on the server, though the server is returning a 200, the 'error' function in the options hash passed to the model's 'save' is being called.

I think I have identified the problem in that the server returns a JSON object containing 'id', whereas the model has an id attribute labeled 'aid'.

My understanding is that this should be handled in the model's 'parse' function, but I cannot get either the model's ''parse' function to be called. Here is my model:

define([
// These are path alias that we configured in our bootstrap
'jquery',     // lib/jquery
'underscore', // lib/underscore
'backbone',    // lib/backbone
'util'
], function($, _, Backbone){
    // Above we have passed in jQuery, Underscore and Backbone
    // They will not be accessible in the global scope

    var Address = Backbone.Model.extend({
        initialize: function() { console.log("Address initialized"); },
        urlRoot: '/address/',
        parse: function(response, options) {
            console.log("In Address::parse");
            for(thing in response) { 
                console.log("Key:" + thing + ", Val: " + response[thing]); 
            }
        }
     });

     return {
         address: Address
     };
});

and here is the relevant part of my view:

events: {
"submit #add-address-form": "addAddress",
},

addAddress: function(ev) {
var that = this;
ev.preventDefault();
var addressDetails = $(ev.currentTarget).serializeObject();
var addr = new A.address();
addr.save(addressDetails, {
    success: function(model, response, options) {
        that.Backbone.application.router.navigate('', {trigger: true});
    },
    error: function(model, response, options) {
        console.log("Response status: " + response.statusCode());
    }
});
return false;
},

When the form presented by the view is submitted 'addAddress' is triggered and the server is updated. My app receives a 200 from the server, and the JSON object '{id: }', but the parse function in the model is never called.

Any help appreciated;

garey
  • 67
  • 1
  • 9

1 Answers1

1

You have to return a value in your parse function :

parse: function(response, options) {
    console.log("In Address::parse");
    for(thing in response) { 
        console.log("Key:" + thing + ", Val: " + response[thing]); 
    }
    return response;
}
Rida BENHAMMANE
  • 4,111
  • 1
  • 14
  • 25
  • Okay, I added 'return response;' to parse and it didn't change anything. I can see backbone.js triggering a sync, and the response from the URL is 200, but save's error option is still being called. Here is the line from the console: PUT http://... 200 OK -21ms – garey Mar 10 '14 at 21:34
  • Have you tried to add `idAttribute: 'aid'` to your Model ? – Rida BENHAMMANE Mar 10 '14 at 21:55
  • By changing the key name generated by my input form from aid to id, I got backbone to generate a POST rather than a PUT. The POST works (I can see the model in my database), and returns a JSON object {aid: XX } where XX is a synthetic key, but error is still called. What could cause that? – garey Mar 10 '14 at 23:58