-
-
Notifications
You must be signed in to change notification settings - Fork 924
Helpers and Extensions
pelonpelon edited this page Jul 1, 2015
·
24 revisions
A collection of extensions to vanilla Mithril that you might find useful.
Helpers that can make your view more succinct.
m.withValue = function (callback) {
return m.withAttr('value', callback)
}
Helpers that deal with components.
This helper allows you to create an instance of a component and manage it yourself. Useful for when you want explicit control over your component's lifecycle, e.g. you want to control when your controller gets initialized.
m.initComponent = function (component, options, content) {
var controller = new component.controller(options)
controller.render = function (options2, content2) {
return component.view(controller, options2 || options, content2 || content)
}
return controller
}
Example use:
var UserList = {
controller: function () { ... }
view: function () { ... }
}
var App = {
controller: function () {
var ctrl = this
ctrl.userList = m.initComponent(UserList, { users: [...] })
},
view: function (ctrl) {
return m('.app', [
m('h1', "My App"),
ctrl.userList.render()
])
}
}
Helpers that deal with AJAX requests and the like.
This simple helper allows you to mock a promise with a successful value. Useful for AJAX cache layers and mocking server responses.
m.deferred.resolve = function (value) {
var deferred = m.deferred()
deferred.resolve(value)
return deferred.promise
}
This simple helper allows you to mock a rejected promise. Useful for mocking server responses.
m.deferred.reject = function (value) {
var deferred = m.deferred()
deferred.reject(value)
return deferred.promise
}