-
Notifications
You must be signed in to change notification settings - Fork 0
General review. Component parts
RayVector edited this page Dec 15, 2020
·
2 revisions
(MainElement.js)
data = {
text: 'Click me',
newText: 'Hey, I am SJS!',
color: 'green',
childProp: 'Child prop!', // state field for prop to child
font: '35px',
}
template = {
id: 'MainApp',
node: 'div',
content: () => this.data.text, // dynamic binding to state
events: [
{
type: 'click', // HTML DOM Event
name: 'changeText', // which method to use? State his name
isSelf: true, // do event just for this node
},
],
}
methods = {
changeText: () => {
this.changeData({ // dynamic method for changing state
text: this.data.newText,
childProp: 'Hello world', // update component state + child prop
})
},
}
styles = {
color: () => this.data.color, // dynamically binding styles to state
fontSize: () => this.data.font, // too ^
display: 'flex',
flexDirection: 'column',
}
childList = [
{
component: ThirdApp,
props: { // props to ThirdApp component
msg: () => this.data.childProp,
},
emitEvents: { // emit events from ThirdApp component
newEmit: emitData => this.changeData({ text: emitData }),
},
},
]
// or simple way:
childList = [
{ component: ThirdApp}
]
created() {
console.log('created!')
}
mounted() {
console.log('mounted!')
}
import Sjs_el from '../../sjs/element/Sjs_el'
import ThirdApp from './MainElement/ThirdElement'
class MainElement extends Sjs_el {
constructor() {
super()
}
data = {
text: 'Click me',
newText: 'Hey, I am SJS!',
color: 'green',
childProp: 'Child prop!', // state field for prop to child
font: '35px',
}
styles = {
color: () => this.data.color, // dynamically binding styles to state
fontSize: () => this.data.font, // too ^
display: 'flex',
flexDirection: 'column',
}
template = {
id: 'MainApp',
node: 'div',
content: () => this.data.text, // dynamic binding to state
events: [
{
type: 'click', // HTML DOM Event
name: 'changeText', // which method to use? State his name
isSelf: true, // do event just for this node
},
],
}
childList = [
{
component: ThirdApp,
props: { // props to ThirdApp component
msg: () => this.data.childProp,
},
emitEvents: { // emit events from ThirdApp component
newEmit: emitData => this.changeData({ text: emitData }),
},
},
]
methods = {
changeText: () => {
this.changeData({ // dynamic method for changing state
text: this.data.newText,
childProp: 'Hello world', // update component state + child prop
})
},
}
created() {
console.log('created!')
}
mounted() {
console.log('mounted!')
}
}
export default MainElement
(main.js)
import Sjs from '../sjs/Sjs'
import MainElement from './elements/MainElement'
import SecondElement from './elements/SecondElement'
// array of neighbors, you can pass one element or array of elements
new Sjs().init('app', [MainElement, SecondElement])