Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Try Vanilla Forums Cloud product
Vanilla 2.6 is here! It includes security fixes and requires PHP 7.0. We have therefore ALSO released Vanilla 2.5.2 with security patches if you are still on PHP 5.6 to give you additional time to upgrade.

Modules and AJAX

R_JR_J Cheerleader & TroubleshooterMunich Moderator

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);
}

Comments

  • hgtonighthgtonight ∞ · New Moderator

    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();
    }
    

    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.

    R_JperegrineShadowdare
  • R_JR_J Cheerleader & Troubleshooter Munich Moderator

    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 :)

  • hgtonighthgtonight ∞ · New Moderator

    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.

  • R_JR_J Cheerleader & Troubleshooter Munich Moderator

    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?

  • businessdadbusinessdad Stealth contributor MVP

    @hgtonight said:
    Ah... LatestPostList, the plugin that started my love affair with Vanilla in the Garden.

    And the source of inspiration for Hot Threads too! Another point for Griffin-hgtonight!

    R_J
Sign In or Register to comment.