Using inlineRegexp with ajax


(Leo McArdle) #1

I’ve been trying to do an ajax call while using Dialect.inlineRegexp, and things haven’t exactly been going as I’ve wanted them to…

I’m trying to include data from the result of the ajax call in the returned html, in this case fetching data about a bug to put in the title of the link to the bug.

Hopefully the following code illustrates what I’m trying to do, but not succeeding at: (you might be able to tell that I’m fairly/completely new to promises)

Discourse.Dialect.inlineRegexp({
  start: 'bug',
  matcher: /^(bug (\d+))(?!\w)/gm,
  wordBoundary: true,

  emitter: function(matches) {
    var bug = matches[1]; // e.g. bug 12345
    var number = matches[2] // e.g. 12345

    return Discourse.ajax(Discourse.SiteSettings.bugzilla_url + '/rest/bug/' + number + '?include_fields=assigned_to,status,summary').then(function(data) {
     return ['a', {
        href: Discourse.SiteSettings.bugzilla_url + '/show_bug.cgi?id=' + number,
        title: data.bugs[0].status + ', ' + data.bugs[0].assigned_to + ' - ' + data.bugs[0].summary
      }, bug];
    });
  }
});

Ideas?


(Robin Ward) #2

You really should not do this, as not only are dialects synchronous so you can’t return promises or anything like that, but also they are run on both the client and server side.

It looks like you’re trying to do something like a onebox – could you just use a onebox for embedding a link to your bug tracker?


(Leo McArdle) #3

I was starting to think this might have been a little… unorthodox.

That’s the plan for “bug #” strings on their own line (though I still haven’t entirely worked out how to replace that with a onebox, or get the necessary information in the onebox).

This is for “bug #” strings within other text (here’s an example of it in action) where a onebox isn’t really feasible, but it would be nice to show more information in a title.

Is there anything one could do on the backend only? So, when a user submits a topic or response then it calls the Bugzilla API and adds the necessary info to the <a> tag.


(Leo McArdle) #4

Oop. I found the docs for this bit. They weren’t exactly hidden…


(Leo McArdle) #5

I’ve tried an alternative method, and I’m getting a weird error on the server side. Is this destined to fail just as the original was?

The error:

https://github.com/Mozilla-cIT/discourse-bugzilla/issues/4#issuecomment-67741644