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

Disable nesting of child relations under parent?

Open jasonayre opened this issue 9 years ago • 3 comments

Hi,

Was wondering if there is a clean way to disable parent auto naming of relation urls. I.e.

CurrentOrganization model, hasMany EmailConfig

(Also CurrentOrganization is a singleton model).

Right now if I fetch or save a email_config relation from the current_organization, it will hit '/current_organization/email_configs'. Because the email config objects are nested in the current organization controller response, but ideally I'd prefer to load them through the current_organization, and when I create new ones current_organization.email_configs.$build({}).$save(), it would hit '/email_configs' instead of '/current_organization/email_configs' (im not hitting that endpoint for get, just create/put).

I haven't been able to figure out a way to do that atm, not sure if its supported or if this is a feature request.

jasonayre avatar Jun 09 '15 20:06 jasonayre

It looks like it may be possible if you use the model definition function method of defining your model rather than the standard definition object method.

You'll also need the attrAsCollection (hasMany) API

Try something like this:

restmod.model('CurrentOrganization', function() {
  this.attrDefault('name', 'My Organization')
    .attrAsCollection('hasManyRelation', 'EmailConfig', '/email_configs')
});

jczerwinski avatar Jun 18 '15 08:06 jczerwinski

@jczerwinski Using the definition function hits same endpoint, as far as I can tell. Although the api is a bit cryptic (mainly due to the position dependent arguments and the particularly long list of params being passed to it) -- so it's possible I'm missing something, though I don't see anything about skipping the parent relations route in docs. Here is the actual code for example/better illustration, using the definition function. (which still hits /current_organization/email_configs)

angular.module('myapp').factory("CurrentOrganization", ["restmod", function(restmod) {
  var model = restmod.singleton(
    '/current_organization',
    'BaseModel',
    'NestedDirtyModel',
    function(){
      this.attrAsCollection('email_configs', 'EmailConfig', '/email_configs');
    }
  );

  return model;
}]);

jasonayre avatar Jun 19 '15 18:06 jasonayre

How about this?

var email_configs = $restmod.model().$mix({
    $config {
        hasMany: {
            hooks: {
                'after-has-many-init': function() {
                    this.$scope.$urlFor = function (_resource) {
                      $urlFor: function(_resource) {
                        return this.$target.$urlFor(_resource);
                    }
                }
            }
        }
    }
})

It's documented in the hooks guide, but not with a concrete how-to.

jczerwinski avatar Jul 07 '15 21:07 jczerwinski