Class: DefaultPacker

DefaultPacker

new DefaultPacker()

Simple $unpack implementation that attempts to cover the standard proposed by [active_model_serializers](https://github.com/rails-api/active_model_serializers.

This is a simplified version of the wrapping structure recommented by the jsonapi.org standard, it supports side loaded associated resources (via supporting relations) and metadata extraction.

To activate add mixin to model chain

restmodProvide.rebase('DefaultPacker');

Json root

By default the mixin will use the singular model name as json root for single resource requests and pluralized name for collection requests. Make sure the model name is correctly set.

To override the name used by the mixin set the jsonRootSingle and jsonRootMany variables. Or set jsonRoot to override both.

Side loaded resources

By default the mixin will look for links to other resources in the 'linked' root property, you can change this by setting the jsonLinks variable. To use the root element as link source use jsonLinks: '.'. You can also explicitly select which properties to consider links using an array of property names. To skip links processing altogether, set it to false.

Links are expected to use the pluralized version of the name for the referenced model. For example, given the following response:

{
  bikes: [...],
  links {
    parts: [...]
  }
}

Restmod will expect that the Part model plural name is correctly set parts. Only properties declared as reference relations (belongsTo and belongsToMany) will be correctly resolved.

Metadata

By default metadata is only captured if it comes in the 'meta' root property. Metadata is then stored in the $meta property of the resource being unwrapped.

Just like links, to change the metadata source property set the jsonMeta property to the desired name, set it to '.' to capture the entire raw response or set it to false to skip metadata and set it to an array of properties to be extract selected properties.

Properties:
Name Type Description
single mixed

The expected single resource wrapper property name

plural object

The expected collection wrapper property name

links mixed

The links repository property name

meta object

The metadata repository property name

Source:
  • module/support/default-packer.js, line 32