0

We are working on a dynamic dashboard which is very similar to twitter's settings panel under our twitter account.As the dashboard has many controls,there is lot of content populated dynamically.So currently this is how we do it.

This is one the menu in my left panel:

<a href="javascript:void(0);" onclick="fnGetFiles();">Files</a>     

This is the function called which gets the HTML:

function fnGetFiles(){
  $('#mainDashboard').html('<div class="rightContainer"><div class="tabLoader"></div></div><div class="clear"></div>');
  $.get(USER_DASHBOARD+'file_share/files.php',function(data){
  $('#mainDashboard').html(data);
});

So we basically attach(append) directly the HTML.But I just went through the twitter dashboard enter image description here
First is the URL handling they do for navigating between the different menus on left(Added image for reference).It just changes on the fly and content is loaded.On inspecting the code I noticed they send html as JSON strings.
I am not exactly sure but does this enhance the performance in any way?Mine is a bit poor on the production and would like to know exactly what frameworks I can utilize to achieve something similar to this(Also,I face AJAX conflicting issues between scripts).I am using PHP,jquery for my application.
I tried to get info from dev.twitter.com,and blog but couldn't find any specific info on this.
Please if possible try to guide me and throw some light on it.
Thank you for your time.

coderunner
  • 925
  • 1
  • 18
  • 33

1 Answers1

1

Your HTML, JavaScript, images and other client-side elements are all static. They should stay that way. You don't need to be creating any elements dynamically within JavaScript.

Your AJAX calls merely need to go off, get the data required to populate your HTML, and add this data to the DOM. JSON / XML is just like a protocol:

Communications protocols have to be agreed upon by the parties involved.

For your JS > PHP > JS, JSON is a great 'protocol' (agreed communication method) to choose because it sets a standard, and you merely use json_encode() / json_decode() in your PHP, and $.parseJSON() if you're using jQuery. Using JSON is just an agreed messaging format that, really as a developer, you should commit to using because it's a 'standard'.

You don't want to be sending any extra data over the wire that you don't need to. Don't send HTML unless you need to. Request and Respond with only the data that you need.

Without going into too much of a rant here, there are many awesome tools out there to help you do things like this. AngularJS, for example, allows you to update the DOM automatically with JSON returned from the server, through the use of statements like ng-repeat. It basically means you don't have to write any custom JavaScript to populate DOM elements with data retrieved from the server (which both your dashboard and my dashboard are currently doing).

As well as this, I'm using websockets, because firing off a load of AJAX requests for real-time data isn't what those AJAX requests are made for. If you only need data every 5 - 10 seconds, fine, but any more often than that and you want some bi-directional technology like websockets, which will require you to learn React PHP and something like Ratchet to implement (when you get it working, however, it's awesome).

First, agree on a 'protocol' that both your client-side and server-side are going to use. Then, only send and receive data that you need, and that which changes. Anything else is static and would only waste bandwidth. Don't pass HTML around. Finally, speed-wise, sending JSON vs. sending plain text - it depends on literally the length of characters you're sending. That's it. Regardless, these gains either way are so insignificant that you don't need to worry about them, unless you're sending HTML, which you aren't. Are you.

Jimbo
  • 25,790
  • 15
  • 86
  • 131
  • I probably got what you are saying.You mean I don't append the HTML,but send in data that the HTML should show!`RIGHT?`.Please correct if I am worng.I cannot go about using angular specifically bcoz we have been using javascript and jquery in a traditional way.So the HTML I want to display dynamically will already be there on my dashboard?Please add a example if possible so that I get a better understanding!Thank you for the details above! – coderunner Nov 18 '13 at 14:28
  • @coderunner Exactly. All your HTML should be there in your template, or in included templates (even if it is hidden). It should already be on your dashboard. The *only* thing your AJAX should do is get the data required to put into these elements and then display them (ie, by removing the 'hidden' attribute). Don't send unnecessary, static data over the wire. Get your data, put it in the hidden elements, then use jQuery to simply `.removeAttr('hidden')` or `.removeClass('hidden')` or whatever you're using to hide the element, or `.fadeIn()` for example. – Jimbo Nov 18 '13 at 14:35
  • I am not using any template engine and following a in house framework.So in my case the `dashboard.php`(for example) would be too big with HTML hidden `divs`.Is that the right way?Wouldn't it impact the performance loading that particular page? – coderunner Nov 18 '13 at 14:50
  • Hidden divs containing HTML is going to make zero difference to your loading times. It'll take longer to load the jQuery library. Focus on the user experience of it flowing smoothly, not on microseconds of difference. – Jimbo Nov 18 '13 at 14:51
  • But,when I go ahead and view other dashboards or twitter for that matter,do they specifically use any framework for it? Also,I had a question of managing `URL's` of the menus on the panel that we have.How does that work in co-ordination? – coderunner Nov 18 '13 at 15:12
  • Just because twitter does it that way does not mean it's right. Go look at google's markup - at one point recently they were using `
    ` tags which have been deprecated for a long time. Not sure about your latter question. Btw, they use the client-side framework twitter bootstrap. It's awesome and really simple to use - definitely check that out for any sort of dashboard, and google "bootstrap admin themes" to see other dashboards and how they work.
    – Jimbo Nov 18 '13 at 15:59
  • No I just took it as a example as it already hosts many users and there architecture can be a good way to understand how they use technology at that scale.Thank you for your help.Really helpful and appreciated! – coderunner Nov 18 '13 at 17:29
  • @coderunner No worries, if you find it to answer your question, could you 'tick' it as correct? Thanks. – Jimbo Nov 19 '13 at 09:55
  • Yes I will definitely,just hoped there would be some more comments,before I close the thread,hence the delay! – coderunner Nov 19 '13 at 10:13
  • @coderunner Could you mark this as correct? Pretty sure it covers a lot and is helpful.. – Jimbo Dec 01 '13 at 13:26