# Are there any examples in Discourse of decorating a widget after an ajax request?

**URL:** https://meta.discourse.org/t/are-there-any-examples-in-discourse-of-decorating-a-widget-after-an-ajax-request/44175
**Category:** Development
**Created:** [12 مايو 2016، 9:57م UTC](https://meta.discourse.org/t/are-there-any-examples-in-discourse-of-decorating-a-widget-after-an-ajax-request/44175 "2016-05-12T21:57:31Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![stevenpslade](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/stevenpslade/32/53550_2.png) [@stevenpslade](https://meta.discourse.org/u/stevenpslade)
#### Post date: [12 مايو 2016، 9:57م UTC](https://meta.discourse.org/t/are-there-any-examples-in-discourse-of-decorating-a-widget-after-an-ajax-request/44175/1 "2016-05-12T21:57:31Z")

</div>

I would like to use `api.decorateWidget` after an `Discourse.ajax` has run.

```
api.decorateWidget('post-contents:before', dec => {
              const attrs = dec.attrs;
              const result = [];

              Discourse.ajax('/test', {
                type: 'GET',
                data: { topic_id: topic_id, group: group }
              }).then(function(res){
                if (res.test) {
                 //this push a virtual node into the result array
                  result.push(h('h1', 'test'));
                //this logs the virtual node and confirm virtual-dom works here
                  console.log(result);
              //expected result here is that api.decorateWidget gets the returned h1 virtual node and attached is, BUT it does nothing with no errors.
                  return result;
                }
             });

           });

```

#EDIT: AFTER RUNNING SOME TESTS  
So what all the code boils down to is…

1. The `decorateWidget` function
2. An `ajax` call inside the `decorateWidget`
3. An if statement in the result of the `ajax` call that returns a vDOM element

I put a `console.log` at each of these steps and got this (note: there are 2 posts on the thread I am testing on, which is why the widget decorator and ajax call runs twice):  
 ![](https://global.discourse-cdn.com/meta/original/3X/c/f/cf80039af9f5d6f837d41cf83f933a43617555d1.png)

Also, I am still not getting the vDOM element to return after the if statement.

---

<div class="post-metadata">

### Author: ![Overgrow](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/overgrow/32/478189_2.png) [@Overgrow](https://meta.discourse.org/u/Overgrow)
#### Post date: [26 نوفمبر 2016، 2:15م UTC](https://meta.discourse.org/t/are-there-any-examples-in-discourse-of-decorating-a-widget-after-an-ajax-request/44175/2 "2016-11-26T14:15:56Z")

</div>

I’d like to follow up on this thread. Is it possible to return results of ajax.then() to html function so it will display in widget rendered content?

The problem is that value is returned without waiting for ajax result.

```
export default createWidget('custom-widget', {
  html(attrs, state) {
    ajax("/customdata.json").then(data => {
      // some data formatting to html
      return `THIS WONT BE SHOWN`;
    });
    return `THIS IS SHOWN`;
  }
});

```

---

<div class="post-metadata">

### Author: ![Overgrow](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/overgrow/32/478189_2.png) [@Overgrow](https://meta.discourse.org/u/Overgrow)
#### Post date: [26 نوفمبر 2016، 5:08م UTC](https://meta.discourse.org/t/are-there-any-examples-in-discourse-of-decorating-a-widget-after-an-ajax-request/44175/3 "2016-11-26T17:08:30Z")

</div>

The only solution I’ve found so far is to switch to Component and use bufferedRender and to observe for changes in local variable, then call re-render.

```
export default Ember.Component.extend(bufferedRender({

  @on("init")
  _updateContent() {
    if (!this.get('content')) {
      ajax("/growfaq.json").then(data => {
        this.set('content', data);
      });
    }
  }

  @observes('content')
  _rerenderOnChange() {
    this.rerenderBuffer();
  },

  
}));

```

Also

> [@eviltrout](#):
>
> Also I should note that widgets aren’t really meant to retrieve data. They can do it, but it usually makes a lot more sense to retrieve the data in a route or controller and pass it down to the widgets.
