Our own little Component Base class.
npm install compose-ui/wagon
var Wagon = require('wagon')
Use it to extend your own Objects.
var Widget = Wagon.extend({
initialize: function(){
// this.options has been set already.
}
})
This comes as the second argument to extend()
. It's useful for things that don't live in each instance.
Define an initialization method called initialize
when extending Wagon. This is very much similar to a Backbone.View. It's much like a JavaScript constructor, except we're able to call it when we really need to.
Wagon needs an element to work with. Pretty much to delegate events on it.
You need to instantiate a DOM element as this.el
within your initialize method.
Much like Backbone.Views, we have and events
object to simplify event delegation. All DOM events are supported. Plus:
- A
tap
event is automatically handled onclick
- CSS animation events are automatically shimmed for cross-browser usage.
This is set on the static properties (class properties) of your Wagon inherited component. It's very useful for have triggers to instantiate your component.
var Dialog = Wagon.extend({
//...
}, {
// these are static properties
docEvents: {
'click [data-trigger=dialog]': 'showDialog'
},
showDialog: function(event){
event.preventDefault()
new this(event.target.dataset) // Pass the element's dataset as options
.show() // ... and then show it.
}
})
In the above example, whenever an element with the attribute data-trigger='dialog'
is clicked, it will instantiate a dialog.
- Code & test / test & code
- Refactor
- Pull request
- Win.