tuupola / jquery_chained

Chained Selects for jQuery and Zepto
https://appelsiini.net/projects/chained/
589 stars 283 forks source link

Added support for an url callback function #43

Closed edwinhageman closed 9 years ago

edwinhageman commented 9 years ago

I needed a way to add custom parameters to the url based on the value of other input fields. This little change makes it possible to pass a callback function to the url option which should return the url without breaking backwards compatibility.

example:

$('#select2').remoteChainedTo({
   parents: '#select1',
   url: function() {
      var url = '/api/endpoint';
      var otherInput = $('#otherInput').val();
      if (otherInput != '') {
         url += '?customParam='+otherInput;
      }
      return url;
   }
});

If you like this functionality but its missing something or you would like to see it implemented in some other way let me know and I will make some time to look into it.

tuupola commented 9 years ago

Looks good. However I think you can already achieve your example with the code below. Check Send additional values in docs for better explanation.

$("#select2").remoteChained({
    parents : "#select1",
    url : "/api/endpoint",
    depends : "#otherInput"
});

That said, I can see there might be other uses cases for url as a function. Let's keep this open while I do some thinkingl

edwinhageman commented 9 years ago

You're right, I was under the impression I could only use depends on other jquery_chained elements.