Using JavaScript with Yii (XWF/SWF Charts)

I'm trying to load up XML/SWF Charts with Yii and having some troubles. As far as I can tell, the trouble seems to be the order that Yii is loading the script tags in the header.

This is what the resulting source looks like from the header generated by my working test.php script (minus Yii).

<HTML>
<script language="javascript">
/*<![CDATA[*/
AC_FL_RunContent = 0;
DetectFlashVer = 0;
var requiredMajorVersion = 10;
var requiredMinorVersion = 0;
var requiredRevision = 45;
/*]]>*/
</script>
<script src="AC_RunActiveContent.js" language="javascript"></script>
<BODY bgcolor="#FFFFFF">

Now I'm trying to get Yii to accomplish the same thing using this code.

<?php
Yii::app()->clientScript->registerScript('AC_FL_RunContent', 'AC_FL_RunContent = 0;', CClientScript::POS_HEAD);
Yii::app()->clientScript->registerScript('DetectFlashVer', 'DetectFlashVer = 0;', CClientScript::POS_HEAD);
Yii::app()->getClientScript()->registerScriptFile($chartUrl.'/AC_RunActiveContent.js'); 
Yii::app()->clientScript->registerScript('DetectFlashVer', 
'
var requiredMajorVersion = 10;
var requiredMinorVersion = 0;
var requiredRevision = 45;
', CClientScript::POS_HEAD);
?>

And this is what I'm getting

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="language" content="en" />
<link rel="stylesheet" type="text/css" href="/css/main.css" />
<link rel="stylesheet" type="text/css" href="/css/form.css" />
<link rel="stylesheet" type="text/css" href="/css/components.css" />
<link rel="stylesheet" type="text/css" href="/css/pages.css" />
<script type="text/javascript" src="/assets/e5f807c3/AC_RunActiveContent.js"></script>
<script type="text/javascript">
/<![CDATA[/
AC_FL_RunContent = 0;
DetectFlashVer = 0;

var requiredMajorVersion = 10; var requiredMinorVersion = 0; var requiredRevision = 45;

/]]>/ </script> </head> <body>

The issue seems to be the placement of the line that loads AC_RunActiveContent.js. When I move it above the other variables in my test script it breaks the test script. Is there any way to force Yii to write the commands in the order I want them?

UPDATE 8/23/10- SOLVED!!!

With Grey Teardrop's suggestions I was able to get this working. Given I struggled with this for several days now, I thought I'd post the full solution for others.

First, I have XML/SWF charts installed at protected/vendors/Maani

show.php (view containing the chart)

<?php
$chartPath=Yii::getPathOfAlias('application.vendors.Maani.*');
$chartUrl=Yii::app()->getAssetManager()->publish($chartPath);

Yii::app()->clientScript->registerScript('AC_FL_RunContent', 'AC_FL_RunContent = 0;', CClientScript::POS_HEAD); Yii::app()->clientScript->registerScript('DetectFlashVer', 'DetectFlashVer = 0;', CClientScript::POS_HEAD); Yii::app()->clientScript->registerScript('Morestuff', ' var requiredMajorVersion = 10; var requiredMinorVersion = 0; var requiredRevision = 45; ', CClientScript::POS_HEAD); Yii::app()->getClientScript()->registerScriptFile($chartUrl.'/AC_RunActiveContent.js',CClientScript::POS_BEGIN); ?>

<?php $chart = "<chart><chart_type>bar</chart_type><chart_border color='FF0000' /></chart>"; ?>

<script language="JavaScript" type="text/javascript"> /<![CDATA[/ if (AC_FL_RunContent == 0 || DetectFlashVer == 0) { alert("This page requires AC_RunActiveContent.js."); } else { var hasRightVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision); if(hasRightVersion) { AC_FL_RunContent( 'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,45,2', 'width', '400', 'height', '250', 'scale', 'noscale', 'salign', 'TL', 'bgcolor', '#777788', 'wmode', 'opaque', 'movie', 'charts', 'src', '<?php echo $chartUrl; ?>/charts', 'FlashVars', "library_path=<?php echo $chartUrl; ?>/charts_library&xml_data=<?php echo $chart; ?>", 'id', 'my_chart', 'name', 'my_chart', 'menu', 'true', 'allowFullScreen', 'true', 'allowScriptAccess','sameDomain', 'quality', 'high', 'align', 'middle', 'pluginspage', 'http://www.macromedia.com/go/getflashplayer', 'play', 'true', 'devicefont', 'false' ); } else { var alternateContent = 'This content requires the Adobe Flash Player. ' + '<u><a href=http://www.macromedia.com/go/getflash/>Get Flash</a></u>.'; document.write(alternateContent); } } /]]>/

Note the relative path info on src and FlashVars is required.

Answers


As far as I can tell, CClientScript keeps separate order of script blocks registered via registerScript() and scripts registered via registerScriptFile(). But it will always render registerScript() after registerScriptFile(). There are 2 possible ways to accomplish your task:

  1. Place script blocks in CClientScript::POS_HEAD and script links in CClientScript::POS_END.
  2. Inherit your own script manager from CClientScript.

Need Your Help

.net thread monitoring

.net

I want to monitor one thread from another thread.currently looking at threasd.isalive property. If there is any exception in thread still thread.isalive property is true.

overriding devise after_sign_up_path_for not working

ruby-on-rails ruby-on-rails-3 devise ruby-on-rails-3.1

In routes i have the root-path pointing "home#index" but when i try to override that with after_sign_up_path_for keeps redirecting me to the root path when I sign in or sign up. I have tried to pu...