I have a directive I'm using for disabling buttons while I'm doing some behind the scenes work (to avoid getting double submits): http://jsfiddle.net/7nA3S/6/
I would like to be able to extend this so that the directive attribute can accept functions with arbitrary length argument lists.
I know the usual angular way is to just assign the values you need to other attributes on the element, but I'm wondering if there's a good way to generalize this. i could maybe do something like
<button my-submit='someFunction' args="arg1, arg2, arg3, ...">No Evals<button>
and then split up the args string, but maybe there's a less cruddy way?
Maybe something like this: http://jsfiddle.net/7nA3S/7/ .
You can use ng.$parse to evaluate the function with the arguments you have provided against the given $scope.
You then can get the arguments passed to the function from the arguments array inside the $scope.myAsyncSubmit function.
Related
I'm running a simulation, which has a "timePerTick" integer variable. The user can set this variable to speed up or slow down the simulation.
The simulation itself is handled by a few classes, all of whom consume the timePerTick variable.
I'm having some trouble eloquently passing timePerTick to the classes, because if I pass an integer, it's passed as a value, not reference. Right now, I'm passing a function that returns a reference, which works, but it's not very pretty.
Is there some pattern I'm not aware of that can handle this?
Is there any reason it needs to be passed by reference? If it's a user-supplied value, then it sounds like your functions aren't going to modify it, so it's not going to change.
If you really want the equivalence of passing it by reference, you could:
Pass the value to the classes each tick, like
myClass.onTick(timePerTick)
Pass an object instead of a number.
Objects in js are passed by reference. myClass.onTick(globalState), where globalState is an object or the class containing your variable.
Pass an object, that has a timePerTick value. Objects are passed by reference.
I am using code lines like the following in order to fetch data from an intranet website:
util.setProp(obj, "firstNameOld", $(msg).find('#fname_a').text());
Now I have another function in the same file where I want to use the above again, resp. the value of that object - currently I am hard-coding this ('Test') for test purposes:
util.setProp(obj, "firstNameNew", 'Test');
How can I pass the value from the firstNameOld object in one function to the firstNameNew object in another function ? If a solution with global variables is better here than this would work as well.
Many thanks for any help with this, Tim.
I've never used the framework that includes util But I imagine that if there is a setProp() then there has to be a getProp() or something similar.
If so, you could do something like
util.setProp(obj, "firstNameNew", util.getProp(obj, "firstNameOld"));
This also relies on the assumption that you want to copy from two properties in the same object.
If not, then pass the desired source object in the getProp() call.
My guess is that functions (or properties) are called "firstNameOld" and "firstNameNew", so the first time you get it from selector, second time you want to do the same.
Try to use the local variable like that:
var text = $(msg).find('#fname_a').text();
//
util.setProp(obj, "firstNameOld", text);
//
util.setProp(obj, "firstNameNew", text);
I'm trying to do something like this:
http://jsfiddle.net/bATu3/5/
Where the entire view is generated within an object, privately and return via a public method so that it can be generated on the page. I'm doing something wrong and would appreciate any pointers to help me sort this out.
Try this:http://jsfiddle.net/bATu3/10/
Basically there were few errors: Be careful of the use of 'this' within callback functions.
Also, note the data bind variables <p><strong data-bind="text:firstName"></strong></p>
another way to do this is: http://jsfiddle.net/bATu3/14/
you can specify the scope for computed values by passing it as a secondary parameter as noted here: Knockout: Computed Observables (read the "Managing ‘this’" section)
With Knockout 2.0 using this data-bind:
data-bind="click: $root.deleteSomeEntity.bind($data, $parent)"
in the Knockout viewmodel JavaScript the first argument in
self.deleteSomeEntity = function (data, parent) {
// perform deletion
}
seems to be the parent rather than the data.
Is there a reason for this behavior or something I'm missing?
When you call bind the first parameter will be the value of this. So, in your call this will be $data and the first argument will be $parent.
If $root is $parent in this case, then you can just do:
$root.deleteSomeEntity.bind($root)
KO will pass the data as the first parameter and this will be set to $root.
If $parent is not $root (and you likely don't want to rely on this being a different object that $root in your method on root), then you would do something like:
$root.deleteSomeEntity.bind($root, $data, $parent)
Otherwise, there are certainly ways to make sure that you have the proper this within your view model. It depends on your structure though.
Why are you using bind()? By default, if you just write the name of the javascript function as the click event Knockout will pass $data as the first argument and the event as the second.
http://knockoutjs.com/documentation/click-binding.html (Note 1&2)
Why bother with bind() when you can simply do this:
data-bind="click: function() {$root.deleteSomeEntity($data, $parent)}"
I am having a function in javascript as
function add(v1,v2){
var add=v1+v2;
}
Now I am calling this function as below -
write.out(var param="1,2";);
write.out(window[add](param););
Using the above call, it's not working. What it does is it gives the complete string "1,2" as value to the first param(v1) of the function.
Its working if I call the function in following way -
write.out(var param1="1";);
write.out(var param2="2";);
write.out(window[add](param1,param2););
I want to achieve it using the first way where i can send the parameters as a comma separated string of parameters.
Can some one help me out how this can be done...
Thanks!!!
You can make usage of ECMAscripts .apply(), which calls a function and accepts an array of paramters.
window['add'].apply(null, param.split(','));
That way, we execute the add function, setting its context to null (you could also change that if you need) and pass in the two paramters. Since we need an Array, we call split() on the string before.
So basically, the above line is the same as
add(1,2);
Since you're haveing that function in the global context (window), we don't even need to write it that explicitly.
add.apply(null, param.split(','));
will just be fine.