I'm working on someone else's code and I don't fully understand it yet. Besides, I'm fairly new to AngularJS.
They have a class that looks like this:
return vm.item[vm.function](request).$promise.then(function (data) {
...
});
When my vm.function has no parameters, it works perfectly. I just do
vm.function = "myFunction"
and myFunction gets called as expected.
However, now I need to call a function that has an "id" parameter. I have already tried
vm.function = "myFunction(0)"
vm.function = "myfunction({id:0})"
And even adding an "id" element to the "request" var, but no result so far.
How can I make this work?
vm.function
is simply the name of the function, it says nothing of the parameters. Let's break down what's going on here.
vm.function = 'myFunction';
vm.item[vm.function](request)
translates to
vm.item['myFunction'](request)
which is equivalent to
vm.item.myFunction(request)
As you can see, it's already passing a request
parameter. To add another, you'd simply do:
vm.item[vm.function](request, id)
Keep in mind when using this pattern that your dynamic function signatures must be compatible. If they begin to diverge (as you say, you're editing someone else's code) you should refactor to handle this in a different way.