-
Notifications
You must be signed in to change notification settings - Fork 7
Getting Started
Kyle Robinson Young edited this page Jul 11, 2015
·
2 revisions
- In your project folder with a
package.jsonfile, run the command:npm install base-element --saveto install. - Using a module bundler such as Browserify or webpack,
var BaseElement = require('base-element')to use the library. - Bundle your code using a module bundler and include the compiled file with a
<script src="bundle.js"></script>in your HTML.
- Download and extract the latest release from: https://github.com/shama/base-element/releases
- Copy the
dist/base-element.jsfile into your project - Create a
<script src="base-element.js"></script>in your HTML. - Use the library with the global variable:
BaseElement
Create a function that represents your element and inherits the prototype of BaseElement:
function Bear (rootNode) {
// Optionally pass a rootNode on where in the DOM your element will get attached
BaseElement.call(this, rootNode)
}
Bear.prototype = Object.create(BaseElement.prototype)Then add a render method to your element:
Bear.prototype.render = function (typeOfBear) {
// HTML tags are created using: this.html(tag, content)
var vtree = this.html('div.bear', ['Im a ' + typeOfBear + '!'])
// A virtual DOM tree will be returned that represents your HTML
// Once you have finished constructing your HTML tree
// Call this.afterRender() with your tree and return it
return this.afterRender(vtree)
}Now you can create and render your element with:
var grizzly = new Bear(document.body)
grizzly.render('grizzly')Which will create a <div class="bear">Im a grizzly!</div> element in your document.body.