Created
December 30, 2013 19:30
-
-
Save djavier/8186812 to your computer and use it in GitHub Desktop.
Extends AngularJS $save method to use Put and Post for Updates and Creates respectively.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var module = angular.module( 'my.resource', [ 'ngResource' ] ); | |
module.factory( 'Resource', [ '$resource', function( $resource ) { | |
return function( url, params, methods ) { | |
var defaults = { | |
update: { method: 'put', isArray: false }, | |
create: { method: 'post' } | |
}; | |
methods = angular.extend( defaults, methods ); | |
var resource = $resource( url, params, methods ); | |
resource.prototype.$save = function() { | |
if ( !this.id ) { | |
return this.$create(); | |
} | |
else { | |
return this.$update(); | |
} | |
}; | |
return resource; | |
}; | |
}]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks!