I have a similar code:
Template.mytemplate.pippo = function() {
var returnValue;
asyncFunc(function (dataReturned) {
returnValue = dataReturned;
});
return returnValue;
}
I have try to load future on client side
var Future = Npm.require('fibers/future');
but don't work :(
How can I wait that asyncFunc return callBack finish to return template value returnValue
thanks!
There is a bit of a question of where you should be calling asyncFunc, but once you figure that out, your code should look something like this:
asyncFunc(function (dataReturned) {
Session.set('returnValue', dataReturned);
});
...
Session.setDefault('returnValue', "loading.."); // or some other default that is safe
Template.mytemplate.pippo = function() {
return Session.get('returnValue');
}
Related
I have several charts built with dc.js. I can achieve the desired functionality by attaching a callback to each dc.js chart's .on("filterted", function(chart) {}) but this is annoying because I have to attach the same callback to each chart. And error prone because as new charts are added, someone has to remember to attach an event hander. I would prefer to just attach a callback to the underlying crossfilter. Is that possible?
Is there a way to optimize this...
var ndx = crossfilter(data);
var dimAlpha = ndx.dimension(function(d) {return d.alpha});
var dimBeta = ndx.dimension(function(d) {return d.beta});
var groupAlpha = dimAlpha.group().reduceSum(function(d) {return 1;});
var groupBeta = dimBeta.group().reduceSum(function(d) {return 1;});
dc.pieChart(myDomId1)
.dimension(dimAlpha)
.group(groupAlpha)
.on("filtered", function(chart) {
//do stuff
});
dc.pieChart(myDomId2)
.dimension(dimBeta)
.group(groupBeta)
.on("filtered", function(chart) {
//do stuff
});
into something like this...
var ndx = crossfilter(data);
var dimAlpha = ndx.dimension(function(d) {return d.alpha});
var dimBeta = ndx.dimension(function(d) {return d.beta});
var groupAlpha = dimAlpha.group().reduceSum(function(d) {return 1;});
var groupBeta = dimBeta.group().reduceSum(function(d) {return 1;});
dc.pieChart(myDomId1)
.dimension(dimAlpha)
.group(groupAlpha);
dc.pieChart(myDomId2)
.dimension(dimBeta)
.group(groupBeta);
ndx.on("filtered", function() {
//do stuff
})
If you've got a million charts and don't want to have to attach the event listener to each one manually, you could iterate through the chart registry and add them that way. Ex:
dc.chartRegistry.list().forEach(function(chart) {
chart.on('filtered', function() {
// your event listener code goes here.
});
});
Note that this code must go after the charts have instantiated to work.
In the absence of a way to attach the callback once globally, one thing you could do to mitigate the risk from duplicate code is to define the callback function once and pass in a reference instead of defining it inline on each chart.
function my_func() {
// do stuff
}
dc.pieChart(myDomId2)
.dimension(dimBeta)
.group(groupBeta)
.on("filtered", my_func);
chart and filter can also be passed to the filter function something like:
function my_func(chart,filter) {
// do stuff
}
dc.pieChart(myDomId2)
.dimension(dimBeta)
.group(groupBeta)
.on("filtered", my_func);
I have these functions to change my backgrounds but the onClick event only works once or one of the four does not work when page loads.
I know "return false" stops the functions but when I use the code without them, the background loads but returns to the original one.
Someone can tell me why?
window.onload = init;
function init(){
document.getElementById("bg01").onclick = mudaBG01;
function mudaBG01(){
$("body").addClass("bg01");
return false;
}
document.getElementById("bg02").onclick = mudaBG02;
function mudaBG02(){
$("body").addClass("bg02");
return false;
}
document.getElementById("bg03").onclick = mudaBG03;
function mudaBG03(){
$("body").addClass("bg03");
return false;
}
document.getElementById("bg04").onclick = mudaBG04;
function mudaBG04(){
$("body").addClass("bg04");
return false;
}
};
Try this
document.getElementById("bg04").onclick = new Function("mudaBG04()");
and define all function mudaBG04() out side of init() function.
Change
onclick= mudaBG01
to
onclick="mudaBG01()"
Done. I changed my JS functions to:
$("#bg01").click(function(){
$("body").css('background', 'url(../images/bg02.jpg)');
return false;
})
$("#bg02").click(function(){
$("body").css('background', 'url(../images/bg03.jpg)');
return false;
})
$("#bg03").click(function(){
$("body").css('background', 'url(../images/bg07.jpg)');
return false;
})
$("#bg04").click(function(){
$("body").css('background', 'url(../images/bg08.jpg)');
return false;
})
thank you all =]
How can I call a function, or run some code for when any and every Template.rendered event is called in Meteor? (Not just a specific template)
(Is there a way I can do this without overloading meteor's base functions?)
Thanks!
One way is to call another method:
dothis = function() {
// Something
}
Template.hello.rendered = function() {
dothis();
}
Template.hello2.rendered = function() {
dothis();
}
If you have nothing else to do in your rendered you could:
Template.hello2.rendered = dothis;
Also in bulk (will override any other rendered if it is defined before, when it is run):
for(tmpl in Template) {
Template[tmpl].rendered = dothis;
};
(and also if you have defined stuff before you can make it run both callbacks:)
for(tmpl in Template) {
if(Template[tmpl].rendered) {
Template[tmpl].rendered = function() {
var originalfunction = Template[tmpl].rendered;
var result = originalfunction.apply(this);
dothis.apply(this);
return result;
}
}
else
{
Template[tmpl].rendered = dothis;
}
};
public Task Disconnect()
{
var context = new HaiTaxiContainer();
var driver = context.OperatorEmployeeSet.Where(o => o.ConnectionId == Context.ConnectionId).FirstOrDefault();
driver.IsWorking = false;
driver.OperatorWorkingHistory.Add(new OperatorWorkingHistory
{
IsWorking = false,
Time = DateTime.Now
});
return Clients.leave(Context.ConnectionId, DateTime.Now.ToString()); ;
}
if (chat.disconnect!=null){
chat.disconnect(function () {
alert('Server has disconnected');
});
alert('Server disconnect==smt');
}else{
alert('Server disconnect==null');
}
The client chat.disconect is null. ANy ideeas why?
Based on your code it is hard to tell what you are actually doing - e.g., what is chat ? However, I think you should take a look at this related question / answer: https://stackoverflow.com/a/9122242/700926 about "How to determine server disconnection from SignalR client?" - it might be useful.
I have an ASP.NET code-behind page linking several checkboxes to JavaScript methods. I want to make only one JavaScript method to handle them all since they are the same logic, how would I do this?
Code behind page load:
checkBoxShowPrices.Attributes.Add("onclick", "return checkBoxShowPrices_click(event);");
checkBoxShowInventory.Attributes.Add("onclick", "return checkBoxShowInventory_click(event);");
ASPX page JavaScript; obviously they all do the same thing for their assigned checkbox, but I'm thinking this can be reduced to one method:
function checkBoxShowPrices_click(e) {
if (_hasChanged) {
confirm(
'All changes will be lost. Do you wish to continue?',
function(arg) {
if (arg.toUpperCase() == 'YES') {
var checkBox = document.getElementById('<%=checkBoxShowPrices.UniqueID%
>');
checkBox.checked = !checkBox.checked;
eval("<%=base.GetPostBackEventReference(checkBoxShowPrices)%>");
_hasChanged = false;
}
});
return false;
} else {
eval("<%=base.GetPostBackEventReference(checkBoxShowPrices)%>");
}
}
function checkBoxShowInventory_click(e) {
if (_hasChanged) {
confirm(
'All changes will be lost. Do you wish to continue?',
function(arg) {
if (arg.toUpperCase() == 'YES') {
var checkBox = document.getElementById('<%
=checkBoxShowInventory.UniqueID%>');
checkBox.checked = !checkBox.checked;
eval("<%=base.GetPostBackEventReference(checkBoxShowInventory)%>");
_hasChanged = false;
}
});
return false;
} else {
eval("<%=base.GetPostBackEventReference(checkBoxShowInventory)%>");
}
}
Add to the event the checkbox that is raising it:
checkBoxShoPrices.Attributes.Add("onclick", "return checkBox_click(this, event);");
Afterwards in the function you declare it like this:
function checkBoxShowPrices_click(checkbox, e){ ...}
and you have in checkbox the instance you need
You can always write a function that returns a function:
function genF(x, y) {
return function(z) { return x+y*z; };
};
var f1 = genF(1,2);
var f2 = genF(2,3);
f1(5);
f2(5);
That might help in your case, I think. (Your code-paste is hard to read..)