How to programatically setup default query params after model?

Here’s the deal:

You’re able to set default values for query params, so that when the query param matches the original value, ember removes it from the URL. I need to be able to programatically set these after I get the model back. Is there a way to do this in Ember or are you not allowed?

Example:

http://my-site.com/category

The default page which people can filter on and updates query params. But, lets say I also have:

http://my-site.com/category/3-stars

I want to be able to set the default query params as 3-star so that when I reset the query params, the default 3 star is still there, but the others are gone.

please note: These urls are generated by humans and so thats why I need to be able to set them programatically. THeres many other default params they can use for these SEO urls.

We use wildcard-globbing pages the same way you want. We have created the option to create an unlimited number of pages but only show one product because the next 2 will be released later this year. See the page route below for /products/scanner-vibrato https://www.martinic.com/products/scanner-vibrato

It is still possible to created special pages like ‘sitemap’

Router.map(function() {
	this.route('wrapper', {path: '/'}, function() {
		this.route('index', { path: '/', resetNamespace: true });
		this.route('sitemap', {path: '/sitemap', resetNamespace: true});
		this.route('page', {path: '/*page_name', resetNamespace: true});
	});
});

Also take a look at the routes in the ember inspector.

Hey, thanks for the reply! I think I didn’t explain it enough. My issue is with query string params, not the route itself.

Lets see if I can try this again :smiley:

Say we have a page:

http://my-site.com/category

And the available query string params are: ‘star’, ‘user’, ‘something’. I can assign the default to “null” and when I set those variables on the controller to something that’s not null, it will add the query string params such as:

http://my-site.com/category?star=3&user=2&something=yep

I have set the default query params as null so if I set them to null again, it will remove them from the query string in the URL since they’ve gone back to the default value.

What I want to do is when a user goes to:

http://my-site.com/category/3-star

I want the default query string param in the controller and route to be “star:3”, so that if they change it to 4 star filter it would look like:

http://my-site.com/category/3-star/?star=4

But, if they changed it back to 3 star, the url would update back to:

http://my-site.com/category/3-star/.

Does that make more sense?

The 3-star ending is something an internal person would type, so it wont always be like that. it could be something as “/category/silly-name” but it gives me the filter type upon successful model loading so I know at that point what the default filters would be and need to be able to set them without triggering the router to update the query string params or triggering another didTransition

Trying to understand. If your (route 3-star eq query param star=3) you want to set query param star to null?

@tsteuwer I was going to suggest trying something like computed.oneWay as a way to set the default via the model hash at runtime, etc. but after playing around with it a bit that doesn’t really seem to do anything. Ember never hides the param properly, presumably because it doesn’t know what the default is supposed to be.

My only other suggestion would be playing around in the router, like in the serizalize/deserialize params logic, or maybe elsewhere in the router module. Kind of a scary prospect but… I’m certainly no authority in this regard, so take what I say with a grain of salt, but I’m going to guess there’s no beaten-path way of doing what you’re trying to.

EDIT: @broerse I think he means the way that ember “hides” the query param when it is equal to its default value. Like if you had the following in a controller:

queryParams: ['genre'],
genre: "All"

then “All” would be the default for the “genre” query param, so if the query param bound value was set to, say, ‘Rock’, the url would be <origin>/#/<route>?genre="Rock" but if it were “All” the url is shown as <origin>/#/<route> (it “hides” the query param because it is the default value). Except @tsteuwer wants the default value to be dynamically determined at runtime I think, instead of statically defined like above.

This exactly. I’m looking to bind the default value of a query param at runtime (after the model has resolved).

You can redirect to the current route with the resolved model.

...
afterModel(model) {
  if ( ... ) { // whatever condition you like
    this.replaceWith(this.routeName, model, { // replace so we don't add another history entry
      queryParams: {
        qp1: ..., // fill in default value
        qp2: ... // or forward query params that's already set
      }
    });
  }
}
...
1 Like