How-to access widget data in helper

后端 未结 1 1176
执笔经年
执笔经年 2021-01-07 07:30

in my frontend helper I use a parameter and give it everywhere data.widget.

Is it possible to access data.widget in the helper context? I\'ve only found the self.out

1条回答
  •  孤城傲影
    2021-01-07 07:51

    Passing a piece/widget/page to a helper at the template level is pretty common practice, just like your illustrating.

    If your issue is that you always need some special data/decision/format to happen based on a widget, you can override the widget's load method and attach that special something to your widget and then access it in your template.

    widget's index.js

    module.exports = {
        extend: 'apostrophe-widgets',
        ...
        construct: function(self, options) {
            const superLoad = self.load;
            self.load = function (req, widgets, callback) {
                return superLoad(req, widgets, function (err) {
                    if (err) {
                        return callback(err);
                    }
                    // `widgets` is each widget of this type being loaded on a page
                    widgets.forEach(function (widget) {
                        // do something cool, attach it to widget
                        widget.somethingCool = 'hello world';
                    });
                    return callback(null);
                });
            };
        }
    }
    

    You would then be able to access data.widget.somethingCool in your template.

    0 讨论(0)
提交回复
热议问题