decorateWidget does not return vDom element

AI-generated summary

The discussion revolves around an issue with api.decorateWidget not returning a virtual-dom element when used within a function. stevenpslade encountered this problem while trying to access topic views from a widget or plugin initializer. They provided an example where a direct return statement works, but a return statement within a nested function does not.

Mittineague suggested that the issue might be due to scope and provided a revised version of the code that returns the result of the nested function.

stevenpslade then shared their actual code, which involves making a JSON request to retrieve topic views. They noted that while the views are successfully passed to the nested function, the virtual-dom element is not attaching as expected.

Continuing the discussion from Accessing topic.views from widget or plugin initializer:

I was encountering issues, as per the linked topic and I have boiled it down to this:

I cannot seem to return a virtual-dom element to api.decorateWidget from within a function.

As an example, this works:

api.decorateWidget('post-meta-data:after', dec => {
  return h('i.fa.fa-eye')
});

But this do not work:

api.decorateWidget('post-meta-data:after', dec => {
  function doThis() {      
    return h('i.fa.fa-eye')
  }
  doThis();
});

Can’t figure out why?

Scope?

maybe

api.decorateWidget('post-meta-data:after', dec => {
  function doThis() {      
    return h('i.fa.fa-eye')
  };
  return doThis();
});

Oh wait ya I just realized that too. I should just use my actual code instead of trying to simplify my problem.

I am doing this:

api.decorateWidget('post-meta-data:after', dec => {
  const attrs = dec.attrs;
  function passViews(views) {
    //views is successfully passed to this function from getJSON
    console.log(views);
    //the below vDom test element does NOT attach as expected
    return h('i.fa.fa-eye'));
  }
  $.getJSON(attrs.topicUrl + '.json', function (data) {
    //passing function to success because ultimately I want to
    // get the topic views
    success: passViews(data.views);
  });
});