How to render template when adding custom route in discourse

I am trying to add a custom route (non-admin route) discourse.

Required route http://discourse.org/customroute

I am able to add the route, but the problem is that instead of rendering the template, it just outputs the json text.

My json data in plugin.rb is

render json: {title:"custom route"}

My customroute-map.js.es6 contains

export default function() {
  this.resource('customroute', { path: '/customroute' })
}

In the route/customroute.js.es6

export default Discourse.Route.extend({
   model:function() {
     return ajax('/customroute.json');
   }

By doing so, I only get the json data in the template. But the customroute.hbs - handlebar template is not rendered.

I tried using the renderTemplate,

 renderTemplate: function() {
      this.render('/customroute');
 }

But then I get the error "Assertion Failed: Cannot callEmber.getwith an empty string"

2 Likes

You have to add the template by using

Discourse.replaceState(`customroute`)

So the route/customroute.js.es6 will be

export default Discourse.Route.extend({

  model: function(params) { return params },

  setupController: function(controller, params) {
    ajax(`/customroute.json`).then(function(data) {
      controller.setProperties({ model:data })
      DiscourseURL.replaceState(`/customroute`)
    })
  }
})
3 Likes