Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1.  
  2.  
  3. class Engine {
  4. constructor(bootstrap) {
  5. this.globalThis = window.this;
  6. this.app = bootstrap;
  7. this.selector = this.app.selector;
  8. this.dataInit = this.app.data.bind(this);
  9. this.actionsInit = this.app.actions.bind(this);
  10. this.templateInit = this.app.template.bind(this);
  11. this.afterViewLoadedInit = this.app.afterViewLoaded.bind(this);
  12. this.data;
  13. this.actions;
  14. this.afterViewLoaded;
  15. this.initialise();
  16. }
  17.  
  18. setData(dataObject) {
  19. for (const [incKey, incVal] of Object.entries(dataObject)) {
  20. for (const [dataKey] of Object.entries(this.data)) {
  21. if (incKey === dataKey) {
  22. this.data[incKey] = incVal;
  23. }
  24. }
  25. }
  26. this.render();
  27. }
  28.  
  29. initialise() {
  30. this.data = this.dataInit();
  31. this.actions = this.actionsInit();
  32. this.template = this.templateInit();
  33. this.afterViewLoaded = this.afterViewLoadedInit;
  34. this.render();
  35. }
  36.  
  37. render() {
  38. const template = this.templateInit();
  39. const buildTemplate = (template) => {
  40. return new Promise((resolve, reject) => {
  41. if (this.select(!this.app.selector)) {
  42. customElements.define(this.app.selector, class extends HTMLElement {
  43. connectedCallback() {
  44. this.innerHTML = template;
  45. resolve();
  46. }
  47. });
  48. } else {
  49. this.select(this.app.selector).innerHTML = template;
  50. resolve();
  51. }
  52. });
  53. }
  54. buildTemplate(template).then(() => {
  55. this.afterViewLoaded();
  56. });
  57. }
  58.  
  59. select(element) {
  60. return document.querySelector(element);
  61. }
  62. }
  63.  
  64.  
  65. new Engine({
  66.  
  67. selector: 'my-element',
  68.  
  69. data() {
  70. return {
  71. myOutput: 'One',
  72. myOutTwo: 'Two',
  73. myOutThree: 'Three'
  74. }
  75. },
  76.  
  77. actions() {
  78. return {
  79. logInput: (event) => {
  80. this.setData({
  81. myOutput: event
  82. })
  83. }
  84. }
  85. },
  86.  
  87. template() {
  88. return (`<div>
  89. my template<br/>
  90. ${this.data.myOutput}, <br/> ${this.data.myOutTwo}, <br/>${this.data.myOutThree}<br/>
  91. <input type="text" id="abc" value="${this.data.myOutput}"/><br/>
  92. <input type="text" id="efg" /><br/>
  93. <input type="text" id="hij" /><br/>
  94. </div>`);
  95. },
  96.  
  97. afterViewLoaded() {
  98. this.select('#abc').addEventListener('blur', (e) => {
  99. this.setData({
  100. myOutput: event.target.value
  101. })
  102. })
  103.  
  104. this.select('#efg').addEventListener('blur', (e) => {
  105. this.setData({
  106. myOutTwo: event.target.value
  107. })
  108. })
  109.  
  110. this.select('#hij').addEventListener('blur', (e) => {
  111. this.setData({
  112. myOutThree: event.target.value
  113. })
  114. })
  115. }
  116.  
  117. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement