Solved

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

Posted on 2014-11-28
6
214 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

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…
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…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

770 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