How can I use the action method in the my own Widget class (extend CWidget) ?
currently I have this code:
in widget class
public static function actions()
{
return array(
'mymethod'=>array(
'class'=>'application.extensions.mywidget.MyWidgetAction',
'property1'=>'value1'
);
}
And in my controller class:
public function actions()
{
return array(
'pro2.'=>array(
'class'=>'application.extensions.mywidget.MyWidget',
'mymethod'=>array(
'property1'=>'value1',
));
}
So I can use the Action of my Widget with the url “index.php?r=MyController/pro2.mymethod”
Is that correct?
But how can I pass (not static) values from my Widget to the Action?
The Action has the MyController as controller object and not the Widget.
But I wanted to pass values from my Widget to the CAction object.
I knew that I can pass values via Url via GET but I thought there must be another way.
Or should I use session values for the widget and read them out in the action again to pass my data?
Is it also possible to have action methods in the Widget, so that a function of the widget is called and not the 'run' method, e.g. to return some json data.
Not sure if I fully understood you. I guess you mainly want to pass dynamic parameters from a widget (in the previous request) to a widget action (in the current request). If that is the case, I think you don't have much choice because they are in two different requests, and you should resort to some persistent data storage, such as GET, session or DB.
Sadly there is no other way then Get or Session data.
Hmm if i wanted to pass data to the action object i also must pass a session name of the widget via get to have the possibility to read out the session value of my widget (widget could be used multiple times on a form).
Am i right?
Is the no possibility to call action methods of the widget object? (e.g. ajax call to the widget)
I mean whats the point of the widget class as an action provider if the actions has no connect to the widget ?
Maybe I am also doing something wrong?
Could you post a simple demo of a action used in a widget which acts as an action provider?
If you are using POST to call the action, you may consider CHtml::statefulForm() (check the hangman demo for its usage). That's another way of persistent data storage.
I don't have a demo for widget action at the moment…
Sadly my Action should only return some values of a query via json on a ajax request.
I wanted it to be independent as possible from a controller and modifications to it.
Cause the widget should have most of the code for displaying the widget and its data, depending on the properties set on creation of the widget.
In the Api there is this sentence:
'When a widget uses several actions, you can declare these actions using this method. The widget will then become an action provider, and the actions can be easily imported into a controller. '