mercredi 7 août 2019

if id is invalid, go to another ember route

I have an ember project, the main concep is there is a list of books on the route /books, but you can pass in a book id in /books/1 and it would view that book only. Here is the router i made...

Router.map(function()
{
    this.route('books', {path: '/'});
    this.route('books', {path: '/*path'});
    this.route('books', {path: 'books/:book_id'});
});

So my default path will always be /books except if the user passes in an id

On my books route, I have the following functions

setupController(controller, model)
{
    this._super(controller);
    controller.loadBook(model.book_id);
},

model(params)
{
    return params;
}

So the model function returns the id and the setupController function will run on init and call the loadBook function in the controller passing in the id

In my controller I have my loadBook function

routerService: service('router'),

loadBook(book)
{
    const validBook = (book === 1);
    if (!validBook)
    {
        console.log('transitioning');
        this.get('routerService').transitionTo('books');
    }
}

So this works, if I go to /books/1, Its a valid book. But if I go to books/2, It will go in that function and transition to /books but the url is still books/2.

Is there any way to actually transition to /books so the url says that, thanks




Aucun commentaire:

Enregistrer un commentaire