Printing Javascript on the document (in view)

I use jQuery widget factory (jQuery widgets) for my js widgets.

$.widget('cool.someWidget', {

     options: {

         someEvent: null


     // other js code


Normally to run the widget from js you write


    someEvent: function() { ..... }


In Yii I use CJSON::encode to compile all the initialization properties which include the someEvent event.

echo CJSON::encode(array(

    'someEvent' => 'function() {....}',


However due to the conversion (CJSON), it converts the function() {…} to a string so in the document it is written the following


    someEvent: "function() { .... }"


because the someEvent is actually a string when I call the this._trigger(‘someEvent’) it doesn’t run the code.

This problem I have only when I "generate" the view and not with Ajax requests (which I handle differently in the system). Is there some way of making Yii actually write in the document the function withought the quotes?

p.s. My alternative is to bind the event on another statement but I would really like to avoid that :)

$(selector).on('onSomeEvent', .... );

Well, I created a base class for all my jQuery widgets which now has the following function that actually does exactly what is needed. Still if anyone has of a better solution please do share :)


 * Encode js options

 * @param array $data contains all data you want to pass to your js object

 * @param array $jsCode contains all the js functions you want to pass to your js object

 * @return string


public function encodeOpts($data, $jsCode = array()) {

    $rVal = CJSON::encode($data);

    if (empty($jsCode))

        return $rVal;

    foreach($jsCode as $key => $code) {

        $codeEntries[] = "\"{$key}\": {$code}";


    return substr_replace($rVal, ', ' . implode(', ', $codeEntries), -1) . '}';