Yii Framework Forum: The best way to write javascript - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

The best way to write javascript Rate Topic: -----

#1 User is offline   Tsunu 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 06-October 11

Posted 06 October 2011 - 03:52 PM

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..
1

#2 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 06 October 2011 - 06:07 PM

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
Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#3 User is offline   Tsunu 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 06-October 11

Posted 06 October 2011 - 10:15 PM

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?
2

#4 User is offline   MGHollander 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 08-July 14

Posted 10 July 2014 - 02:06 PM

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.

gist.github.com/MGHollander/c3f677260f742e6cacce

Cannot post link so copy & past to view code
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

3 User(s) are reading this topic
1 members, 2 guests, 0 anonymous users

  1. vinssss