Router Opting Into a Full Transition

optional

You can use the optional queryParams configuration when a controller query parameter property changes to opt into a full transition by setting the refreshModel config property to true. The transitionTo or link-to arguments will change in the query parameter values, but do not change in the route hierarchy; the controller properties will get updated with new query param values as well in the URL.

Syntax

Ember.Route.extend ({
   queryParams: {
      queryParameterName: {
         refreshModel: true
      }
   }
});

Example

The example given below shows opting into a full transition when a controller query param property changes. Create a new route and name it as paramfulltrans and open the router.js file to define URL mappings −

import Ember from 'ember';                   
//Access to Ember.js library as variable Ember
import config from './config/environment';
//It provides access to app's configuration data as variable config 

//The const declares read only variable
const Router = Ember.Router.extend ({
   location: config.locationType,
   rootURL: config.rootURL
});

//Defines URL mappings that takes parameter as an object to create the routes
Router.map(function() {
   this.route('paramfulltrans');
});

//It specifies Router variable available to other parts of the app
export default Router;

Open the file application.hbs file created under app/templates/ with the following code

<h2>Opting Into a Full Transition</h2>
{{#link-to 'paramfulltrans'}}Click Here{{/link-to}}

When you click the above link, the page should open with an input box which takes a value entered by an user. Open the paramfulltrans.hbs file to opt into a full transition by using the queryParams configuration −

//sending action to the addQuery  method
<form {{action "addQuery" on = "submit"}}>
   {{input value = queryParam}}
   <input type = "submit" value = "Send Value"/>
</form>
{{outlet}}

Now define the computed property of the queryParam filtered array which will display the paramfulltrans template −

import Ember from 'ember';

export default Ember.Controller.extend ({
   //specifying 'query' as one of controller's query parameter
   queryParams: ['query'],
   
   //initialize the query value
   query: null,

   //defining a computed property queryParam
   queryParam: Ember.computed.oneWay('query'),
   actions: {
      addQuery: function () {
         
         //setting the query parameters and displaying it
         this.set('query', this.get('queryParam'));
         document.write(this.get('query'));
      }
   }
});

Now use the queryParams configuration on the Route with the respective controller and set the refreshModel config property to true in the paramfulltrans.js file defined under app/routes/.

import Ember from 'ember';

export default Ember.Route.extend ({
   queryParams: {
      query: {
         //opting into full transition
         refreshModel: true
      }
   }
});

Output

Run the ember server and you will receive the following output −

optional

When you click on the link, it will generate an input box wherein you can enter a value and send an action to the addQuery method .

After clicking the button, it will show the parameter value to the right of the ” ? ” in a URL.

Previous Page:-Click Here

Leave a Reply