The best way to write javascript

I’m having difficulties in writing javascript in yii view files.

For example:




<?php Yii::app()->clientScript->registerScript('id', '$("#idselect").jqGrid({

        ..lots of option here

    });'); ?>



In the above example, i have to write the script inside registerScript method call. This is annoying, because I lost my IDE formatting ability.

Another example:




<?php $this->widget('zii.widgets.jui.CJuiAutoComplete', array(

                    'name'          =>  'nomorOp'

                    ,'sourceUrl'    =>  $this->createUrl('pengiriman/searchOp')

                    ,'options'      =>  array(

                        'select'        =>  'js:function(event, ui) { 

                            $("#orderPembelianId").val(ui.item.value);

                            

                            return false;

                        }'

                        ,'focus'        =>  'js:function(event, ui) {

                            var start = $(this).val().length;

                            $(this).val(ui.item.label);

                            var end = ui.item.label.length;

                            setCaretSelection(this, start, end);

                            return false;

                        }'

                        ,'change'       =>  'js:function(event, ui) {

                            if(ui.item == undefined) {

                                $(this).val("");

                                $("#orderPembelianId").val("");

                            }

                        }'

                        ,'autoFocus'    =>  true

                    )

                    ,'value'   => ($model->orderPembelian != null ? $model->orderPembelian->nomor : '' )

                )); ?>



As you can see above, I need to insert a javascript function callback in the option. This is also annoying.

Is there any ways to avoid these? Do I really need to use registerScriptFile instead?

What is the best way to write javascript?

I’m sorry if this is a silly question… I’m still new in Yii…

Thanks for the advice…

Check if assigning the js to a PHP variable will work better with your IDE.




$js =

'

  js code

';


or heredoc style


$js = <<< EOD

  js code

EOD;


Yii::app()->clientScript->registerScript('id', $js);



/Tommy

heredoc style came close to what I have in mind. But i’m still missing the formatting ability and the autocomplete ability of my IDE (yeah, i’m a lazy programmer… lol)

BTW, i’m using NetBeans 7.

I’ve actually manage to create a simple extension to somewhat solve this. So now I can write js like this:




<?php $clientScript = Yii::app()->clientScript; ?>

<?php $clientScript->beginScript('pengiriman-grid'); ?>

    <script type="text/javascript">

    $("#list-pengiriman").jqGrid({

        datatype:   "local"

        ,colNames:  ["Tanggal", "Total", " "]

        ,colModel:  [

            {name: "tgl", index: "tgl"}

            ,{name: "total", index: "total"}

            ,{name: "actions", index: "actions"}

        ]

    });

    </script>

    <?php $clientScript->endScript(); ?>



notice that I still use script tag, which will automatically be removed.

here’s my extension code:




class MyClientScript extends CClientScript {

    //put your code here

    

    protected $activeScriptId;

    protected $activeScriptPosition;

    

    public function beginScript($id, $pos = parent::POS_READY) {

        

        $this->activeScriptId = $id;

        $this->activeScriptPosition = $pos;

        

        ob_start();

        ob_implicit_flush(false);

    }

    

    public function endScript() {

        

        $script = ob_get_clean();

                

        $script = strip_tags($script);

        

        parent::registerScript($this->activeScriptId, $script, $this->activeScriptPosition);

        

    }

    

}



what do you think? Am I doing this right?

That’s a good feature!

I’ve modified the code to remove the script tags using a regular expression, because it may occur that other HTML tags are used within a JavaScript string. I also add a parameter for htmlOptions since this is available in the registerScript function. Finally I added some comments.

Cannot post link so copy & past to view code