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
Feature request: Allow $.param(function) #3008
Comments
|
If it's only for the top level couldn't you just do this already? var params = $.param((function(){
var obj = {};
// Dynamically build obj
return obj;
})()); |
|
You can, and your example is essentially how I am handling it now. But I guess for the same reason you wouldn't want to do: var params = $.param({
a: (function(){ return 1; })(),
b: (function(){ return 2; })(),
c: (function(){ return 3; })()
});..more key strokes, more bytes, uglier code. Probably the most relevant reason (for me) is dealing with ambiguous variables. Consider the following example (which happens frequently in my world): function Widget (options) {
this.url = options.url;
this.params = options.params; // Plain object -or method that returns object.
}
Widget.prototype.remoteFunction = function () {
$.ajax({
url: this.url,
data: this.params // Could be object or method
});
}Simply invoking data: typeof this.params === 'function' ? this.params() : this.params;(might not seem like much, but this is repeated a few dozen times in each of my projects) This seems to break the model of library (most abstract) to plugin (less abstract) to implementation (most defined). And, in my opinion, the most natural place for an ambiguous entry point would be the library. On a lesser note: function getParams () {
return {};
}
$.ajax({ url: 'abc.php', data: getParams }); // Seems more intuitive than
$.ajax({ url: 'abc.php', data: getParams() });Allowing a function argument would result in the same for both examples, but currently only the latter would work. |
|
I'm opposed to expanding the input signature in this way, and with @dmethvin on valuing consistency between |
|
I agree with Dave and Richard. |
Would it be a bad idea for
$.paramto allow a function argument?@dmethvin originally mentioned in #2633
Although I am suggesting additional functionality, I tend to agree with @dmethvin 's point of minimizing the behavior of this function (I am a fan of simple and strict rules).
My thought process being, that if it's allowed on an attribute level, it should certainly be allowed at the root. However, secondarily, I am suggesting that this should only be allowed at the root, and to simply ignore method attributes (similar to
JSON.stringify). To me, it would make more sense to pass a function that builds a dynamic object, than to pass an object that can build dynamic properties -almost seems like the tail is wagging the dog. A natural evolution might be to firstly allow this at the root, and to eventually phase out methods on an attribute basis.The text was updated successfully, but these errors were encountered: