Pass cardOptions to custom renderers#35
Conversation
Passes `cardOptions` as the final argument to `markupElementRenderer` and `sectionElementRenderer`.
|
What is the advantage of this over this #34? |
|
@gdub22 this is mostly at my nudging, though @bantic and I talked it over. There are a couple things at play. Using the context of a function as an input to that function feels incorrect. For example a naive developer could easily write: new MobiledocDOMRenderer({
sectionElementRenderer: {
p() {
let tagName = this.someLogicToGetTagName();
return document.createElement(tagName);
}
}
});What is new MobiledocDOMRenderer({
sectionElementRenderer: {
p(tagName, dom, {renderer}) {
let tagName = renderer.someLogicToGetTagName();
return document.createElement(tagName);
}
}
});Now you know exactly what object is providing new MobiledocDOMRenderer({
sectionElementRenderer: {
p: (tagName, dom, {renderer}) => document.createElement(renderer.someLogicToGetTagName())
}
});And the change from a Passing the renderer wholesale feels incorrect. We're concerned that passing the renderer (as Instead we'd like to continue opting in to features as needed. For example in #34 the canonical use-case is access to const host = 'http://google.com'
new MobiledocDOMRenderer({
cards: [someCard, anotherCard],
cardOptions: {host},
markupElementRenderer: {
A(tagName, dom, attrs) {
let element = document.createElement('a');
element.setAttribute('href', [host, attrs.href].join('/'));
return element;
}
});This seems seems almost more reasonable than passing But, that said passing markupElementRenderer: {
A: (tagName, attrs, {cardOptions, dom}) => elementForA
}That would avoid the constant shifting of arguments. We still have some play to change things being pre-1.0, and it seems ok to iterate as we learn instead of using |
|
Great, thanks for the explanation. Typically, just closing over some options works fine. The need to access the options outside of that scope arose when I wanted to reuse the same customized dom-renderer with different 'host' options for example. Also note that I still need to access the whole renderer instance from within cards. For example, in a listicle-card, you'd want to render the body contents with the parent renderer's options. I currently hack around that by closing over and creating a reference to the renderer itself as a card option. This LGTM, my main concern was the api for these renderers is becoming argument soup and a potential compatibility nightmare. One hash argument is the way to go for flexibility and self documentation. |
Passes
cardOptionsas the final argument tomarkupElementRendererand
sectionElementRenderer.