When to use '<event>'(event, instance) and when to use '<event>':function (event)? - meteor

I'm new to meteor, and I've followed different tutorials that explain and use things differently.
There seems to be two ways of processing an event. For example, if I want to manage a click on a tag, both of the following methods work :
This one is present on the hello world meteor app
'click p'(event, instance) {
}
This one is the one used in the tutorial.
'click p': function(event){
}
Both work perfectly and if I use both the last one will be effective. The weird thing is the color is not the same (on sublime text), the second has usual js colors but the first one is only green, orange, and everything else is white (on monokai).
I'm tempted to use the second one for better visibility, but I know I should not make that choice base on that. Which one is correct, and when ?

Bottom line: it doesn't really matter if you only need the event.
There are 2 syntactic differences between the functions, but there is no substantial difference:
The notation that you are using:
funcName(arg1, arg2)
vs
funcName: function(arg1, arg2)
The highlighting coloration difference you see in your editor is probably related to the shorthand notation. This shorthand notation is a feature of ES2015, the relatively new version of JS, and both are functionally identical. It is just syntactic sugar.
The arity (number of arguments).
The function is used as an event handler as a callback. Due to the dynamic nature of JavaScript, any function can be called with any number of parameters. The parameters are being assigned to arguments based on the function's definition, and are also dynamically available to the called function via the arguments pseudo-array.
The callback will always be called with 2 parameters. In the version with 1 argument, the second parameter will not be bound to any identifier within the function. You can omit the second argument if you don't need the template instance.

Related

Is a method still "pure" if it invokes a designated callback given as parameter

