I had like to wrap any JavaScript invocation at runtime,
e.g. I had like to write to a log that an invocation of Func has been occurred.
This wrapping must work for any function even those function that has been added using eval or prototyping.
What your looking for is node-proxy
You can't do this using native JS. This will only work for node.js. It can probably be adjusted to work for any js running on V8.
If you were to call your functions with the call method, you could do something like this:
oldCall = Function.prototype.call;
Function.prototype.call = function(){
// do some logging here
oldCall.apply(this, arguments);
}
Related
What I want looks like this:
function bindFunctions(bindFunction, callbackFunction) {
// Add binding so that I can call the callbackFunction if the bindFunction is called
}
function log(message) {
console.log(message);
}
function notifyUser() {
alert('Something');
}
bindFunctions(log, notifyUser);
log('Error'); // Now the notifyUser-functions should be called and "Something" printed to the alert-box
bindFunctions($('.element').click, function() {/* CODE */}); // Or this: but I don't know if this is even possible because this is not the event-function but the binding-function of the click-event
Important: I have no influence on the bindFunction so it's not possible to implement a trigger there.
It's an attachment of a callback on any kind of existing function. Do you know how or if this is possible?
I believe you're looking at it the wrong way. What you need is some good old dependency inversion. Whatever code needs log has to receive it from a higher-level component (e.g. the composition root of your application). You're then free to implement a straightforward wrapper that calls notifyUser and inject it instead of the actual log.
I've linked some articles taking an OO perspective, but feel free to translate to a more functional model (the approaches are equivalent). In your case, you're using closures (which are, under a certain light, "equivalent" to objects with a single anonymous method).
The way you have to do to add a callback to a function is this:
var foo = function(number, callback){
number += 2;
callback(number);
}
foo(2, function(result){
window.alert(result)
});
https://jsfiddle.net/6dpz88md/
Good luck
In an effort to avoid repeating code I found it useful to have helper functions that could be called from within a foo.rendered function (for instance). Why is this possible in 0.9.3 of Meteor, but throws an error in 1.0 ?
Template.foo.helpers({
'fooFn' : function(){
return "something"
}
});
Template.foo.rendered = function(){
var something = Template.foo.fooFn();
}
Should I change the syntax in foo.rendered (am I calling it wrong?) or maybe use a different approach entirely (set up functions outside of the helpers({}) and rendered() and call those? or set this up as a registered helper function?
It looks like it is possible as of Meteor 1.0.3.1 to find and call helper functions, although it is clear it's not supposed to be used like this.
Still it can be done:
Template.foo.__helpers[" fooFn"]()
Please notice the leading space for the function name.
The other way of dealing with this is attaching a function to a global namespace, then calling that from somewhere else in your code, as user3557327 mentioned.
Additionally you can use:
Template.registerHelper('myHelper', function (){return 'Look At Me!'})
to register a global helper, and call it explicitly using:
UI._globalHelpers['myHelper']()
I think this would be a better method: How to use Meteor methods inside of a template helper
Define a function and attach it to the template. Call that function from rendered, as well as your template helper. Like MrMowgli said, you probably aren't "supposed" to call template helpers from within the .js file, only from the ...that could probably break in the future.
For example define a function and attach it to the tamplate:
Template.Play.randomScenario = function () { // HACK HACK HACK }
and then call it from your lifecycle method
Template.Play.created = function () {
Template.Play.randomScenario();
};
scenario: function () {
return Template.Play.randomScenario();;
},
I had the same problem and this is the solution I used. Hope that helps.
box_tpv1 = {
box:$("#box_tpv1"),
open:function(mensaje,f_ok,f_x){
this.box.show()
}
}
And when I call this box_tpv1.open() won't work, but If I write inside open function $("#box_tpv1").show() it works.
In your case, box_tpv1 is a singleton object, which cannot be further instantiated using new. Which means the value of this is insignificant.
You might as well simply call box_tpv1.box.show() inside the open function.
there might be issues on the context this function is being called and that depends upon from where are you calling this function from
try calling like this
box_tpv1.open.call(box_tpv1);
I don't know why but I solved it this way, I can get with this.box the value inside the object methods but doesnt work the jquery selector, if I do that it works
box_tpv1 = {
box:"#box_tpv1",
open:function(mensaje,f_ok,f_x){
$(this.box).show()
}
}
I get the javascript error
SCRIPT16385: Not implemented
When i run the following piece of code..
$j('img[id="edit_destination"]').bind('click',function(){
document.getElementById("edit_destination").onclick = editPRINum(this);
});
editPRINum is a function in the same javascript. I googled the problem and looks like i have to declare in case it is a variable. However i am using this to bind a function. What should i be doing?
I think you should try this
$j('img#edit_destination').bind('click',editPRINum);
In your callback function (editPRINum), this will be a reference to the img element.
PS: What is $j? a shortcut to jQuery?
I dumbed down this script so it wasn't so bulky, but the gist of it is that I keep getting a reference error every second that I have no function getList(). I tried to move setInverval() above and below it but it pretty much does nothing. It tells me an anonymous function is calling getList and that it is not defined.
If it makes a difference I had to add the jquery conflict so that it didn't interfere with mootools and I'm running joomla 1.5
jQuery(document).ready(function($) {
function getList(){
i=0;
$.getJSON(
"./test.php",
function(data)
{
while(data.streams[i]){
channel[i] = data.streams[i];
stats[i] = data.status[i];
title[i] = data.title[i];
viewers[i] = data.viewers[i];
i++;
}
}
);
}
setInterval("getList()", 1000);
});
I tried debugging it via console, but I'm still new at console debugging so it didn't get me too far. This program works alone, without joomla and the jquery no conflict stuff, in it's on HTML file just fine so I'm not sure what could possibly be wrong :/
When using the eval-style version of setInterval() (by passing a string) the function must be global. This is a bad thing anyway, so do this instead:
setInterval(getList, 1000);
And so you never pass a string again, in case you need arguments, do it like this:
setInterval(function() {
getList(whatever, ...);
// you can have more code here and even access local variables
}, 1000);
setInterval(getList, 1000);
setInterval (as well as setTimeout) require a reference to a function. Passing the function as strings have the same risk as using eval
Passing a string instead of a function to setTimeout() suffers from the same hazards as using eval. String literals are evaluated in the global context, so local symbols in the context where setTimeout() was called will not be available when the string is evaluated as code.