3

I want to fetch extension attributes value in any extension of yii.

Ex Below:

$this->widget('xupload.XUpload', array(
            'url' => Yii::app()->createUrl("site/upload", array("parent_id" => 1)),
            'model' => $model,
            'attribute' => 'file',
            'multiple' => true,
            'options' => array('submit' => 'js:function (){

            }')
        ));  

i want to fetch suppose URL of above extension in submit function of options but how to?

4
  • So, for example, you wanted something like ${url} to put into your js:function... string? And to make it into actual url value by Yii?
    – user133408
    Mar 15, 2013 at 7:41
  • @PeterM- not getting properly.. error generate...can u give me proper syntax for getting value.
    – harsh4u
    Mar 15, 2013 at 7:45
  • You should create a js var with the needed value, or get the value from the widget generated html code (show us).
    – soju
    Mar 15, 2013 at 8:18
  • @harsh4u It's not syntax, I just used this to clarify question, syntax showed in my comment will not work.
    – user133408
    Mar 15, 2013 at 10:39

1 Answer 1

2

It will not be possible to access the url, or any other attribute of an extension, when the extension is being initialized. But we can access the attribute after the widget has been initialized.

When you call $this->widget('extension', array( ... )); the widget is being initialized, so the options passed in the array can't be immediately used within another option. In your case you are trying to access the property url within the property options.

To use the property we have to:

  1. Store a reference to the widget after it has been initialized, like:

    $xwidget = $this->widget('extension', array( ... ));
    
  2. Then use/access a property :

    $xwidget->propertyname;
    

For your exact scenario when you want to pass the property to a js function, you will additionally have to do the following:

  1. Instead of passing an anonymous function to the submit option, pass a function reference:

    'options' => array('submit' => 'submitHandler')
    
  2. Then either register a script with registerScript or use direct html <script> tag, and access the widget's property in it:

    Yii::app()->clientScript->registerScript('script-id','
        function submitHandler() {
            var url = "'. $xwidget->url .'"; // this is how we can insert the property into js
            // ... more code ...
        }',
        CClientScript::POS_END// the position of the script
    );
    
1
  • Harsh4u, I have made a few edits to better explain the solution, be sure to check it out.
    – bool.dev
    Mar 16, 2013 at 4:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.