Modules and AJAX
I like to update a modules content with AJAX. So I started by creating the module and gave some arguments to the modules ToString() function. Let me give you an easy example: say I just want to have a colored PanelBox and the color should be variable.
public function ToString($Color) { echo ' < div id="ColorExampleModule" class="Box ColorExampleBox '.$Color.'">'; echo '< a href"#" class="Button HelloKittyPink">< /a>'; echo '< a href"#" class="Button SpongeBobYellow">< /a>'; echo '< /div>'; ... }
1.) How would my plugin have to call the module so that it displays a given color (maybe 'DepressiveViolett')
$ColorExampleModule = new ColorExampleModule ($Sender); $Sender->AddModule($ColorExampleModule); //no place for module arguments here :-/
Do I need a constructor for that? Like new ColorExampleModule ($Sender, $Color)
and then public function __construct($Sender, $Color) {$this->Color = $Color}
?
2.) How could that be called from JavaScript? Would I have to do something like the below to get my ColorExampleModule::ToString($Solor)
output from /myforum/plugin/colorexample/hellokittypink
or is there an easier/more direct way?
public function PluginController_ColorExample_Create($Sender, $Args) { $Color = $Args[0]; $ColorExampleModule = new ColorExampleModule ($Sender); return $ColorExampleModule->ToString($Color); }
Best Answers
-
hgtonight MVP
Create public members/methods on your module that sets whatever params you want.
Create a controller that accepts the params you want and creates the module and returns the rendered module.
Request the controller method as appropriate via JS.
Sample module code:
private $Color; public $Number; public function SetData($col) { $this->Color = $col; } public function ToString() { echo ' < div id="ColorExampleModule" class="Box ColorExampleBox '.$this->Color.'">'; echo '< a href"#" class="Button HelloKittyPink">< /a>'; echo '< a href"#" class="Button SpongeBobYellow">< /a>'; echo $this->Number; echo '< /div>'; }
Sample hook:
public function PluginController_ColorExample_Create($Sender, $Args) { $Color = $Args[0]; $ColorExampleModule = new ColorExampleModule ($Sender); // Use a public method to set the data $ColorExampleModule->SetData($Color); // Set a public member $ColorExampleModule->Number = 12; return $ColorExampleModule->ToString(); }
Check out the Documentation! We are always looking for new content and pull requests.
Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.
3 -
hgtonight MVP
Not really.
For trivial members, making them public and setting the in the
__construct()
method would work wonderfully.The only disadvantage is you have to have public members and understand the potential pitfalls of external direct manipulation. E.g.
$ColorExampleModule->Number = 'this is not a number';
. This means you have to validate the data right before using it as well.Check out the Documentation! We are always looking for new content and pull requests.
Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.
5
Answers
Create public members/methods on your module that sets whatever params you want.
Create a controller that accepts the params you want and creates the module and returns the rendered module.
Request the controller method as appropriate via JS.
Sample module code:
Sample hook:
Search first
Check out the Documentation! We are always looking for new content and pull requests.
Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.
Before I've posted my question I had taken a look at your LatestPostList but didn't quite understood what you have done. Now I do
Ah... LatestPostList, the plugin that started my love affair with Vanilla in the Garden.
Sweet, sweet memories.
P.S. It goes without saying that you should validate the user input before accepting it willy-nilly like I did in the example.
Search first
Check out the Documentation! We are always looking for new content and pull requests.
Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.
My real function only takes integers and boolean, so sanitizing the user input is really easy
I would prefere using
__construct($Sender, $Args = array('Number' => 13)) {$this->Number = (int)$Args['Number']}
. After creating my module with$ColorExampleModule = new ColorExampleModule ($Sender, array('Number' => 5));
I should be able to access my $ColorExampleModule->Number anywhere I want to.Do you see any disadvantages to your solution?
Not really.
For trivial members, making them public and setting the in the
__construct()
method would work wonderfully.The only disadvantage is you have to have public members and understand the potential pitfalls of external direct manipulation. E.g.
$ColorExampleModule->Number = 'this is not a number';
. This means you have to validate the data right before using it as well.Search first
Check out the Documentation! We are always looking for new content and pull requests.
Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.
And the source of inspiration for Hot Threads too! Another point for Griffin-hgtonight!
My shop | About Me