angular-restmod icon indicating copy to clipboard operation
angular-restmod copied to clipboard

Allow query string args to be passed to $destroy / $save?

Open scriby opened this issue 10 years ago • 2 comments

We have a need to pass query string params to a DELETE call via $destroy. Would you consider allowing them to be passed in similar to $fetch?

The API we're calling also has a need to pass query string args to a POST via $save. I know it's strange to include a query string for POST calls, but I also don't think it's disallowed in HTTP. So, it would be helpful if we had the ability to pass query string args to $save as well.

Thanks for considering this!

Chris

scriby avatar Feb 05 '15 15:02 scriby

Hi @scriby,

I'll add this for consideration for the 1.3 milestone, I'm not sure on the api though, specially for $save, since it already take other parameters.

Have you considered using the $decorate method? You could do this:

model.mix({
  $extend: {
    'Record.withParams': function(_params, _fun) {

      // create a decorator that hooks to the before-request event and adds some query parameters.
      var decorator = {
        'before-request': function(req) {
          req.params = _params;
        }
      }, decorated = this;

      // return proxy object that exposes decorated versions of common  operations.
      return {
        $save: function() {
          return decorated.$decorate(decorator, function() { return this.$save() );
        },
        $destroy: function() {
          return decorated.$decorate(decorator, function() { return this.$destroy() );
        }
      };
    }
  }
});

And then:

someRecord.withParams({ some: 123 }).$save();

iobaixas avatar Feb 05 '15 23:02 iobaixas

I'm not sure on the api though, specially for $save, since it already take other parameters

it could be something like this:

{
    $save: function(_patch, _params) {
         /** ... some code */

        _params = _params || {};

        if(angular.isObject(_patch)) {
            _params = _patch;
            _patch = null;
        }

        if(_patch) {
            request = {
              method: this.$type.getProperty('patchMethod', 'PATCH'),
              url: url,
              data: this.$wrap(/** Special patch wrapping */) 
            };
          } else {
            request = { method: 'PUT', url: url, data: this.$wrap(Utils.UPDATE_MASK) };
          }

          request.params = _params;

         /** ... some code */
    }
}

smashercosmo avatar Feb 11 '15 09:02 smashercosmo