Update partial with Ajax in Rails 3 too Slow - ruby-on-rails

I opened a thread recently at: Update partial with Ajax in Rails 3
Actually, thanks to Wizard of Ogz, everything works but it is very slow when I move the mouse between two items which are far away from each other since XMLHTTPRequest are sent for each item in between...
I thought about aborting the request if the interval between mouseenter and mouseleave is lower than a threshold but it does not help.
I also tried to add a timer to trigger the request close to the "expiration time" so that the request doesn't have the time to be answered if the same interval is too small but without success again.
Here is the code:
var timeStart, timeEnd;
$("div.show_item").mouseenter(function(event){
timeStart = (new Date()).getTime();
var id = $(this).attr('data');
var url = "/item/list?id="+id;
var data = $(this).serialize();
setTimeout(xhr = $.post(url, function(response_data){$("#item_details").html(response_data)}),290);
return false;
}).mouseleave(function(event){
timeEnd = (new Date()).getTime();
if(timeEnd - timeStart < 300)
xhr.abort();
});
Thank you in advance for your help.

Related

Trying to send email from Google Sheet to Gmail. Tab name is Analysis

Need your help and expert guidance as I need my google sheet to send emails every time a condition becomes true in "K" column which is named "Subject" as a header in the tab name "Analysis". Whenever I run the below code. Similarly last 3 lines I get while running the code as errors. Please explain in a simple possible way and not too much technical
function sendEmail(){
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Analysis");
var range = ss.getRange("J2:J35");
range.clear();
var n = ss.getLastRow();
for (var i = 2;i<n+1; i++){
var emailRequired = ss.getRange(i,9).getValue();
var subject = ss.getRange(i,11).getvalue();
var message = ss.getRange(i,12).getvalue();
if (emailRequired=="YES"){
MailApp.sendEmail("ksm272364#gmail.com",subject,message);
ss.getRange(i,10).setvalue("YES");
}
}
}
1:21:51 PM Error
TypeError: ss.getRange(...).getvalue is not a function
sendEmail # Code.gs:8

Script fails when running normally but in debug its fine

I'm developing a google spreadsheet that is automatically requesting information from a site, below is the code. The variable 'tokens' is an array consisting of about 60 different 3 letter unique identifiers. The problem that i have been getting is that the code keeps failing to request all information on the site. Instead it falls back (at random) on the validation part, and fills the array up with "Error!" strings. Sometimes its row 5, then 10-12, then 3, then multiple rows, etc. When i run it in debug mode everythings fine, can't seem to be able to reproduce the problem.
Already tried to place a sleep (100ms) but that fixed nothing. Also looked at the amount of traffic the API accepts (10 requests per second, 1.200 per minute, 100.000 per day) , it shouldn't be a problem.
Runtime is limited so i need it to be as efficient as possible. I'm thinking it is an issue of computational power after i pushed all values in the json request into the 'tokens' array. Is there a way to let the script wait as long as necessary for the changes to be committed?
function newGetOrders() {
var starttime = new Date().getTime().toString();
var refreshTime = new Date();
var tokens = retrieveTopBin();
var sheet = SpreadsheetApp.openById('aaafFzbXXRzSi-eXBu9Xh81Ne2r09vM8rLFkA4fY').getSheetByName("Sheet37");
sheet.getRange('A2:OL101').clear();
for (var i=0; i<tokens.length; i++) {
var request = UrlFetchApp.fetch("https://api.binance.com/api/v1/depth?symbol=" + tokens[i][0] + "BTC", {muteHttpExceptions:true});
var json = JSON.parse(request.getContentText());
tokens[i].push(refreshTime);
Utilities.sleep(100);
for (var k in json.bids) {
tokens[i].push(json.bids[k][0]);
tokens[i].push(json.bids[k][1]);
}
for (var k in json.asks) {
tokens[i].push(json.asks[k][0]);
tokens[i].push(json.asks[k][1]);
}
if (tokens[i].length < 402) {
for (var x=tokens[i].length; x<402; x++) {
tokens[i].push("ERROR!");
}
}
}
sheet.getRange(2, 1, tokens.length, 402).setValues(tokens);
}

Pagination issue in Report Viewer 11 in MVC Application

