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:10] – [Components in headless scripts] 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 | + | This object have the following functions: |
+ | ^Function^Description^ | ||
+ | |redraw()|Redraw the component.| | ||
+ | |addIOForm(form, | ||
+ | |backendIO(values, | ||
+ | |timedIO(values, callback, polltime, precision)|Send some information to the component backend at a timed interval.| | ||
+ | |removeTimedIO()|Stop sending information to the component backend at a timed interval.| | ||
+ | |||
+ | ==== Communicating between the frontend and backend ==== | ||
+ | |||
+ | Use the '' | ||
+ | |||
+ | === Example === | ||
- | <code js textoutput.js> | + | <code js script.js> |
- | | + | mycomponent.backendIO({event: |
- | // Do your stuff here! | + | |
- | }); | + | |
</ | </ | ||
- | The item variable | + | This will immediately pass the variables to the '' |
+ | |||
+ | <code php component.php> | ||
+ | public function handleIO() : array { | ||
+ | if ($_POST[' | ||
+ | else return [' | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | ==== Passing forms into the backend ==== | ||
+ | |||
+ | Forms can also be passed | ||
+ | |||
+ | When the form is submitted it is directed to the '' | ||
+ | |||
+ | === Example === | ||
+ | |||
+ | <code js script.js> | ||
+ | mycomponent.addIOForm($('# | ||
+ | </ | ||
+ | |||
+ | When the form is submitted (and validates in the frontend), it will be intercepted by the component and the form output will end up in the '' | ||
+ | |||
+ | <code php component.php> | ||
+ | public function handleIO() : array { | ||
+ | $form = new \Platform\Form(); | ||
+ | if (! $form-> | ||
+ | return [' | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | There are some requirements to the return value when handling forms. First you must return a value //status// in your return array. This should be 0 if form validation failed, and 1 if form validation succeeded. If the form failed you should also return all form errors in the // | ||
+ | |||
+ | In addition to this, you are free to return whatever values you want. These will be returned to the functions in javascript. | ||
+ | |||
+ | === Form handling without javascript === | ||
+ | |||
+ | For basic form handling, you can do this without using javascript. | ||
+ | |||
+ | <code php component.php> | ||
+ | class myComponent { | ||
+ | ... | ||
+ | $form = new \Platform\Form(); | ||
+ | $this-> | ||
+ | ... | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | 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> | ||
+ | class MyComponent { | ||
+ | ... | ||
+ | | ||
+ | |||
+ | | ||
+ | if ($_POST[' | ||
+ | // Handle the custom event | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ==== Special return instructions (reserved words) ==== | ||
+ | |||
+ | 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 javascript. | ||
+ | |||
+ | <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 109: | 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 145: | 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.1619644249.txt.gz · Last modified: 2021/04/28 21:10 by sahl