May 18, 2013, 07:52:19 AM

Login with username, password and session length

FORUM POSTING GUIDELINES - Please read and follow them.

Pages: [1]
  Print  
Author Topic: Some javascript help  (Read 2989 times)
Jon Lachlan
Full Member
***
Offline Offline

Posts: 57


« on: June 25, 2012, 10:46:24 PM »

This is going to seem kind of crazy, but I'm trying to create some javascript that will update the hits count of a listing whenever I click on a link. So far, this is what I've coded, but I don't know how to debug it to make it work. (nor do I know much about coding any of this stuff)

Beginning with the theme file, an onclick call to addHits:
Code:
  <span class="jrButton" title="<?php __t("Increase Hits");?>" onclick="jreviews.listing.addHit(this,{'listing_id':'<?php echo $listing['Listing']['listing_id'];?>'})"> hit++ </span>

In jreviews.js, a jQuery call to _hitsAdd:
Code:
addHit: function(element,options)
                {
                    jQuery(element).s2SubmitNoForm('listings','_hitsAdd','data[listing_id]='+options.listing_id);
                },

In everywhere_com_content.php, which calls hitMe:
Code:
     function _hitsAdd()
    {               
        $listing_id = Sanitize::getInt($this->data,'listing_id');
        $res = $this->Listing->hitMe($listing_id);
        if($res['success'])
        {
            return json_encode(array('error'=>false));       
        }
        return $this->ajaxError(s2Messages::submitErrorDb());
    }

In listings_controller.php, where the database is updated:
Code:
  function hitMe($listing_id)
    {
        $listing_id = (int) $listing_id;
        $result = array('success'=>false,'hits'=>null);
        if(!$listing_id) return $result;
       
        $query = "
            SELECT
            Listing.hits
            FROM
            #__content AS Listing
            WHERE
            Listing.id = " . $listing_id
            ;
        $this->_db->setQuery($query);
        $data['Listing']['id'] = $listing_id;
        $data['Listing']['hits'] += $result['hits'];
         
        if($this->store($data,false,array()))
            {
                // Clear cache
                clearCache('', 'views');
                clearCache('', '__data');       
                $result['success'] = true;           
            };
        return $result;
    }

Currently, the error message I'm getting is:
Code:
Uncaught TypeError: Object [object Object] has no method 's2SubmitNoForm' jreviews.js:427
I'm not really expecting a response, but has anyone done a customization of the ajax/javascript code like this?
Logged
Alejandro
Global Moderator
Administrator
Hero Member
*
Offline Offline

Posts: 28332


« Reply #1 on: June 25, 2012, 11:07:17 PM »

You seem to have models and controllers confused. The ajax call goes to a controller/action. Then from there you can call a model if you want to, but it's not mandatory. You need to dumb things down. First make sure your ajax request goes where you need it, then you can do the rest. In your onclick attribute use this:

Code:
jreviews.dispatch({method:'get',type:'html',controller:'listings',action:'hitMe',data:{id:<?php echo $listing['Listing']['listing_id'];?>});

That makes a call to the listings_controller.php, function hitMe. There just echo something to see if it comes out in the ajax response. You can get the listing id in that function with

Code:
$listing_id = Sanitize::getInt($this->params,'id');
Logged

http://www.reviewsforjoomla.com/testimonials Leave a testimonial for JReviews
Jon Lachlan
Full Member
***
Offline Offline

Posts: 57


« Reply #2 on: June 25, 2012, 11:46:12 PM »

Thanks for the helpful feedback. You're right, and I actually mistyped the location of those pieces of code. Still working on getting an ajax response. Even though I'm not getting much in the way of positive results yet, at least I feel like I'm close to grasping what the heck I'm doing as I poke around through this code.

Thanks again.
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Valid XHTML 1.0! Valid CSS!