Skip to content

Commit

Permalink
Launch todo.setup() from app for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
dpilafian committed Oct 18, 2023
1 parent b53813e commit 7c33be3
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,22 @@ Use `import` statements in your application to pull in the library's CSS and Jav
// Imports
import 'dna-engine/dist/dna-engine.css';
import 'dna-engine';

// Initialization
const dna = globalThis.dna.initGlobal(window);
```

Then use `dna.registerContext(appName, appObject)` to expose your application so its functions can
be used as callbacks from web pages:
```javascript
const myApp = {
doSomething() { ... }
doSomething(elem) {
console.log('myApp.doSomething() was called.');
},
};
dna.registerContext('myApp', myApp);

dna.registerContext('myApp', myApp); //give dna-engine access to your code
```
Now in the HTML you can wire up a button to call the function:
```html
<button data-on-click=myApp.doSomething>Do Something</button>
```

See the example code in [app.js](src/js/app.js).
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"rimraf": "~5.0",
"run-scripts-util": "~1.2",
"style-loader": "~3.3",
"webpack": "~5.88",
"webpack": "~5.89",
"webpack-cli": "~5.1"
}
}
2 changes: 1 addition & 1 deletion src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<header>
<h1>To Do List</h1>
</header>
<main data-on-load=todo.setup>
<main>
<input type=text id=task-title data-on-enter-key=todo.add autofocus>
<button data-on-click=todo.add>Add Task</button>
<div class=tasks>
Expand Down
3 changes: 2 additions & 1 deletion src/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const todo = {
done: false,
};
dna.clone('task', task, { fade: true });
console.log(dna.getModels('task')); //display array of tasks in the js console
},

setup() {
Expand All @@ -31,5 +32,5 @@ const todo = {
};

// Initialization
dna.initGlobal(window);
dna.registerContext('todo', todo); //expose application functions to be callbacks from web page
dna.dom.onReady(todo.setup);

0 comments on commit 7c33be3

Please sign in to comment.