View.js get block ID

Permalink
Hi,
Im trying to make this.

view.php
<?php            
defined('C5_EXECUTE') or die(_("Access Denied."));
if ($content) {
?>
   <blockquote <?php  if ($cite) { ?> cite="<?php  echo $cite; ?>"<?php  } ?>>
        <p class="showme-fast-<?php echo $blockID; ?>" style="opacity:0;"><?php  echo $content; ?></p>
          <?php 
            if ($name) {
                        echo '<footer class="show-slow-'.$blockID.'" style="opacity:0;">'.$name.', ';
                 if ($url || $urlName) {
                     if ($url && $urlName) {
                           echo '<cite title="'.$urlName.'"><a href="'.$url.'">'.$urlName.'</a></cite>';
                        } elseif ($url) {
                           echo '<cite title="'.$url.'"><a href="'.$url.'">'.$url.'</a></cite>';
                        } elseif ($urlName) {


and then view.js
/* Every time the window is scrolled ... */
$(window).scroll( function(){
var $bID = $obj->getBlockID();
  var $showslow = ".show-slow-" + $bID;
    /* Check the location of each desired element */
    $($showslow).each( function(i){
        var bottom_of_object = $(this).position().top + $(this).outerHeight();
        var bottom_of_window = $(window).scrollTop() + $(window).height();
        /* If the object is completely visible in the window, fade it in */
        if( bottom_of_window > bottom_of_object ){
            $(this).animate({'opacity':'1','zoom':'1'},1500);
        }
    }); 
});

So what is wrong with getting $bID in view.js.
I'm shure that this works:
var $showslow = ".show-slow-150"


Or am I doing whole thing wrong.
Thanks for answers.
-Olli

OlliSavolainen
 
dsfgwegerhreh replied on at Permalink Reply
In your view.js file, you're mixing Javascript and PHP together and you can't have PHP code in .js files anyway when it's got <?php ?> around it.

This means

var $bID = $obj->getBlockID();


is invalid code. Get rid of that.


What you need to do is add this to your controller.php file where on_page_view is a function in your controller class that you override.

public function on_page_view() {
      $v = View::getInstance();
      $v->addHeaderItem('<script type="text/javascript">var bID = $this->bID . '"; /* Rest of code here */</script>');
   }


The "rest of code here" part can access any variable that you create in view.js (in the same directory as view.php) so you don't need too much Javascript inside your controller. Just enough to get the items not accessible in view.js.

Remember that you may have several blocks of the same type in a single webpage and you want to make sure they don't interfere with one another (unless you intend to). on_page_view is executed for every block that's loaded. But view.js is only executed once per page load regardless of how many blocks there are.
OlliSavolainen replied on at Permalink Reply
OlliSavolainen
Thanks for the answer, but this:

"Remember that you may have several blocks of the same type in a single webpage and you want to make sure they don't interfere with one another (unless you intend to). on_page_view is executed for every block that's loaded. But view.js is only executed once per page load regardless of how many blocks there are."

Yes and that´s the problem here. I don´t know how to make it.
How you can get that bID for just that block? Your code gives multiple bID:s... (If there is many blocks)