Fix server communication and handling in BaseResource.
Review Request #3833 — Created Feb. 3, 2013 and submitted
Fix server communication and handling in BaseResource. BaseResource now sends POST/PUT data to the server using encoded form data instead of Backbone.js's default JSON encoding. It goes through RB.apiCall for now, so that all our behavior (such as the "Loading..." indicator) works as expected. It also parses the error response to give us an appropriate error message from the server. Some fixes were made to save(). Previously, it would fail to create new objects, because it didn't have a url at the right time. This needed to happen after the object was ready and its parent resource, if any, was known to be created. parse() now ensures that rspNamespace is filled out, so we don't have weird 'undefined' errors when a subclass isn't written correctly. And finally, save(), fetch(), destroy(), etc. take a context parameter for the callbacks. This is something Backbone.js should really be providing, since it establishes this pattern with other callback-reated functions, but it doesn't. We now wrap the callbacks when calling the parent functions.
Unit tests pass. These changes came about from some of the first real-world usage with my upcoming comment change, which exercised each of these code paths.
- Change Summary:
-
The previous version wasn't actually using RB.apiCall, since Backbone.ajax was only in newer versions. I have a change to just switch to the new version. Some of the changes in here help with that switch. * Changed the Backbone.ajax assignment to use a new function that thunks to RB.apiCall, allowing us to test that RB.apiCall is actually getting called. * Added tests to ensure RB.apiCall is being called. * Added tests for ensuring the "saved" event was being triggered. * Added the ability to disable the AJAX indicator and the API call queueing, so that unit tests don't have to deal with them.