GithubHelp home page GithubHelp logo

Comments (5)

SeyZ avatar SeyZ commented on May 10, 2024 2

Hey @allistercsmith @bradbumbalough @mrkvon @sazzer

I've added the possibility to set a function for the meta key:
https://github.com/SeyZ/jsonapi-serializer/blob/master/test/serializer.js#L198

If you need a dynamic meta for a relationship, it was already implemented:
https://github.com/SeyZ/jsonapi-serializer/blob/master/test/serializer.js#L1549

I think it's fixed this issue, right? @bradbumbalough Hope you will come back here 👍 💃

from jsonapi-serializer.

SeyZ avatar SeyZ commented on May 10, 2024

Hey @allistercsmith ! Thanks for your message, I appreciate!

Good idea for the dynamic meta. You're totally right, it's not good to recreate a serializer each time the meta change.

I like the idea to pass an opts to the serialize method too. You should use the lodash merge method to merge the objects opts.meta and that.opts.meta :)

PR is of course welcomed!

from jsonapi-serializer.

sazzer avatar sazzer commented on May 10, 2024

Once we're passing options in to the serialize function, it could be useful to make them available to all of the various functions that are declared as part of the schemas as well. That way, we can - for example - make the request details available inside the functions that are used to generate URLs, instead of having to solely depend on the record data or else re-creating the schema every time.

from jsonapi-serializer.

mrkvon avatar mrkvon commented on May 10, 2024

The first (and possibly sufficient) step, as suggested in #67 could be: Allow the meta in Serializer's opts to be a function (or object of functions if one wishes to be more strict).

// meta as a function
// fully dynamic meta
var UserSerializer = new JSONAPISerializer('users', {
  attributes: ['firstName', 'lastName'],
  meta: (data) => { // or meta: (data) => data.meta
    foo: 'bar',
    baz: data.baz
  }
});

// meta as an object of functions (optionally)
// which is more strict
// and similar to topLevelLinks and other ...Links options
var UserSerializer = new JSONAPISerializer('users', {
  attributes: ['firstName', 'lastName'],
  meta: {
    foo: 'bar', // static meta property
    baz: (data) => data.baz // dynamic meta property
  }
});

var users = UserSerializer.serialize(data);

This would be the most intuitive solution imho.

The implementation of 2nd option may be identical to the way topLevelLinks are done:

// lib/serializer.js
    function getMeta(meta) {
      return _.mapValues(meta, function (value) {
        if (_.isFunction(value)) {
          return value(records);
        } else {
          return value;
        }
      });
    }
...
    if (that.opts.meta) {
      payload.meta = getMeta(that.opts.meta);
    }

from jsonapi-serializer.

bradbyte avatar bradbyte commented on May 10, 2024

This was a requirement for my use case. Ended up switching to https://github.com/kutlerskaggs/json-api-ify as they provide a somewhat dynamic meta option.

from jsonapi-serializer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.