Yii And Jquery : Beginner Question

I was just recently turned on to yii from a co-worker. The only MVC architecture I have worked with is one that my company had already developed. I have been reading about yii and it seems like the way to go. I have also watched a few videos and read some of the beginner tutorials.

With our MVC at work we use jquery for pretty much every button click. We will always include the code in the view like this:


<div id="listOfStuff"></div>

<div id="button">Click Me</div>


<script>

$('#button).click(function() {

$.ajax({

type: POST,

dataType: json,

url: '/functions/whatever.php',

success: function( returnData ) {

$('#listOfStuff').html( returnData );

}

});

});

</script>

From looking at the forum it seems like this may not be they way to do it with yii. What is the best way to go about doing somehting like this to take advantage of yii?

Hi Gilberg, good choice getting started on Yii Framework.

To add javascript directly on the view you should use function registerScript() (doc: http://www.yiiframework.com/doc/api/1.1/CClientScript#registerScript-detail )

Here is an example:




Yii::app()->clientScript->registerScript('identificator', "

$('#button).click(function() {

$.ajax({

type: POST,

dataType: json,

url: '/functions/whatever.php',

success: function( returnData ) {

$('#listOfStuff').html( returnData );

}

});});

");



Hi Gilberg

There are many ways to do that

One of them is the @sergifm one

You can do it by inline code (as sergifm said), by javascript file and so on,

check also

http://www.yiiframework.com/doc/api/1.1/CClientScript#registerScript-detail

http://www.yiiframework.com/doc/api/1.1/CClientScript#registerScriptFile-detail

In addition there are plenty functionalities to handle all the javascript things :)