Backbone.js issues GET requests for calls to `model.save ()`

I have a Backbone application in which I create a new model and save it. Here's a snippet of code with debugging in CoffeeScript:

newListing = new Listing console.log "New?", newListing.isNew() newListing.save creation, { wait: true success: (model, response) => console.log "SAVED", model console.log "RESPONSE", response } 

For debugging, I also redefined Backbone.sync :

 oldSync = Backbone.sync Backbone.sync = (method, model) -> console.log "Syncing:", method, model oldSync(arguments...) 

This usually works fine. I get this in the console:

 New? true Syncing: create > Listing SAVED > Listing RESPONSE > Object 

And in Network Inspector, I see:

 listings POST 200 application/json 

I also see a POST request registered in my application log (Rails).

However, after creating several listings, I begin to see the following behavior on the console:

 New? true Syncing: create > Listing SAVED > Listing RESPONSE [> Object, > Object, > Object, > Object, > Object, > Object] 

where each Object is a listing already stored in the database. The network inspector and my application log also indicate that Backbone has fulfilled the GET request in / listings. In addition, Listing in the third line, which was “SAVED”, is a client-side view of the list (without any additional information that is usually inserted by the server).

I could not find any pattern for this behavior; sometimes, Backbone insists on sending a GET after the GET, and after the update it starts to work. Sometimes it fires until I restart the application server. Nice to explore any offers!

[change]

Well, after some tracking, it appears that after calling fetch() in the Listings collection, Listing#save() starts to do this. The problem only appears on my laptop running Chrome Dev (v19). In other browsers and older versions of Chrome, it works fine.

+4
source share
1 answer

Obviously, this is a problem associated with a specific application. I assume the problem is server related. Perhaps your server code returns all listings after creation, and not just the one that was created. Upon successful creation, your server should respond with 200 OK and the representation of the created object in the response body.

0
source

Source: https://habr.com/ru/post/1402030/


All Articles