Solved

Knockout & jQuery takes a VERY long time using push...

Posted on 2014-11-28
6
213 Views
Last Modified: 2014-12-02
Hi all,

I am using jQuery and Knockout on my webapp, but when I try to process an array (~400 elements) the request can take a very long time to execute (around 60s!)

JS
AppViewModel.page_model = {
    my_log_entries : ko.observableArray(),
    };

function AjaxReportsMyLog()
{
	var myCommand = [];
	myCommand.cmd = "GetReportMyLog";
	var params = JSON.stringify({LoginName:AppViewModel.user_session.UserDetails.LoginName, SessionID:AppViewModel.user_session.UserDetails.SessionID });
	myCommand.data = params;
	myCommand.onSuccess = function(o)
	{
		AppViewModel.page_model.last_updated(new Date().toLocaleString());
		if(JSON.stringify(AppViewModel.page_model.my_log_entries()) != JSON.stringify(o.ajaxdata.ListOfEntries))
		{
			alert('Starts here...');
			$.each(o.ajaxdata.ListOfEntries, function(key, val)
			{
				AppViewModel.page_model.my_log_entries.push(val);
			});
			alert('60s later, gets here!!');
		}
	}
	myCommand.onError = function(o)
	{
		Utils.Dialog_Server_Error(o.errorThrown);
	}
	Api.Exe(myCommand);
};	

$(document).ready(function()
{
	. . 
	. . 
    AppViewModel.page_model.my_log_entries.removeAll();
	AjaxReportsMyLog();
});

Open in new window


html
<script type="text/html" id="my-log-template">
	<table class="report">
		<tr>
			<th class="mv-log2">Field 1</th>
			<th class="mv-log4">Field 2</th>
			<th class="my-log3">Field 3</th>
		</tr>
		<tbody id="my-log-records" data-bind="foreach: $data">
			<tr class="section">
				<td data-bind="text: $data.Field1"></td>
				<td data-bind="text: $data.Field2"></td>
				<td data-bind="text: $data.Field3"></td>
			</tr>
		</tbody>
	</table>
</script>

Open in new window


It all seems to work just fine, just takes a very long time!!!

Any help would be appreciated
0
Comment
Question by:jatkin
  • 4
  • 2
6 Comments
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 40470873
Might be a silly question but you have called the applyBindings method?
i.e. "ko.applyBindings(AppViewModel.page_model);" ?
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 40470876
Also you've encapsulated your table in a <script> tag? why?  that's not necessary.
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 40470897
I've set up a demo here: http://jsbin.com/remeyi/1/edit?html,js,output

I've had to change some code and add a call to some dummy data (over 400 records: http://jsbin.com/futoze/1.json) .  This runs in about a couple of seconds.  Is there anything more complicated to your actual data than what you see here?
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 4

Author Comment

by:jatkin
ID: 40473442
Hi,
Thanks for the suggestions...
I had applied the bindings.
The SCRIPT tag was inherited from the previous developer :-) It appears that as this form was previously used for multiple reports, they used the ID of the SCRIPT to run the appropriate section. Not needed anymore now so I'll remove it.
I do believe however that I have identified the issue... well, I have fixed the delay anyway...
As the "AppViewModel.page_model.my_log_entries" is an observable object, each time an entry is added to it, it is reevaluated. Looks like this takes some time in an array this time (quite why it is 60s I don't know!)
However, I have applied the 'pausable' extension to knockout:
https://github.com/Areson/Knockout-Extensions/wiki/Knockout.Pausable
and it seems to be resolved. Now takes a fraction of a second to process:

AppViewModel.page_model.my_log_entries.pause();
$.each(o.ajaxdata.ListOfEntries, function (key, val)
{
    AppViewModel.page_model.my_log_entries.push(val);
});
AppViewModel.page_model.my_log_entries.resume();

Open in new window

0
 
LVL 42

Accepted Solution

by:
Rob Jurd, EE MVE earned 500 total points
ID: 40474356
Yes, that does make sense.  Great to hear it's working for you.  

Going forward, If you don't want to use a plugin, you could just write the values to a temporary array, which is what I do anyway.

e.g.
var temp = []; // empty array
$.each(o.ajaxdata.ListOfEntries, function (key, val)
{
    temp.push(val);
});

AppViewModel.page_model.my_log_entries(temp);

Open in new window


Now the my_log_entries observable is only being updated once
0
 
LVL 4

Author Closing Comment

by:jatkin
ID: 40475461
This looks like a much neater way that pausing the observable!
Thanks a lot for the help.
James
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now