Hi all,
I’m building a plugin but having some problems understanding what the current API is for appending routes to the Ember router. I’ve read a number of tutorial/howto posts on Meta but there seem to be a number of different ways of doing it, and it’s unclear what is the ‘current’ way.
The tutorials are great but they clearly can’t cover every use-case, and so one is left trying to guess how you do routing if you don’t want to add to an admin
route, for example. In my case I want to add a new tab to the user’s profile, and have managed to do this using the plugin outlet.
I’ve followed the following tutorials in detail:
Creating Routes in Discourse and Showing Data
Adding Ember Components to Discourse
These are helpful but they are written from the point of view of patching Discourse core code, rather than building this into a plugin, so some elements of it don’t apply.
Of course I’ve also followed the full series of Beginner's Guide to Creating Discourse Plugins Part 1: Creating a basic plugin through Beginner’s Guide to Creating Discourse Plugins Part 7: Publish your plugin
At present I’m struggling with what will, I’m sure, turn out to be a simple problem with the interplay between Rails routing and Ember routing.
I have added my ‘Reflection’ tab to the user profile, which seems to work
// assets/javascripts/discourse/reflexivity-route-map.js.es6
export default {
resource: 'user',
path: '/u/:username/reflection',
map() {
this.route('reflection');
}
};
On the Rails side there is a route declaration which is intended to serve only JSON to Ember’s Ajax call, it seems to be working at the URL reflection.json
, although I haven’t figured out the Ajax bit yet within Ember (see below)
# config/routes.rb
Discourse::Application.routes.append do
get "u/:username/reflection" => "reflections#index", constraints: { username: RouteFormat.username }
get "users/:username/reflection" => "reflections#index", constraints: { username: RouteFormat.username }
end
Problem1
All this seems to work fine if I navigate to the Reflection tab from within another part of the Ember app, but if I refresh the page while on my Reflection tab, the Ember app disappears and it seems as though Rails routing takes over, because I just get JSON in the browser.
I’ve tried all kinds of permutations of routing options in Ember and Rails and can’t get my head around what’s going on.
Problem 2
I’m also having difficulty getting the Ember router to be run, I don’t know if it’s a file path issue, or if there is some other problem.
// assets/javascripts/discourse/routes/user/reflection.js.es6
import { ajax } from 'discourse/lib/ajax';
export default Ember.Route.extend({
model() {
console.log('The model hook just ran!') # <-- this does not log to console
const userReflection = "something"
}
});
I’ve looked at a number of other plugins and plugin templates (eg ProCourse) in order to deduce the patterns, but there seems to be a variety of different ways of doing things, and although inspecting the Discourse source code can be useful, it doesn’t replace the need for regular documentation with examples. Of course, the Ember and Rails guides and doco are useful, but some elements of this interaction are pure Discourse, and hence different to how it’s done in pure Ember or Rails.
Any help with the above would be greatly appreciated, and of course I can supply more information if needed.