Best way to share logic between parallel routes


#1

I have forget-password (/forget-password) and reset-password (/reset-password/:token) route. They share some logic but their urls are not nested. Thus, I cannot use parent route to define those shared logic.

Would create a route with those shared logic and inherit it to create forget-password and reset-password routes be the best way to share logic here?


#2

You could define a “virtual” route PasswordRoute and let both extend that object.


#3

Don’t know abut best way. Another way is to create a mixin that you include in both. I personally prefer that.


#4

Can you point me to a “virtual” route example?


#5

virtual is just a description; just declare it like a normal route

// non ES6 syntax

// this route doesn't actually exist in your router
// it serves as ancestor for the two real routes
App.PasswordRoute = Ember.Route.extend({
   // shared logic ...

});

App.ForgetPasswordRoute = App.PasswordRoute.extend({
   // specific logic
});

App.ResetPasswordRoute = App.PasswordRoute.extend({
   // specific logic
});

#6

I learned something. Thanks!


#7

Another simple option (maybe better) is to put your shared code in a Mixin (see @rainerfrey’s answer in this thread.)