Getting model.id from backbone.js collection creat

2019-07-30 15:36发布

问题:

I used to be able to get my model.id from a newly created backbone collection.create call via the following example code:

OPTS['success'] = function( response, model, options ){
                model.id
}

SOMECOLLECTION.create(json_attributes,OPTS)

This no longer works in v1.1.2. Anyone know what is going on here? It now returns an array of objects. I am using Django-Tastypie to create my RESTful urls.

回答1:

I actually found the problem today, it was I was using namespaces with the tastypie urls:

urlpatterns += patterns('', (r'^api/', include(v1_api.urls)))

was changed to this

urlpatterns += patterns('', (r'^api/', include(v1_api.urls),namespace='api'))

for unit testing purposes, but this created an issue with reverse calls in the tastypie framework itself, and in the end it changed the response.location header to not include the url to the actual model itself after the create (POST) call was made. Remove the namespace fixed this problem