Solved

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

Posted on 2014-11-28
6
210 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
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…

746 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

12 Experts available now in Live!

Get 1:1 Help Now