Remove the "create" funtion from the Resource JS class

The original thought was that the actions you can do on a resource is:
- create it
- read it
- update it

(I guess you should have "delete" as well, but that isn't currently there)

Although we use "read" and "update", we never use "create". So having it goes against the YAGNI principle :)

Also, it had a bug: when sending a "create", the opt_newId in send_() would
permanentily change the uri of the request, causing any subsequent request to
go to the wrong endpoint.

By removing the "create" we can simplify the rest of the code (the send_() function).

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=213029499
This commit is contained in:
guyben 2018-09-14 13:01:11 -07:00 committed by jianglai
parent 8ddbf88151
commit cdcd5c2a0e

View file

@ -39,8 +39,8 @@ goog.inherits(registry.Resource, registry.Session);
/**
* Get the resource from the server.
*
* @param {!Object} args Params for server. Do not set the 'op' field on args.
* @param {!Function} callback For retrieved resource.
* @param {!Object} args Params for server.
* @param {!Function} callback for retrieved resource.
*/
registry.Resource.prototype.read = function(args, callback) {
this.send_('read', args, callback);
@ -48,22 +48,9 @@ registry.Resource.prototype.read = function(args, callback) {
/**
* Create the resource on the server.
* Update the resource on the server.
*
* @param {!Object} args params for server. Do not set the 'op' field on args.
* @param {!Function} callback on success.
* @param {string} newId name for the new resource.
* @throws {!Exception} if the 'op' field is set on args.
*/
registry.Resource.prototype.create = function(args, callback, newId) {
this.send_('create', args, callback, newId);
};
/**
* Create the resource on the server.
*
* @param {!Object} args params for server. Do not set the 'op' field on args.
* @param {!Object} args params for server.
* @param {!Function} callback on success.
* @throws {!Exception} if the 'op' field is set on args.
*/
@ -78,17 +65,13 @@ registry.Resource.prototype.update = function(args, callback) {
* @param {string} opCode One of (create|read|update)
* @param {!Object} argsObj arguments for the operation.
* @param {!Function} callback For XhrIo result throws.
* @param {string=} opt_newId name for the new resource.
* @private
*/
registry.Resource.prototype.send_ =
function(opCode, argsObj, callback, opt_newId) {
function(opCode, argsObj, callback) {
// NB: must be declared this way in order to avoid compiler renaming
var req = {};
req['op'] = opCode;
req['args'] = argsObj;
if (opt_newId) {
this.uri.setPath(this.uri.getPath() + '/' + opt_newId);
}
this.sendXhrIo(goog.json.serialize(req), callback);
};