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

Allow params to be a callback #2615

Closed
burzum opened this issue Mar 11, 2016 · 3 comments
Closed

Allow params to be a callback #2615

burzum opened this issue Mar 11, 2016 · 3 comments

Comments

@burzum
Copy link

burzum commented Mar 11, 2016

We have a filter that sets selected params to the local storage with cookie fallback. I could not find a better way to inject the params as shown below.

I could encapsulate the whole repetetive logic here into a method that would build the params based on the filter settings we have for each filter type. But the router won't let me do that. :( I can define a callback for every param but I can't do params: function() { ... }.

Is there a better way of doing what I try to do or can we get that feature added to the router? We're currently using 0.2.*.

params: {
  page: function(listingFiltersService) {
    return listingFiltersService.getQueryParam('profiles', 'page', 'int');
  },
  country_id: function(listingFiltersService) {
    return listingFiltersService.getQueryParam('profiles', 'country_id', 'int');
  },
  state_id: function(listingFiltersService) {
    return listingFiltersService.getQueryParam('profiles', 'state_id', 'int');
  },
  category_id: function(listingFiltersService) {
    return listingFiltersService.getQueryParam('profiles', 'category_id', 'int');
  },
  founded: function(listingFiltersService) {
    return listingFiltersService.getQueryParam('profiles', 'founded', 'int');
  },
  company_size_id: function(listingFiltersService) {
    return listingFiltersService.getQueryParam('profiles', 'company_size_id', 'int');
  },
  term: function(listingFiltersService) {
    return listingFiltersService.getQueryParam('profiles', 'term');
  }
},

Overall I had a lot of trouble getting the query params to persist, even between two route changes... I've also had to deal with this issue #2603 and this here #2608 was (well, still is) a huge pain in the rear.

@eddiemonge
Copy link
Contributor

Is this when you are defining the states?

@christopherthielen
Copy link
Contributor

I want to clarify this: you are defining default values for parameters in code such as this:

params: {
  page: function(listingFiltersService) {
    return listingFiltersService.getQueryParam('profiles', 'page', 'int');
  },

Is that what you want to do? If no parameter value is provided (either from ui-sref or deep link) then the default value is computed (your functions are called) and used. I'm not sure if that's your assumption or not based on this comment:

Overall I had a lot of trouble getting the query params to persist, even between two route changes.


Or, you could reduce your state definition boilerplate like so:

function profileQueryParam(name, type) {
  return function(listingFiltersService) {
      return listingFiltersService.getQueryParam('profiles', name, type);
    }
}

$stateProvider.state('foo', {
  params: {
    page: profileQueryParam('page', 'int');
    country_id: profileQueryParam('country_id', 'int');
    state_id: profileQueryParam('state_id', 'int');
    category_id: profileQueryParam('category_id', 'int');
    founded: profileQueryParam('founded', 'int');
    company_size_id: profileQueryParam('company_size_id', 'int');
    term: profileQueryParam('term')
  }
}

Or go even deeper:

function profileQueryParam(name, type) {
  return function(listingFiltersService) {
      return listingFiltersService.getQueryParam('profiles', name, type);
    }
}

function makeParams(paramsDefArray) {
  return paramsDefArray.reduce(function (acc, paramDef) {
    var name = paramDef.split(":")[0];
    var type = paramDef.split(":")[1];
    acc[name] = profileQueryParam(name, type);
    return acc;
  }, {});
}

$stateProvider.state('foo', {
  params: makeParamsObj([
    'page:int', 
    'country_id:int', 
    'state_id:int', 
    'category_id:int', 
    'founded:int', 
    'company_size_id:int', 
    'term'])
})

@eddiemonge
Copy link
Contributor

closing due to inactivity

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

3 participants