JSON

sirkitree - February 10, 2008 - 22:22
Project:Activity
Version:5.x-3.x-dev
Component:Code
Category:feature request
Priority:normal
Assigned:sirkitree
Status:closed
Description

This patch provides menu callbacks for providing the latest activity post in json format.

I've successfully used this to provide a nice way to update the 'activity all' page dynamically through an ajax call using the jquery plug-in attached and the following (needs work) theme override function.

<?php
function phptemplate_activity_page($activities, $table) {
 
drupal_add_js(drupal_get_path('theme', 'garland') .'/js/spy.js', 'theme');
 
drupal_add_js("
    $(document).ready(function() {
      $('table tbody').spy({
        'ajax': '/activity/all/json',
        'push': custom_push,
        'method': 'json',
        'limit': 1,
        'timeout': 7500,
        });
    });
    function custom_push(response) {
      eval(\"var json = \" + response); // convert to JSON
      alert(json);
      // I'm being lazy here, but you get the idea:
      // Build the HTML
      var html = '<tr class=\"even\"><td>' + json.date + '</td><td>' + json.message + '</td></tr>';
      // Prepend the HTML inside the container
      $('table tbody').prepend(html);
    }"
,
 
'inline');
  return
$table;
}
?>

I would like to consider using this as the default 'activity all' theme because it's so flippin' sweet!

AttachmentSize
activity_json.patch2.73 KB
spy.js_.txt4 KB

#1

sirkitree - February 10, 2008 - 22:26
Status:active» patch (code needs review)

#2

sirkitree - February 11, 2008 - 21:35
Status:patch (code needs review)» fixed

I committed the JSON patch by accident actually... dern ide... was trying to commit to svn and it did cvs instead... back to the command line from now on... /sigh

I would still like to explore using this spy as default page view.

#3

Anonymous (not verified) - February 25, 2008 - 21:41
Status:fixed» closed

Automatically closed -- issue fixed for two weeks with no activity.

#4

giggler - March 26, 2008 - 04:12

I ran the patch and got this:

patching file activity.module
Hunk #1 succeeded at 60 (offset 9 lines).
Hunk #2 FAILED at 81.
Hunk #3 succeeded at 569 (offset 51 lines).
1 out of 3 hunks FAILED -- saving rejects to file activity.module.rej

#5

sirkitree - March 26, 2008 - 12:47

That's because it is already applied. hence this ticket being closed.

#6

giggler - March 26, 2008 - 20:35

I installed the most recent and tried the above code and couldn't get it to work, maybe I did it wrong. I'll try again. Is it possible to get views to run spy.js?

http://leftlogic.com/lounge/articles/jquery_spy2

#7

sirkitree - March 26, 2008 - 20:40

Make sure you use the dev version of the module if you want the json functionality. Hence why this issue's version is marked 5.x-3.x-dev

I'm sure you could get views to use spy.js. Help will not be provided here though as this is the activity module's issue queue.

#8

giggler - March 28, 2008 - 06:29

I have 5.x-3.x-dev dated 3-20-08. This is what I did and it shows the activities but it doesn't seem to do the jquery spy fade in thing:

1. put the php function in template.php
2. created a new folder in garland theme called js
3. created a file in js folder called spy.js with the above txt file.

Do you have a working example? I'm not sure what I'm doing wrong. I also don't know what /activity/all/json is pointing to...

Thanks!

#9

d0t101101 - April 30, 2008 - 23:21

This is really cool, can't wait to get this implemented on my site. I suggest expanding on this to add a more a more visually pleasing interface, such as:
http://www.vimeo.com/home/right_now

Their use of action icons, avatars, and thumnbails ties things together well

#10

d0t101101 - May 6, 2008 - 03:39

In case this might help someone else:

I am using the following function in template.php:

function phptemplate_activity_page($activities, $table) {
  if (drupal_get_normal_path($_GET['q']) == 'activity/all') {
    drupal_add_js(drupal_get_path('theme', 'YOURTHEMEDIR') .'/js/spy.js', 'theme');
    drupal_add_js("
      $(document).ready(function() {
        $('table tbody').spy({
          'ajax': '/activity/all/json',
          'push': custom_push,
          'method': 'json',
          'limit': 1,
          'timeout': 5000,
  'fadeLast': 3,
  'fadeInSpeed': '1400'
          });
      });
      function custom_push(response) {
        eval(\"var json = \" + response); // convert to JSON
        var html = '<tr class=\"even\"><td>' + json.date + '</td><td>' + json.message + '</td></tr>';
        $('table tbody').prepend(html);
      }",
    'inline');
  }
    return $table;
 
}

This checks that the path equals /activity/all before it includes the JS for JSON real-time updates to the activity table. I ran into problems with the when using Activity on my user profile pages too - the old function would try to update all activity tables site-wide. This new function allows you to have 'my activity' on the profile pages, and the 'all activity' page refresh via JSON updates.

.

 
 

Drupal is a registered trademark of Dries Buytaert.