Search code examples
ember.jsember-router

How to get `params.id` after the model hook in route?


After successfuly my page loads, some point later I would like to redirect to next page with params.id information. what is the correct way to retrive the params any time?

here is my router.js:

export default Ember.Route.extend(Validation, {

  model: function(params) { //where to store?

    if(this.store.hasRecordForId('card-list', params.id)){
        return this.store.peekRecord('card-list', params.id );
      }

  },


  actions:{

    goToNext(){
      return;
      this.transitionTo('cs2i.balance.balanceReview', 12 ); //how to get id?
    },

Any one help me here? Thanks in advance.


Solution

  • Your template has the id because of the model. So you can pass it directly into the action.

    Note: this is linking directly to the same route, you'll need to do some math to get the next id in goToNext. But in the case where a record is deleted and another created the ID is not going to be consistent enough to do a simple +1. It would be better to have the actual ID of the next record. Which could be in the relationship of the API. Then you could do something like model.previousId instead of model.id.

    // route.js
    export default Ember.Route.extend(Validation, {
      model: function(params) {
        if(this.store.hasRecordForId('card-list', params.id)){
          return this.store.peekRecord('card-list', params.id ;
        }
      },
      actions:{
        goToNext(paramId) {
          this.transitionTo('cs2i.balance.balanceReview', paramId);
        },
      }
    });
    
    // template.hbs
    <button {{action 'goToNext' model.id}}></button>