Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 10th, 2012  |  syntax: None  |  size: 2.25 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Mootools filtering
  2. {"users": [{"friend_id":"62","name":"admin","username":"admin"},{"friend_id":"66","name":"other","username":"other"}],"total": "1","total_online":"1"}
  3.  
  4.     onSuccess: function(f){ /*Periodical function*/                                
  5.         for(var i=0; i< f.users.length;i++){
  6.             if(f.users[i].friend_id){                                                          
  7.                 friends.push(chat.render('add_contact',f.users[i]));
  8.             }
  9.         }
  10.  
  11.         /*  Question here: How can I replace my code below to:
  12.             1. Remove from container(<div id="fc_contacts" class="fc_contacts">) blocks with id="fc_ID" which not exist in JSON string
  13.             2. Add new block to container(<div id="fc_contacts" class="fc_contacts">) if if doesn't exist in container
  14.             3. If friend_id is in container and in the JSON - do nothing.      
  15.         */
  16.  
  17.         $('fc_contacts').empty(); /*Replace this*/
  18.         $('fc_contacts').addContacts(friends.join(''), 'bottom');   /*ELSE - ADD NEW*/ //el.empty();
  19.  
  20.     }
  21.  
  22.  
  23.     Element.implement({
  24.         addContacts: function(html,where){
  25.             return this.grab(new Element('<div>', {
  26.                 'html': html,
  27.                 'class': 'fc_contacts_container'
  28.             }),where);          
  29.         }
  30.     });
  31.  
  32.  
  33.     <div id="fc_contacts" class="fc_contacts">
  34.         <div class="fc_contacts_container">
  35.             <div class="fc_contact clear_fix" id="fc_62">
  36.                 <!--OTHER HTML!->
  37.             </div>
  38.         </div>
  39.         <div class="fc_contacts_container">
  40.             <div class="fc_contact clear_fix" id="fc_66">
  41.                 <!--OTHER HTML!->
  42.             </div>
  43.         </div>
  44.     </div>
  45.        
  46. var ids = [];  
  47. for(var i=0; i<f.users.length;i++){                            
  48. ids.push('fc-' + f.users[i].friend_id);
  49. }
  50. jQuery('#fc_contacts').append(friends.join('')).children().filter(function(i) {
  51.     return ids.indexOf(this.id) === -1;
  52. }).remove();
  53.        
  54. $('fc_contacts').getElements("div.fc_contact").each(function(c) {
  55.     var id = c.get("id");
  56.     if(!f.users.some(function(u){return id == 'fc_'+u.friend_id;}))
  57.         c.destroy();
  58. });
  59. f.users.each(function(u) {
  60.     if(!$('fc_'+u.friend_id))
  61.         $('fc_contacts').addContacts(chat.render('add_contact',u), 'bottom');
  62. });