I am using ReportViewer 11 to display a report inside an iframe (aspx page is loaded into iframe). It displays first page properly. But when I click on next page, it loads the first page again. Even if I type in page number manually into the textbox, it loads first page only. It seems like, it doesn't fire the pagenavigation event in the server side. If I set the currentpage property to 2 in code behind, then also it loads page 1 only no matter what settings I provide.
Searched for this for days. Got no solution.
Any help will be appreciated.
Thanks
Apply this logic
Store the records in container1
Pass the records to container2
print 5 records(or may be more) from container2
when "next" button is pressed print next 5 from container2
and so on.
I dont know whether this will help you, but records from database can be paginated in this way.
I have tried this logic too. But the main problem is that ReportViewer.CurrentPage is 0 always (in my case) even when the "next" button is pressed. So it will again show the container1 records. This is my code:
public void SetReportData(ref ReportViewer objRptVwr, ReportParameter[] rptParam, string moduleName, string reportName, string dsName, System.Data.DataTable dtFinal){
objRptVwr.Reset();
objRptVwr.ProcessingMode = ProcessingMode.Local;
objRptVwr.LocalReport.ReportPath = HttpContext.Current.Server.MapPath("~/ReportViewer/" + moduleName + "/" + reportName + ".rdlc");
if (rptParam != null)
objRptVwr.LocalReport.SetParameters(rptParam);
objRptVwr.ShowPrintButton = true;
objRptVwr.ShowParameterPrompts = true;
objRptVwr.AsyncRendering = false;
objRptVwr.InteractivityPostBackMode = InteractivityPostBackMode.AlwaysSynchronous;
objRptVwr.ShowBackButton = true;
objRptVwr.SizeToReportContent = false;
objRptVwr.PageCountMode = PageCountMode.Actual;
objRptVwr.ShowExportControls = true;
objRptVwr.ShowPageNavigationControls = true;
objRptVwr.ShowToolBar = true;
objRptVwr.ShowWaitControlCancelLink = false;
objRptVwr.ShowZoomControl = true;
objRptVwr.LocalReport.EnableExternalImages = true;
objRptVwr.ExportContentDisposition = ContentDisposition.AlwaysInline;
ReportDataSource reportDataSource = new ReportDataSource(dsName, (System.Data.DataTable)dtFinal);
objRptVwr.LocalReport.DataSources.Clear();
objRptVwr.LocalReport.DataSources.Add(reportDataSource);
objRptVwr.LocalReport.Refresh();
}
What I did that the generated buttons will be according to the list size.
Each buttons(may be li tag) will have a textfield containing value like 0,5,10 and so on dynamically(In case of 5 elements per page).
A button is clicked, the value of that textfield(e.g index) I got and shifted the resulset position to index and print the records upto 5 elements.

onFullSync action, show updated info - DHTMLX Grid + RoR

I have a Ruby on Rails project where I use a DHTMLX Grid.
Is there a way of showing, using the event handler "onFullSync" provided by the grid API, to show updated data?
Let me explain a little better... I know I can do something like:
dp.attachEvent("onFullSync", function(){
alert("update complete");
})
But what I want is something more complex. I want to, after each completed update, alter a div adding the information like this:
Field 2 was updated to XYZ and field 3 was updated to XER on line X
Field 1 was updated to 123 and field 3 was updated to XSD on line Y
Is this possible?
Thanks
There is a onAfterUpdate event that can be used similar to onFullSync
http://docs.dhtmlx.com/api__dataprocessor_onafterupdate_event.html
It will fire after each data saving operation ( if you are saving 5 rows - it will fire 5 times )
Still, info about updated columns will not be available here.
Also, you can try onEditCell event of grid. It fires after changing data in db, but before real saving in database. Here you can get all necessary info - row, column, old value and new value.
http://docs.dhtmlx.com/api__link__dhtmlxtreegrid_oneditcell_event.html
So, what I end up doing was:
After creating the grid I created an array:
var samples = [];
Then, as per #Aquatic suggestion, I added to "onEditCell" event the following line:
samples[samples.length] = grid.cells(rId, 5).getValue();
This allowed me to add to the array the value present on column 5 of the row changed. Then, on "onFullSync" event I hide or show the div created on the view with the messages (I distinguish if it's on row or more changed on the grid).
//Deals with messages after update
dp.attachEvent("onFullSync", function(){
unique_samples = uniq_fast(samples.sort());
if (unique_samples.length == 1){
$('#updated-samples').text("");
$(".messages").show();
$('#updated-samples').text("A seguinte amostra foi actualizada: " + unique_samples[0]);
//to clear the array
samples = [];
} else if (unique_samples.length > 1){
$('#updated-samples').text("");
$(".messages").show();
$('#updated-samples').text("As seguintes amostras foram actualizadas: " + unique_samples.sort().join(", "));
//to clear the array
samples = [];
} else {
$('#updated-samples').text("");
$(".messages").hide();
//to clear the array
samples = [];
}
})
The problem with using "onEditCell" is that everytime a field is changed on that row I get a repeated value on my "samples" array, I I had to remove duplicate from that array. For that I used one of the suggestions at this answer
// remove duplicates in array
function uniq_fast(a) {
var seen = {};
var out = [];
var len = a.length;
var j = 0;
for(var i = 0; i < len; i++) {
var item = a[i];
if(seen[item] !== 1) {
seen[item] = 1;
out[j++] = item;
}
}
return out;
}
Then I have on the beginning of the view, to show the messages:
<div class="alert alert-success messages" id="updated-samples">
And that's it. I could be not the most efficient way but it works for what I wanted. I will leave the question open a few more days to see if any other option appears.

JScript url replacement

Ok here it goes, I'm making a JScirpt for a page so you can press a keyboardbutton to move to the next page. The page URL looks like this; http://example.org/12345 , so what i want my script to do is increase the number by 1 each time you press the button. I think most of the code is right but it wont do anything
function GoThere() {
var url = window.location.pathname;
var ew = 'url'+1
url = eq.replace(location.hostname, location.hostname+ew);
window.location = url;
}
Would be grateful if someone could take a look and try to explain what I have done wrong
//EniM
check that url is an int, and take the quotes off. Might use some cleanup, but:
// strip out the /
var curint = window.location.pathname.replace(/\D/g,'');
// convert string to int
curint = parseInt( curint, 10 );
var nextint = curint + 1;
window.location = 'http://example.org/' + nextint;
Check out the Console in Chrome. You can run JS line by line... just type a function or var and it will print the result. Or set break points under Sources.
i believe your problem relies in this line
var ew = 'url'+1
it should be
var ew = parseInt(url)+1;

Resources