Skip to content

Commit

Permalink
GH-46 Add javacript run before model run
Browse files Browse the repository at this point in the history
  • Loading branch information
david-polak committed Aug 18, 2019
1 parent c686dec commit 559976a
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/runtime/Builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import perf from './builders/widgets/Performance/build'
import spinner from './builders/widgets/Spinner/build'
import model from './builders/widgets/Model/build'
import widget from './builders/widgets/Widget/build'
import javascript from './builders/widgets/Javascript/build'

import api from './builders/api/build'

Expand Down Expand Up @@ -56,6 +57,7 @@ class Builder {
spinner(),
toggle(),
model(),
javascript(),

api(),

Expand Down
11 changes: 8 additions & 3 deletions src/runtime/builders/application/init/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
/* global models */
/* global animateFps */
/* global spinner */
/* global javascript */

function init() {
createjs.Ticker.framerate = animateFps
Expand All @@ -16,11 +17,15 @@ function init() {
initWidgets()
]).then(() => {
resolveValueProviders()
Object.entries(models).forEach(([id, model]) => model.init())
Object.entries(widgets).forEach(([id, widget]) => widget.updateComponent())
Object.entries(models).forEach(([, model]) => model.init())

javascript.onBeforeModelRun.forEach(fn => fn())

Object.entries(models).forEach(([, model]) => model.play())
Object.entries(widgets).forEach(([, widget]) => widget.updateComponent())
spinner.hide()
})
})
}

export default init
export default init
10 changes: 10 additions & 0 deletions src/runtime/builders/widgets/Javascript/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import configuration from './configuration'
import generateTemplate from '@runtime/builders/generateTemplate'

export default () => {
const script = `const javascript = ${generateTemplate(configuration())}`
const html = ''
const css = ''

return { script, html, css }
}
17 changes: 17 additions & 0 deletions src/runtime/builders/widgets/Javascript/configuration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import configureStore from '@src/configureStore'
import { getJavascripts } from '@reducers'

export default () => {
const store = configureStore().store.getState()

let javascript = {
onBeforeModelRun: []
}

Object.entries(getJavascripts(store)).forEach(([, item]) => {
const fn = `function() {${item.javascript}}`
javascript.onBeforeModelRun.push(new Function(`return ${fn}`)())
})

return javascript
}
3 changes: 1 addition & 2 deletions src/runtime/builders/widgets/Model/template/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ export default function init() {

this.instantiate()
this.setup()
this.play()
}
}

0 comments on commit 559976a

Please sign in to comment.