Eg. PureFoo(onSuccess: () => { DoSomething(); }); (example in C#)
Does Purefoo still counts as pure (without "side effects") if it invokes onSuccess? or is the method only "honest" (as in doesn't hide what it does)?
I've searched on Google but it didn't yield relevant results.
So, long story short, that sounds like a no.
If there are side effects, from whichever source, it shouldn't/can't be called pure.
However, I guess it can still be made more honest by limiting the side effects to only all clear & explicit intended callbacks which sorta ask as an alternative return + call based on returned values.
Note that you can't ensure the callback is called only when expected and the number of times it's expected, etc. still.
In this particular case, onSuccess has a name and a signature (returning void/unit) that signals an intentional side effect. If your function doesn't do anything with the result of the onSuccess() call, then there isn't anything possible to pass into there that will make the outer call pure.

How do you apply src_indices in promotes_inputs=[], when you have multiple promoted inputs?

The docs only show examples for when a component promotes a single input. How do I use src_indices to indicate that only one of my promoted inputs takes a certain slice?
p.model.add_subsystem('ComputeWakePosition', ComputeWakePosition(num_wake_points_per_side=4),
promotes_inputs=['wake_upper_lengths',
'wake_lower_lengths',
'wake_upper_angles',
'wake_lower_angles',
'displaced_cw_coordinates'], <-- I want to specify src_indices for this input only.
promotes_outputs=['upper_wake_coordinates',
'lower_wake_coordinates'])
I think I would be able to just use connect for that input, but given that everything else I've written doesn't use it, it'd be nice if there was a way to avoid it.
There is a function called promotes that you can call on your group after you've added a subsystem. In your code above, you could remove the promotion of the displaced_cw_coordinates variable from your add_subsystem call and make a separate call something like this p.model.promotes('ComputeWakePosition', inputs=['displaced_cw_coordinates'], src_indices=[2,4,6,8])

How can I tell the Closure Compiler not to rename an inner function using SIMPLE_OPTIMIZATIONS?

How can I tell the Closure Compiler not to rename an inner function? E.g., given this code:
function aMeaninglessName() {
function someMeaningfulName() {
}
return someMeaningfulName;
}
...I'm fine with Closure renaming the outer function (I actively want it to, to save space), but I want the function name someMeaningfulName left alone (so that the name shown in call stacks for it is "someMeaningfulName", not "a" or whatever). This despite the fact that the code calling it will be doing so via the reference returned by the factory function, not by the name in the code. E.g., this is purely for debugging support.
Note that I want the function to have that actual name, not be anonymous and assigned to some property using that name, so for instance this is not a duplicate of this other question.
This somewhat obscure use case doesn't seem to be covered by either the externs or exports functionality. (I was kind of hoping there'd be some annotation I could throw at it.) But I'm no Closure Compiler guru, I'm hoping some of you are. Naturally, if there's just no way to do that, that's an acceptable answer.
(The use case is a library that creates functions in response to calls into it. I want to provide a version of the library that's been pre-compressed by Closure with SIMPLE_OPTIMIZATIONS, but if someone is using that copy of the library with their own uncompressed code and single-stepping into the function in a debugger [or other similar operations], I want them to see the meaningful name. I could get around it with eval, or manually edit the compressed result [in fact, the context is sufficiently unique I could throw a sed script at it], but that's awkward and frankly takes us into "not worth bothering" territory, hence looking for a simple, low-maintenance way.)
There is no simple way to do this. You would have to create a custom subclass of the CodingConvention class to indicate that your methods are "local" externs (support for this was added to handle the Prototype library). It is possible that InlineVariables, InlineFunctions, or RemoveUsedVariables will still try to remove the name and would also need to be fixed up.
Another approach is to use the source maps to remap the stack traces to the original source.
read the following section
https://developers.google.com/closure/compiler/docs/api-tutorial3#export
Two options basically, use object['functionName'] = obj.functionName or the better way
use exportSymbol and exportProperty both on the goog object, here is the docs link for that
http://closure-library.googlecode.com/svn/docs/closure_goog_base.js.html
-- edit
ah, i see now, my first answer is not so great for you. The compiler has some interesting flags, the one which might interest you is DEBUG, which you can pass variables into the compiler which will allow you to drop some debugging annotations in via logging or just a string which does nothing since you are using simple mode.
so if you are using closure you can debug against a development version which is just a page built with dependiencies resolved. we also the drop the following in our code
if(DEBUG){
logger.info('pack.age.info.prototype.func');
}

Detect when ALL HTML page rendering has taken place

I am working with a pretty complicated .aspx page that is full of controls (Telerik, Ajax, etc.) that all expand, collapse, show, hide, etc. when the page is loaded. Since this rendering happens on the client-side and can take different lengths of time based on the users machine specs, is there a way to detect when all (or some) rendering has taken place (jQuery?) so I can then act on specific elements, knowing they are fully rendered?
JavaScript is single threaded. The time passed to setTimeout is a minimum, but not a maximum, so if you pass something like 10(ms), you essentially are saying "execute this code after all the currently running code is finished."
So, if all the controls use $(document).ready() to do their thing, all you need is:
$(document).ready(function() {
setTimeout(function() {
doStuff();
},10);
});
doStuff will be called after all the functions passed to $(document).ready have run. However, this isn't foolproof. If the controls have their own way of detecting whether the document has loaded, or do their own setTimeout(), you're in trouble. The problem is that JavaScript does not guarantee the execution order of setTimeouts. Sometimes your code may run last, other times it may run before the setTimeouts used for the animation.
One last idea: if all the animation is done using jQuery, then the effects run in a single queue. In doStuff you could add an animation of some sort with a callback and be reasonably certain that the callback would run last.
Whenever I had to wait for multiple things to be ready before proceeding, I would create an array with true/false values. Every mandatory part of the page got an event which, when it is called, updates the specific entry in the array to true. Also, it called a general function which returned true if all values in an array was true, otherwise false.
If that function finally returned true, I would proceed with the execution. It is especially useful if you have to wait for an AJAX call to end but don't want to use async = true. It also is useful if you want to start loading multiple things at once instead of one after another, since they all report ready-state to the same array.
It does however use global variables so you might need to do some optimizations. You might not want to do this approach either if you have a grudge against global variables.
You should place your code inside the jQuery $(document).ready() function. This will ensure that all elements are loaded before the code runs.
http://docs.jquery.com/Tutorials:Introducing_$(document).ready()
I think the doc you need is:
http://docs.jquery.com/Events/load
"I can then act on specific elements, knowing they are fully rendered?"
You can use the load method (linked above) to attach to any element. So if you had a div with an id of "lastElement", you could write
$('div#lastElement).load(runThisFunction);

VBScipt: Call builtin functions shadowed by global variables

VBScript on ASP Classic contains an "int" function. (It rounds numbers towards -∞.) Suppose that some excessively "clever" coder has created a global variable named "int". Is there any way to get at the original function? I've tried all manner of workarounds with scoping and dodgy execs, but no dice. I suspect that it is impossible, but I'm hoping that someone will know more about it than I do.
EDIT: Thanks for the responses. Since y'all asked, the global variable, called "Int" (though unfortunately, vbscript is not case-sensitive), is a factory for a class similar to Java's Integer. The default property is essentially a one-arg constructor; i.e. "Int(42)" yields a new IntClass object holding 42. The default property of IntClass in turn simply returns the raw number.
The creator was trying to work around the lack of proper namespaces and static methods, and the solution's actually pretty seamless. Pass in an IntClass where an int is expected and it will automatically trigger the default property. I'm trying to patch the last remaining seam: that external code calling "int" will not round properly (because the constructor uses CLng).
Not that I know of, getref only works on custom functions not on build-ins. I would suggest renaming the custom'int' function and update all references to this custom ones. You can use the search function visual studio (express) or any other tool of your liking for this. Shouldn't be to much work.
I didn't think reserved words would be allowed for function names or variables.
Duncanson's right. Do the pain and rename int. Chances are there are worse things going on than just this.
(why would someone make a global variable named int... that's going to take some thinking)
Or you can use CInt instead on Int
response.write trim(cint(3.14)) + "<br>"
Wrong!!
See NobodyMan comments

Resources