script file registration fails

Hi all,

i just can’t manage to register a simple script file… anybody got a clue what i’m missing?

i have 2 jquery plugins (jhashtable and numberformatter), which i want to register.

i tried to put them in the folder root/js/ and to register via


Yii::app()->clientScript->registerScriptFile(Yii::app()->request->baseUrl.'/js/jhashtable.js', CClientScript::POS_HEAD);

Yii::app()->clientScript->registerScriptFile(Yii::app()->request->baseUrl.'/js/jquery.numberformatter-1.2.1.min.js', CClientScript::POS_HEAD);

no luck.

so i put them in the folder root/protected/components/js/ and registered them like this


$jsPath = Yii::app()->assetManager->publish(Yii::app()->basePath.'/components/js/');

Yii::app()->clientScript->registerScriptFile($jsPath.'/jhashtable.js', CClientScript::POS_HEAD);

Yii::app()->clientScript->registerScriptFile($jsPath.'/jquery.numberformatter-1.2.1.min.js', CClientScript::POS_HEAD);

still no luck. although a folder is created under root/assets/ in which both scripts are found, and this folder is referenced in the <script>-tag, they just don’t work.

if i copy the contents of the js-files and register the source code with registerScript, it works. So there must be something wrong with my use of registerScriptFile, i just can’t figure it out…

any help is appreciated, thanks.

How do you render the view file holding the registrations and where all the registrations are?

hi, thanks for your answer.

these are the relevant parts…

Controller Method:




public function actionEdit()

 {

   	$this->id = Yii::app()->getRequest()->getQuery('id');

   	$this->model = $this->loadModel($this->id);

   	// get a list of uploaded files for the current project

   	$this->files = $this->getFiles($this->id, 'Projekte');

   	$this->render('edit', array('model'=>$this->model));

 }

View:

This is the part of the form holding the input fields which should be formatted




<div class="row">

		<?php echo CHtml::label('Finanzinfos', false); ?>

    	<table class="infotable">

   			<colgroup>

   				<col width="200" />

            	<col />

        	</colgroup>

        	<tr>

   				<td><?php echo $form->labelEx($model,'umsatz'); ?><?php echo $form->error($model,'umsatz'); ?></td>

            	<td><?php echo $form->textField($model,'umsatz',array('size'=>10, 'style'=>'text-align: right;')); ?> &euro;</td>

        	</tr>

        	<tr>

   				<td><?php echo $form->labelEx($model,'ebitda'); ?><?php echo $form->error($model,'ebitda'); ?></td>

            	<td><?php echo $form->textField($model,'ebitda',array('size'=>10, 'style'=>'text-align: right;')); ?> &euro;</td>

        	</tr>

        	<tr>

   				<td><?php echo $form->labelEx($model,'ebit'); ?><?php echo $form->error($model,'ebit'); ?></td>

            	<td><?php echo $form->textField($model,'ebit',array('size'=>10, 'style'=>'text-align: right;')); ?> &euro;</td>

        	</tr>

        	<tr>

   				<td><?php echo $form->labelEx($model,'ebt'); ?><?php echo $form->error($model,'ebt'); ?></td>

            	<td><?php echo $form->textField($model,'ebt',array('size'=>10, 'style'=>'text-align: right;')); ?> &euro;</td>

        	</tr>

        	<tr>

   				<td><?php echo $form->labelEx($model,'kaufpreis'); ?><?php echo $form->error($model,'kaufpreis'); ?></td>

            	<td><?php echo $form->textField($model,'kaufpreis',array('size'=>10, 'style'=>'text-align: right;')); ?> &euro;</td>

        	</tr>

    	</table>

	</div>



this is the section where i register the javascript




	Yii::app()->clientScript->registerScriptFile(Yii::app()->request->baseUrl.'/js/jhashtable.js', CClientScript::POS_HEAD);

	Yii::app()->clientScript->registerScriptFile(Yii::app()->request->baseUrl.'/js/jquery.numberformatter-1.2.1.min.js', CClientScript::POS_HEAD);


	Yii::app()->clientScript->registerScript('numberformat1',

		'$(".infotable input").each(function(){

			var number = $(this).val();

			number = $.parseNumber(number, {format:"#,###.00", locale: "us"});

			number = $.formatNumber(number, {format:"#,###"});

			$(this).val(number);

    	});',

		CClientScript::POS_READY);


	Yii::app()->clientScript->registerScript('numberformat2',

		'$(".infotable input").blur(function(){

			var number = $(this).val();

			number = $.parseNumber(number, {format:"#,###.00", locale: "us"});

   			number = $.formatNumber(number, {format:"#,###"});

			$(this).val(number);

    	});',

		CClientScript::POS_READY);

resulting in this rendered html:

in the <head> section

[html]

<script type="text/javascript" src="/root/projekte/js/jhashtable.js"></script>

<script type="text/javascript" src="/root/projekte/js/jquery.numberformatter-1.2.1.min.js"></script>[/html]

in the <body> section

[html]<script type="text/javascript">

/<![CDATA[/

jQuery(function($) {

$(".infotable input").each(function(){

		var number = &#036;(this).val();


		number = &#036;.parseNumber(number, {format:&quot;#,###.00&quot;, locale: &quot;us&quot;});


		number = &#036;.formatNumber(number, {format:&quot;#,###&quot;});


		&#036;(this).val(number);


    });

$(".infotable input").blur(function(){

		var number = &#036;(this).val();


		number = &#036;.parseNumber(number, {format:&quot;#,###.00&quot;, locale: &quot;us&quot;});


		number = &#036;.formatNumber(number, {format:&quot;#,###&quot;});


		&#036;(this).val(number);


    });

});

/]]>/

</script>[/html]

for me this looks perfectly normal as it should, all the paths are correct, jQuery works in all other places of the application… obvously i’m missing something here.

I may do not understand properly as I see the resulting HTML to be exactly as you want. ?

edit: Oh… you mean to make it work?

exactly… :)

the html is correct, but the js does not work.

Somehow the script files are not found, even though the paths created by the registerScriptFile function are correct…

Have you tried to use firebug to check whether the selector work?

Open firebug console and then type: $(’.infotable input’) check whether it returns the array or not

Maybe it is better to use:




$('#form_id:input').each(function (el) {

  //

});



Instead

Have you verified that this particular view is actually including the core jQuery? I find that often times when I’m adding custom jQuery, it’s easy to overlook explicitly calling




Yii::app()->clientScript->registerCoreScript('jquery');



That was something so obvious I didn’t even mention it but Dana is right. I still think that is something to do with the selectors.

Hi,

thanks for your replies.

i’m really really sorry, it was just my own blindness…

i had a typo in the filename of the hashtable function, it’s not jhashtable but jshashtable. damn, i checked that 5 times and still didn’t see it.

thank you for trying to help! …some things just can’t be helped, like stupidity. ;)