Comments (9)
Sounds like we should add webpack support to dna.js.
I've not yet experimented with webpack. How do you access functions? Are functions namespaced off of global
instead of window
?
Possibly somewhat related, a few minutes ago support for Node.js module loading (issue #29) was pushed out in v1.2.2 so that require
can be used like:
var window = require('jsdom').jsdom('<html></html>').defaultView;
var $ = require('jquery')(window);
var dna = require('dna.js')(window, $);
console.log(dna.util.toKebab('accountNumber')); //outputs: "account-number"
Of course, window
is still being used, but it's being passed in after being created by jsdom.
from dna-engine.
Update... I created a sample project to import dna.js using webpack.
Right now I'm encountering the error:
TypeError: __WEBPACK_IMPORTED_MODULE_0_dna_js___default.a.info is not a function. (In '__WEBPACK_IMPORTED_MODULE_0_dna_js___default.a.info()', '__WEBPACK_IMPORTED_MODULE_0_dna_js___default.a.info' is undefined)
Looks like I need to make window
and jQuery
available plus some other stuff:
https://webpack.github.io/docs/shimming-modules.html
from dna-engine.
@dpilafian This is a working dna-webpack starter. https://github.com/santios/dnajs-webpack-starter. Let me know if that helps.
I don't see a good way to expose the class methods to the window. Whats your opinion on doing something like this:
import $ from 'jquery';
import dnaFactory from 'dna.js';
const dna = dnaFactory(window, $);
export default class PageA {
constructor() {
this.addTodo = this.addTodo.bind(this);
}
render() {
/* Add this guy to the api
* dna.registerEvents(template, object)
*/
dna.registerEvents('todo', {
addTodo: this.addTodo
});
dna.clone('todo', { title: 'Todo One' });
dna.clone('todo', { title: 'Todo Two' });
}
addTodo(ev) {
dna.clone('todo', { title: 'new todo' });
}
}
const page = new PageA();
page.render();
from dna-engine.
The above commit should support registering page
:
dna.registerContext('page', page);
...so its member functions can be called:
<button data-click=page.addTodo>Add To Do</button>
Alternatively, a function can be registered by itself:
dna.registerContext('addTodo', page.addTodo);
...and called with:
<button data-click=addTodo>Add To Do</button>
from dna-engine.
Notes:
-
The webpack ProvidePlugin may also solve the problem with something like
plugins: [new webpack.ProvidePlugin({ page: 'page' })]
to effectively makepage
global. Or maybe expose-loader. -
The name
registerContext()
seems reasonable since a function or an entire entry point may be registered. Or mayberegisterEventContext()
should be the name. -
Events might be associated with a specific template or used anywhere even outside of a template.
from dna-engine.
Proposed addition to the API:
dna.registerContext(contextName, contextObjectOrFunction)
Is there a better approach? Can we improve this?
from dna-engine.
I really like the new addition @dpilafian. I also think the name is good. I will test this soon. Thanks!
from dna-engine.
To make it easier to test, the API change has been pushed out with release 1.2.3 (documentation won't be updated until we're sure this works).
from dna-engine.
http://dnajs.org/docs/#setup-webpack
from dna-engine.
Related Issues (20)
- Replace deprecated assert.deepEqual() in spec/package.js HOT 2
- Replace deprecated assert.deepEqual() in spec/utils.js HOT 1
- Switch to trailing comma style for multiline arrays and objects in spec/dom.js HOT 1
- Switch to ES6 object method shorthand in spec/visual.html HOT 3
- Switch to ES6 object method shorthand for the array module in dna.js HOT 2
- Switch to object method shorthand for the pageToken module
- Add singleGroups rule to jshint to catch unnecessary grouping operator
- Switch to ES6 object method shorthand in dna.js
- Add error checking for missing data parameter HOT 3
- Upgrade to ES6 spread operator to merge options HOT 1
- Add support in the dna.events module for focus and blur HOT 1
- Switch to trailing comma style for multiline arrays in: spec/array.js
- Create dna.templateExists() function in dna.js
- Add JSHint rule to disallow var declarations HOT 1
- Switch to ES6 object method shorthand for top-level functions HOT 1
- Replace deprecated assert.deepEqual() in spec/arrays.js HOT 2
- Replace deprecated $.trim() function
- Replace deprecated assert.deepEqual() in spec/dom.js
- Add JSHint rule to disallow invalid space characters
- Switch to trailing comma style for multiline arrays and objects in spec/arrays.js
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dna-engine.