Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: Default Query String Params #938

Closed
meenie opened this issue Mar 6, 2014 · 5 comments
Closed

RFC: Default Query String Params #938

meenie opened this issue Mar 6, 2014 · 5 comments

Comments

@meenie
Copy link
Contributor

meenie commented Mar 6, 2014

This is something that has plagued me for the last couple of months. When doing things like pagination, I like to use query strings. It doesn't matter what order they are in, they are optional, and it's industry standard. What I don't like is that I can't set a default value for each of the query strings that I register with my state. So in my resolve I have to check if something like page is empty, if it is, set it to 1. Or if limit is empty, set it to 25. If I could just pass the current $stateParams straight to my XHR call without having extraneous code checking for defaults that would be fantastic.

So have something like this:

$stateProvider.state('products', {
    url: '/products?page&limit&orderBy&direction',
    controller: 'ProductsCtrl as ProductsCtrl',
    templateUrl: 'app/products/template.tpl.html',
    defaultQueryParams: {
        page: 1,
        limit: 25,
        orderBy: 'popular'
    }
});

Then if someone landed on /products, the $stateParams would be:

{
    page: 1,
    limit: 25,
    orderBy: 'popular'
}

If the url was /products?orderBy=price&direction=desc, the $stateParams would be:

{
    page: 1,
    limit: 25,
    orderBy: 'price',
    direction: 'desc'
}

I am willing to put in the work to get this done. I just want to make sure it's something that is needed and will get merged in :).

@svemir
Copy link

svemir commented Mar 6, 2014

I was trying to do the same thing but in the end I made sure the server response with list data has all the information. Server side has to have defaults anyway, so my pagination code uses the numbers from server response instead of the state values. The state does have the same URL arguments you have, but they only become explicit when user goes to another page or changes the order.

Still, it would be nice to be able to specify the default values, but they would be default state values, not default query values, I guess.

@meenie
Copy link
Contributor Author

meenie commented Mar 7, 2014

I think you are right in that it should be separate from the URL in that it should just be default $stateParams. So it would be something like:

$stateProvider.state('products', {
    url: '/products/:category?page&limit&orderBy&direction',
    controller: 'ProductsCtrl as ProductsCtrl',
    templateUrl: 'app/products/template.tpl.html',
    defaultStateParams: {
        category: 'featured',
        page: 1,
        limit: 25,
        orderBy: 'popular'
    }
});

So the url /products would have the $stateParams:

{
    category: 'featured',
    page: 1,
    limit: 25,
    orderBy: 'popular'
}

And the url /products/new?page=2&limit=100, the $stateParams are:

{
    category: 'new',
    page: 2,
    limit: 100,
    orderBy: 'popular'
}

@nateabele
Copy link
Contributor

See #454 and #125.

@nateabele
Copy link
Contributor

Fixed in a472b30. Will be merged to master shortly.

@gronel
Copy link

gronel commented Aug 23, 2016

please help me how to set static value in query string this is my code

.state('160100000027', { url: '/issues/Response/?modtype=1', templateUrl: '/Issues/TopicResponse', controller: 'ctrlTopicResponse as ctrl', ncyBreadcrumb: { label: 'Topic' } })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants