Solved

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

Posted on 2014-11-28
6
216 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 43

Expert Comment

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

Expert Comment

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

Expert Comment

by:Rob
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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
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 43

Accepted Solution

by:
Rob 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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
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)
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…

809 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