28
loading...
This website collects cookies to deliver better user experience
<script src='https://yoururl.com/vibe.min.js'></script>
// Or from a CDN
<script src='https://cdn.jsdelivr.net/gh/bretgeek/vibejs@main/vibe.js'></script>
const aComponent = () => {
const template = `<div>Hey, I am a component!</div>`;
return {
template: template,
className: 'myComponent',
}
}
const aRef = $vibe.render(aComponent, {
to: '#app',
className: 'renderedComponent',
});
const myComponent = () => {
const template = `<div>Hey, I am app!</div>`; // Your component's HTML as string template, can pass in variables too;
// Just an example function (optional)
const func = function say() {
console.log('I am app')
};
// init runs automatically if it exists
const init = function init(e) {
console.log('I am an '+e);
// can chain built-in e.$fns DOM methods!
e.$css('cursor: pointer;').$text('I am new text');
};
// Another function but this time we will pass it in as an event handler
const click = function click(e) {
// target may or may not be "this" if template has nested elements
console.log('clicked ' + e.target.tagName );
// In an Event "this" refers to the component and you can access $fn.func()
this.$fn.func(this);
};
// A state object (optional, pass in anything you want).
// After making changes to state, state can be reverted to original saved in component's $origState
const state = {x:1};
// Render uses this return object to add these to the component
return {
template: template,
className: 'myComponent',
init: init,
fn: {
func: func
},
events: {
click: click,
},
state: state
}
}
// somewhere in your html
<div id='app'> </div>
// className, state, events, plugins (like fn of component) etc. can be added here too
const myRef = $vibe.render(myComponent, {
to: '#app',
position: 'append',
className: 'renderedComponent',
});
You can render a component as many times as you want using different reference names.
Components can render other components too (hint: modules/import/export within components).
// Call built-in Vibe methods to change the css and the text etc. (chain-able).
myRef.$css('display: inline-block').$text('Hey hey!');
// Call the internal function you declared in the component
myRef.$fn.func();
// Tack on more events with $on
myRef.$on('click', function(){ console.log('my Text is: '+this.$text()) });
const aDiv = document.querySelectorAll('.myDdiv')[0];
const aDivRef = $vibe.render(aDiv);
// Use it!
console.log('div existing text is ' + aDivRef.$text());
aDivRef.$text('I am new text.');
export const App1 = () => {
const template = `I am an App1 component!`;
return {
template: template,
className: 'App1',
}
}
import {App1} from '/js/App1.js';
const App2 = () => {
const template = `App2 rendered an App1 component!`;
const init = function init(e) { // "e" here is $self and you can also pass in $self here.
// Render App1 to App2
const App1Ref = $vibe.render(App1, {
to: e,
position: 'append',
});
// change App1's text to show its working
App1Ref.$text('App1 component imported to App2');
};// End init
return {
template: template,
init: init,
className: 'App2',
}
}