component_class
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
component_class [2021/04/28 21:35] – Easy javascript / PHP interaction sahl | component_class [2023/08/15 11:49] (current) – sahl | ||
---|---|---|---|
Line 59: | Line 59: | ||
* The id will match the id set by the '' | * The id will match the id set by the '' | ||
- | * It will add a class // | + | * It will add three data-elements: |
- | * It will add a class // | + | |
- | * It will add up to three data-elements: | + | |
==== Example ==== | ==== Example ==== | ||
<code html output.html> | <code html output.html> | ||
- | < | + | <div id=" |
<span style=" | <span style=" | ||
</ | </ | ||
</ | </ | ||
- | ==== Javascript and CSS handling | + | ==== Javascript and CSS loading |
- | A component will typically need its own css-files and javascript to function properly. These can just be included in the constructor using the general | + | A component will typically need its own css-files and javascript to function properly. These can just be included in the constructor using either |
<code php> | <code php> | ||
Line 79: | Line 77: | ||
public function __construct() { | public function __construct() { | ||
- | | + | |
- | | + | |
parent:: | parent:: | ||
); | ); | ||
Line 87: | Line 85: | ||
</ | </ | ||
- | In order for javascript to work, especially if the page is hyper-dynamic, | + | ==== Using CSS ==== |
- | <code js textoutput.js> | + | Using CSS is easy. Setting the ID of the component is accomplished using the '' |
- | addPlatformComponentHandlerFunction('CLASS_IN_LOWERCASE', function(item) { | + | |
- | // Do your stuff here! | + | ==== Using Javascript ==== |
- | }); | + | |
+ | All components have an associated javascript object named '' | ||
+ | |||
+ | <code js> | ||
+ | var component_object = $('# | ||
</ | </ | ||
- | CLASS_IN_LOWERCASE should be exchanged with the class name of the PHP class in lowercase, so for the class textOutput, it should read like this: | + | This object have the following functions: |
+ | ^Function^Description^ | ||
+ | |redraw()|Redraw | ||
+ | |addIOForm(form, func, failfunc)|Set a form to be posted to the component backend. See below.| | ||
+ | |backendIO(values, func)|Send some information to the component backend (and get an answer). See below.| | ||
+ | |timedIO(values, | ||
+ | |removeTimedIO()|Stop sending information to the component backend at a timed interval.| | ||
- | <code js textoutput.js> | + | ==== Communicating between the frontend and backend ==== |
- | | + | |
- | // Do your stuff here! | + | Use the '' |
- | }); | + | |
+ | === Example === | ||
+ | |||
+ | <code js script.js> | ||
+ | mycomponent.backendIO({event: | ||
</ | </ | ||
- | The item variable passed is a Jquery-selector pointing to the DOM node of the component. | + | This will immediately pass the variables |
- | ===== Easy Javascript / PHP interaction ===== | + | <code php component.php> |
+ | public function handleIO() : array { | ||
+ | if ($_POST[' | ||
+ | else return [' | ||
+ | } | ||
+ | </ | ||
- | Components support easy interaction between Javascript and PHP. This interaction can both take place using forms and also directly from Javascript. | ||
- | In javascript this interaction takes place through two JQuery plugins called '' | + | ==== Passing forms into the backend ==== |
- | In PHP everything is handled by overriding | + | Forms can also be passed into the backend, much like the example above. Here you just use the '' |
- | ==== Easy form interaction ==== | + | When the form is submitted it is directed to the '' |
- | If you have a form, you can get your component to handle it. In javascript do this: | + | === Example === |
<code js script.js> | <code js script.js> | ||
- | mycomponent.componentIOForm($('# | + | mycomponent.addIOForm($('# |
</ | </ | ||
- | // | + | When the form is submitted |
- | + | ||
- | When the form is submitted it will be intercepted by the component and the form output will end up in the '' | + | |
<code php component.php> | <code php component.php> | ||
Line 133: | Line 147: | ||
</ | </ | ||
- | If you want the form to fail, you need to return | + | There are some requirements |
- | Otherwise the array returned from the function | + | In addition to this, you are free to return whatever values you want. These will be returned to the functions in javascript. |
- | ==== Easy general interaction ==== | + | === Form handling without javascript |
- | If you just want to pass some variables to the '' | + | For basic form handling, you can do this without using javascript. |
- | < | + | < |
- | mycomponent.componentIOForm({action: ' | + | class myComponent |
+ | | ||
+ | $form = new \Platform\Form(); // You need to get the appropriate form. | ||
+ | $this-> | ||
+ | ... | ||
+ | | ||
</ | </ | ||
- | This will immediately pass the variables to the '' | + | This will ensure that the form is handled by the component |
+ | |||
+ | ==== Sending events directly to the backend ==== | ||
+ | |||
+ | Typically events are handled using the '' | ||
<code php component.php> | <code php component.php> | ||
- | public function handleIO() | + | class MyComponent { |
- | if ($_POST[' | + | ... |
- | else return [' | + | |
+ | |||
+ | public function handleIO() { | ||
+ | | ||
+ | // Handle the custom event | ||
+ | } | ||
+ | } | ||
} | } | ||
</ | </ | ||
- | The return | + | ==== Special |
+ | |||
+ | When returning an array from the '' | ||
+ | |||
+ | ^Keyword^Description^ | ||
+ | |data|Attach data attributes to the component.| | ||
+ | |destroy|Destroy the component in the DOM| | ||
+ | |properties|Update the component properties in the frontend.| | ||
+ | |redirect / target|Redirect the user to another URL. An optional target can be provided| | ||
+ | |script|Javascript that will be executed in the frontend.| | ||
+ | |trigger|Trigger an event on the component, which will bubble if the component doesn' | ||
+ | |||
+ | === Example === | ||
+ | |||
+ | You can execute generic | ||
+ | |||
+ | <code php> | ||
+ | public function handleIO() { | ||
+ | return [' | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | If you change component properties in the '' | ||
+ | |||
+ | <code php> | ||
+ | public function handleIO() { | ||
+ | return [' | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | If the component is redrawable, the redraw can also be executed by returning true in the // | ||
+ | |||
+ | <code php> | ||
+ | public function handleIO() { | ||
+ | return [' | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ==== Putting it all together ==== | ||
+ | |||
+ | In this example we will make a component which prompts the user for a name and age, and then displays this information afterwards. This is obtained 100 % in PHP: | ||
+ | |||
+ | <code php example.php> | ||
+ | class myExample extends Component { | ||
+ | |||
+ | public $form = null; | ||
+ | |||
+ | public function __construct() { | ||
+ | // Set component properties | ||
+ | $this-> | ||
+ | ' | ||
+ | ' | ||
+ | ]); | ||
+ | |||
+ | // Construct form | ||
+ | $this-> | ||
+ | $this-> | ||
+ | $this-> | ||
+ | $this-> | ||
+ | |||
+ | // Attach form to component | ||
+ | $this-> | ||
+ | |||
+ | // Redirect the resetcomponent event to the backend | ||
+ | $this-> | ||
+ | } | ||
+ | |||
+ | public function renderContent() { | ||
+ | // Check if name is attached to component | ||
+ | if ($this-> | ||
+ | // Display text | ||
+ | echo 'You have entered ' | ||
+ | // Display a link to reset the component | ||
+ | $menuitem = new MenuItem(' | ||
+ | $menuitem-> | ||
+ | } else { | ||
+ | // Display the form | ||
+ | $this-> | ||
+ | } | ||
+ | } | ||
+ | |||
+ | public function handleIO() { | ||
+ | // Check if form is submitted. | ||
+ | if ($this-> | ||
+ | // Validate form | ||
+ | $result = $this-> | ||
+ | // Send error if not validated | ||
+ | if (! $result) return [' | ||
+ | // Get values and transfer to component properties | ||
+ | $values = $this-> | ||
+ | $this-> | ||
+ | $this-> | ||
+ | // Return status, updated properties and a redraw request. | ||
+ | return [' | ||
+ | } | ||
+ | // Check if reset was triggered | ||
+ | if ($_POST[' | ||
+ | // Clear properties | ||
+ | $this-> | ||
+ | $this-> | ||
+ | // Return updated properties and a redraw request. | ||
+ | return [' | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
===== Components in headless scripts ===== | ===== Components in headless scripts ===== | ||
Line 160: | Line 294: | ||
If you use components in headless scripts, which are scripts that doesn' | If you use components in headless scripts, which are scripts that doesn' | ||
- | ===== Component event model ===== | + | ===== Adding to the DOM using javascript |
- | Components are designed to work with the Javascript/ | + | If you load components onto your page using javascript (by loading a PHP script which generate components), you need to execute the '' |
- | ^event^Effect^ | + | <code php> |
- | |redraw|Redraws the component by fetching its content from the server. See below.| | + | var html = SOME HTML CONTAINING COMPONENTS. |
+ | |||
+ | var node = domnode.html(html); | ||
+ | |||
+ | Platform.apply(node); | ||
+ | </ | ||
===== Automatic component redraw ===== | ===== Automatic component redraw ===== | ||
Line 196: | Line 335: | ||
In this case everyone can replay the Ajax redraw request and see the content of the component. | In this case everyone can replay the Ajax redraw request and see the content of the component. | ||
+ | |||
+ | ===== Extending the component javascript class ===== | ||
+ | |||
+ | If you want to extend the javascript object associated with your custom component, this can be done in three easy steps. | ||
+ | |||
+ | ==== 1. Give the component an unique class name ==== | ||
+ | |||
+ | You first need to give your component a unique DOM class name, which is accomplished by overwriting the '' | ||
+ | |||
+ | <code php> | ||
+ | protected static $component_class = ' | ||
+ | </ | ||
+ | |||
+ | ==== 2. Create a new javascript class for your component ==== | ||
+ | |||
+ | Now create a new javascript class for your component. It must extend the Platform.Component class: | ||
+ | |||
+ | <code js> | ||
+ | class MyCustomPlatformComponentClass extends Platform.Component { | ||
+ | |||
+ | // Code goes here | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ==== 3. Associate the DOM class with the javascript class ==== | ||
+ | |||
+ | This is done like this in javascript: | ||
+ | |||
+ | <code js> | ||
+ | Platform.Component.bindClass(' | ||
+ | </ | ||
+ | | ||
+ |
component_class.1619645735.txt.gz · Last modified: 2021/04/28 21:35 by sahl