I have a hub server method called with signalr many times.
I know i can do:
hub.server.method().done(function(data){
//my_code
}
but is there any way I can set a 'default' done() function so I don't have to repeat it every time I call that method?
Create a function in your *.js file like this
function doSomething(){
hub.server.method().done(function(data){
//my_code
}
};
And then call doSomething instead of
hub.server.method().done(function(data){
//my_code
}
Edited:
If you want to add a function to the server object you can do:
hub.server.myDecoratedBehavior= function() {
if (connected) {
this.originalBehavior().done(function () {
console.log("Chat window was cleared");
});
}
};
var onDone = function(data) {
// code
};
hub.server.method1().done(onDone);
hub.server.method2().done(onDone);
Related
I'm using the setInterval function in JavaScript but I get like one hundred reps per second from the console.log('NOW').
setTimeout also has the same effect.
Where is my mistake?
I want to call the function "function1" every 15 minutes.
JavaScript Code:
function1() {
console.log('NOW');
.
.
.
});
},
refreshData() {
this.function1();
setInterval(this.refreshData(), 900000);
},
Thanks in adavance!
you're invoking the function
setInterval(this.refreshData(), 900000);
rather than passing a reference to a function
setInterval(this.refreshData, 900000);
Wrap your function call like This:
var self = this;
refreshData() {
this.function1();
setInterval(function(){ self.refreshData() }, 900000);
}
There are two possible ways:
In provided code you should use setTimeout, because you restart function manually:
function function1() {
console.log('NOW');
}
function refreshData() {
this.function1();
setTimeout(this.refreshData, 3000);
}
refreshData();
Or simply replace existing logic with setInterval, it should do all the job you've implemented manually:
function function1() {
console.log('NOW');
}
this.function1();
setInterval(this.function1, 3000);
I am trying to call an exported function inside the nodejs module.
exports.sayHello = function (msg) {
console.log(msg)
}
function run(msg) {
this.sayHello(msg);
}
run("hello");
when I run this script I got TypeError: this.sayHello is not a function
Simply declare it separately from exporting it (and don't use this when calling it, you haven't attached it to an object):
function sayHello(msg) {
console.log(msg)
}
exports.sayHello = sayHello;
function run(msg) {
sayHello(msg);
}
run("hello");
That said, you could call it via exports:
exports.sayHello = function (msg) {
console.log(msg)
}
function run(msg) {
exports.sayHello(msg); // <===
}
run("hello");
...but that seems a bit roundabout to me, though I'm told it can help with testing, such as in this example.
I am facing the some issue with $.when().done() functions with jQuery. Can anyone help please? When I have ajax calls and non-ajax call methods, non-ajax call is calling even I use $.when().done(). See below snippet. Method/function three is running before.
$(document).ready(function () {
Initial();
});
function Initial() {
debugger;
var emp = { Name: "Ram", Age: 10 };
Main(emp);
}
function Main(em) {
$.when(One(em)).done(Two(em)).done(Three(em.Name));
}
function One(et) {
//some ajax call
console.log("One");
}
function Two(et) {
//some ajax call
console.log("Two");
}
function Three(et) {
console.log(et);//not an ajax call
console.log("Three");
}
Edit:
Below is the code snippet after the modifications by Vohuman, which is working like a charm
$(document).ready(function () {
Initial();
});
function Initial() {
debugger;
var emp = { Name: "Ram", Age: 10 };
Main(emp);
}
function Main(em) {
var def1 = $.Deferred();
var def2 = $.Deferred();
One(em, def1);
Two(em, def2);
$.when(def1, def2).done(function () {
Three(em.Name)
});
}
function One(et, defObj) {
//some ajax call
if (defObj) {
defObj.resolve();
}
console.log("One");
}
function Two(et, defObj) {
//some ajax call
if (defObj) {
defObj.resolve();
}
console.log("Two");
}
function Three(et) {
console.log(et);//not an ajax call
console.log("Three");
}
The () is called Invocation Operator. It invokes a function. This means you are calling the function yourself and the returned value of the function is set as the callback and not the function itself.
$.when(One(em)).done(Two).done(Three);
And if you want to have the callback called with parameters you should use a middleware, i.e. another function.
function Main(em) {
$.when(One(em)).done(function() {
Two(em);
}).done(function() {
Three(em.Name);
});
}
Also note that if you want send several ajax requests and have a callback executed when all of them are complete, you can pass several deferred objects to $.when:
$.when(deferredOne, deferredTwo).then(function(resolvedValueOne, resolvedValueTwo) {
});
And as a suggestion, do not use PascalCase names for regular functions. By convention, in JavaScript PascalCase names are used for naming constructors and classes.
another way of doing same is
$.when(One(em), Two(em)).done(function( a1, a2 ) {
Three(em.Name)
});
but One and Two method must return promise object.
I have an AngularJS factory that has multiple functions.
I want to call one of the functions inside the other function as shown below:
.factory("AppStart", function($cordovaSQLite) {
return {
init: function() {
var res = "hello";
console.log("in load start up page");
},
create_table: function() {
AppStart.init();
}
}
});
But I get the following error:
AppStart is not defined.
So how do I call the init() function in the create_table() function? I have tried just calling init(), but it doesn't work either.
To accomplish this, I recommend defining your functions with names, and then creating a service object with properties that refer to them, as I did below:
.factory("AppStart", function($cordovaSQLite) {
function init() {
var res = "hello";
console.log("in load start up page");
}
function create_table() {
init();
}
return {
init: init,
create_table: create_table
};
});
I need for a function to be executable only after an object is defined, I'm currently working in a fascade pattern and one method is dependent on another method. in this case 'addNewLayer' fails because 'setFullMap' hasn't finished executing. is there a solution? I'm using jquery and vanilla js so most any solution would be helpful at this point:
var jen = (function(){
function setFullMap(mapID){
jen.map = new Map(mapID);
}
function setLayer(opt){
//execute code here after jen.map is defined
}
return{
samp: function(id, opt){
setFullMap(id);
addNewLayer(opt);
}
};
})();
Thanks
solution:
var jen = (function(){
function setFullMap(mapID, callback) {
jen.map = new Map(mapID);
if(jen.map){
callback();
}
}
return {
samp: function(id, opt){
setFullMap(id, function(){
addNewLayer(opt);
}.bind(this));
}
};
})();
You will have to pass a callback function to setFullMap, and execute it once the function has completed (at the very end, before the closing }).
var jen = (function(){
function setFullMap(mapID, callback){
jen.map = new Map(mapID);
callback();
}
function setLayer(opt){
//execute code here after jen.map is defined
}
return{
samp: function(id, opt){
setFullMap(id, function() {
addNewLayer(opt);
}.bind(this));
}
};
})();
Do not forget using .bind(this) - it is very important in order to keep the original this in your callback function.
Edit:
Actually that would not work work if the Map constructor is a-synchronous. If you do not have access to the constructor and/or you cannot pass it a callback, then presumably the only (and sad) option would be to use a setTimeout or (easier) setInterval, continuously checking at defined intervals if the operation has been completed, and then fire the callback.
You could use a callback parameter:
function setFullmap(mapId,callback) {
jen.map = new Map(mapId);
callback();
}
....
samp: function(id, opt){
setFullMap(id,function() {
addNewLayer(opt);
});
}
When u dont have a way to manipulate the Map Object then u need to use a loop:
var loop=self.setInterval(function(){
if(jen.map) {
//execute code here after jen.map is defined
console.log(typeof jen.map);
window.clearInterval(loop);
}
},50);
Check jsfiddle:
http://jsfiddle.net/9yv5t/1/
I have checked the docs and it seems that there are various events you could listen to.
For example:
var m = new Map(...);
m.on('load', function () {
//execute code when the first layer is ready
});
var l = new Layer(...);
l.on('load', function () {
//execute code when the layer has been initialized
});
It's also carefully stated for the Layer.load event:
fires after layer properties for the layer are successfully populated.
This event must be successful before the layer can be added to the
map.