Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extendable context references in config files #1055

Open
mkoe-unitb opened this issue Jul 1, 2021 · 2 comments
Open

Extendable context references in config files #1055

mkoe-unitb opened this issue Jul 1, 2021 · 2 comments

Comments

@mkoe-unitb
Copy link

Normal config files with context references looks like:

module.exports = {
    title: 'teaser-list',
    context: {
        items: [
            '@molecules-teaser',
            '@molecules-teaser',
            '@molecules-teaser',
        ]
    }
}

The Problem I have in multiple projects is, that I couldn't extend the referenced object.
This wouldn't work:
const extendedTeaserContext = Object.assign('@molecules-teaser', { newVariable: 'lorem ipsum' })

I have two ideas to solve this problem.

Possible Solution 1: getContext Function
A function to convert a context reference into the object.
const extendedTeaserContext = Object.assign(getContext('@molecules-teaser'), { newVariable: 'lorem ipsum' })

Possible Solution 2: Post Context Generation Hook
A function to adjust the context after all references are resolved.

module.exports = {
    title: 'teaser-list',
    context: {
        items: [
            '@molecules-teaser',
            '@molecules-teaser',
            '@molecules-teaser',
        ]
    },
    postReferenceResolvedHook(context) {
        const extendedItems = [];

        for (const item of context.items) {
            item.newVariable = 'lorem ipsum';
            delete item.globalContextVariable;
            extendedItems.push(item)
        }
        
        context.items = extendedItems;
        return context;
    }
}
@mihkeleidast
Copy link
Member

I like the idea of proposed solution 1 more, because it'll open up more possibilities with using different context helpers in the future (for example, adding a render context helper function instead of the current @@handle syntax). The second solution seems too manual, especially if the context object is large (and it may be) and has multiple instances of the objects in different places.

@RickMeijer
Copy link

I was just about to file an issue/help regarding this, example repository and all! Guess it's not necessary. For us this feature would be great, since we try to follow the atomic design principles, and try not to copy the properties belonging to the atoms all over the codebase. Of course there's the render helper with merge=true, but this isn't much use when using @partial-blocks.

How about fractal.components.find() returning a promise? You could then use the .context property on the found item, instead of it returning undefined. After that you can handle the context however way you wish.

If that's impossible (like doing the .find after a load(), which is never triggered since the context would never be resolved), maybe add a hook to each loaded component? Something like fractal.components.on("load:componentName") would help greatly.

Something like this below works, but is hackneyed at best, and malicious at worst. You could spend days figuring out why on some machines it compiles correctly, while on others it's not.

function findPromise(componentName, waitForIt=100) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      const atom = fractal.components.find(componentName);
      if (!atom) reject(new Error("No atom found"));
      fractal.components.resolve(atom.context).then(resolve);
    }, waitForIt); // Random number depending on computing power
  });
}

module.exports = {
  context: {
    item: findPromise("@atom--list").then(context => ({
      ...context,
      items: [ 'fi', 'fie', 'fo']
  })
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants