Wesley
Wesley

Reputation: 155

Ember. Keep selections from dropdown menu when user refreshes website

I have a dropdown menu in an ember component. When the user selects an option, the component sends an action to the route, and the route changes an attribute in a controller and makes a transitionToRoute to a different url.

Example:

rootpath= www.example.com

User selects candy. Url => www.example.com/candy

After candy,User selects kitkat. => www.example/candy/kitkat.

If the user refreshes the website or shares the url, the dropdown menu appears unselected. I know that using query params fixes this, but I don't want to use query params.

Any idea how can I do this? Thanks!

Upvotes: 0

Views: 69

Answers (1)

kristjan reinhold
kristjan reinhold

Reputation: 2048

You could use window.location on initialization. And extract last part out of it. Then find the following from you select data and set it as selection.

Something like this

....
this._super(...arguments);
var fullURL = window.location.href; // www.example.com/candy
var splittedURL = fullURL.split('/'); 
var lastPart = Ember.get(splittedURL, 'lastObject'); // candy.
var chocolateData = this.get('selectMenuContent');
var toSetOnInit = chocolateData.findBy('value', lastPart);
this.set('myChocolateSelection', toSetOnInit);
...

Upvotes: 1

Related Questions