clean up
This commit is contained in:
parent
9e558c199c
commit
2923104d27
6 changed files with 124 additions and 5 deletions
|
|
@ -1,5 +1,21 @@
|
|||
## Change Log
|
||||
|
||||
---
|
||||
|
||||
[v0.2.0](/mithril/archive/v0.2.0) - improved components
|
||||
|
||||
### News:
|
||||
|
||||
- Mithril modules will be referred to as *components* from now on.
|
||||
- Virtual DOM tree can now contain [components](mithril.component.md)
|
||||
- Components can now be parameterized via `m.component`
|
||||
|
||||
### Deprecations:
|
||||
|
||||
- `m.module` has been renamed `m.mount`. Calling `m.module` will still work, but should be considered deprecated. Rationale: Mithril modules and components are the same thing, therefore from now on, they will be referred to as components, since that name is more descriptive of their purpose, and causes less confusion in the face of ES6 modules.
|
||||
|
||||
---
|
||||
|
||||
[v0.1.34](/mithril/archive/v0.1.34) - maintenance
|
||||
|
||||
### Bug Fixes:
|
||||
|
|
|
|||
|
|
@ -254,10 +254,10 @@ var m = (function app(window, undefined) {
|
|||
if (!data.attrs) data.attrs = {};
|
||||
if (!cached.attrs) cached.attrs = {};
|
||||
|
||||
var dataAttrKeys = Object.keys(data.attrs)
|
||||
var dataAttrKeys = Object.getOwnPropertyNames(data.attrs)
|
||||
var hasKeys = dataAttrKeys.length > ("key" in data.attrs ? 1 : 0)
|
||||
//if an element is different enough from the one in cache, recreate it
|
||||
if (data.tag != cached.tag || dataAttrKeys.join() != Object.keys(cached.attrs).join() || data.attrs.id != cached.attrs.id || data.attrs.key != cached.attrs.key || (m.redraw.strategy() == "all" && cached.configContext && cached.configContext.retain !== true) || (m.redraw.strategy() == "diff" && cached.configContext && cached.configContext.retain === false)) {
|
||||
if (data.tag != cached.tag || dataAttrKeys.join() != Object.getOwnPropertyNames(cached.attrs).join() || data.attrs.id != cached.attrs.id || data.attrs.key != cached.attrs.key || (m.redraw.strategy() == "all" && cached.configContext && cached.configContext.retain !== true) || (m.redraw.strategy() == "diff" && cached.configContext && cached.configContext.retain === false)) {
|
||||
if (cached.nodes.length) clear(cached.nodes);
|
||||
if (cached.configContext && typeof cached.configContext.onunload === FUNCTION) cached.configContext.onunload()
|
||||
if (cached.controllers) {
|
||||
|
|
@ -746,7 +746,7 @@ var m = (function app(window, undefined) {
|
|||
|
||||
// Get all routes and check if there's
|
||||
// an exact match for the current path
|
||||
var keys = Object.keys(router);
|
||||
var keys = Object.getOwnPropertyNames(router);
|
||||
var index = keys.indexOf(path);
|
||||
if(index !== -1){
|
||||
m.mount(root, router[keys [index]]);
|
||||
|
|
|
|||
96
mvc.html
Normal file
96
mvc.html
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
<body></body>
|
||||
<script src="mithril.js"></script>
|
||||
<script>
|
||||
var Reloadable = new function() {
|
||||
var controllers = []
|
||||
var reloadable = function(controller) {
|
||||
return function() {
|
||||
var ctrl = new controller
|
||||
ctrl.onunload = function() {
|
||||
controllers.splice(controllers.indexOf(ctrl), 1)
|
||||
}
|
||||
controllers.push({instance: ctrl, controller: controller})
|
||||
return ctrl
|
||||
}
|
||||
}
|
||||
reloadable.update = function() {
|
||||
controllers.map(function(c) {
|
||||
ctrl = new c.controller
|
||||
for (var i in ctrl) c.instance[i] = ctrl[i]
|
||||
})
|
||||
}
|
||||
return reloadable
|
||||
}
|
||||
|
||||
var saved = []
|
||||
|
||||
var Contact = function(data) {
|
||||
data = data || {}
|
||||
this.id = m.prop(data.id)
|
||||
this.name = m.prop(data.name || "")
|
||||
this.email = m.prop(data.email || "")
|
||||
}
|
||||
Contact.list = function(data) {
|
||||
return m.request({method: "GET", url: "test.json", data: data, type: Contact})
|
||||
.then(function(contacts) {return contacts.concat(saved)})
|
||||
}
|
||||
Contact.create = function(data) {
|
||||
data.id(saved.push(data) + 2)
|
||||
return Contact.list()
|
||||
}
|
||||
|
||||
var ContactsWidget = {
|
||||
controller: function() {
|
||||
},
|
||||
view: function(ctrl) {
|
||||
return [
|
||||
m.module(ContactForm),
|
||||
m.module(ContactList)
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
var ContactForm = {
|
||||
controller: function(args) {
|
||||
var contact = m.prop(new Contact())
|
||||
return {
|
||||
contact: contact,
|
||||
createContact: function() {
|
||||
Contact.create(contact()).then(Reloadable.update)
|
||||
},
|
||||
}
|
||||
},
|
||||
view: function(ctrl) {
|
||||
var contact = ctrl.contact()
|
||||
|
||||
return m("form", [
|
||||
m("label", "Name"),
|
||||
m("input", {oninput: m.withAttr("value", contact.name), value: contact.name()}),
|
||||
|
||||
m("label", "Email"),
|
||||
m("input", {oninput: m.withAttr("value", contact.email), value: contact.email()}),
|
||||
|
||||
m("button[type=button]", {onclick: ctrl.createContact}, "Create")
|
||||
])
|
||||
}
|
||||
}
|
||||
|
||||
var ContactList = {
|
||||
controller: Reloadable(function() {
|
||||
return {contacts: Contact.list()}
|
||||
}),
|
||||
view: function(ctrl) {
|
||||
return m("table", [
|
||||
ctrl.contacts().map(function(contact) {
|
||||
return m("tr", [
|
||||
m("td", contact.id()),
|
||||
m("td", contact.name()),
|
||||
m("td", contact.email())
|
||||
])
|
||||
})
|
||||
])
|
||||
}
|
||||
}
|
||||
|
||||
m.module(document.body, ContactsWidget)
|
||||
</script>
|
||||
8
onerror.html
Normal file
8
onerror.html
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
<body></body>
|
||||
<script src="mithril.js"></script>
|
||||
<script type="text/javascript">
|
||||
m.request({ url: "invalid" }).then(null, function() {console.log(1);})
|
||||
m.deferred.onerror = function() {
|
||||
console.log(2)
|
||||
}
|
||||
</script>
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
<!doctype html>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/es5-shim/4.0.3/es5-shim.min.js"></script>
|
||||
<!--<script src="https://cdnjs.cloudflare.com/ajax/libs/es5-shim/4.0.3/es5-shim.min.js"></script>-->
|
||||
<script src="test.js"></script>
|
||||
<script src="mock.js"></script>
|
||||
<script src="../mithril.js"></script>
|
||||
|
|
|
|||
|
|
@ -1780,7 +1780,6 @@ function testMithril(mock) {
|
|||
test(function() {
|
||||
var root = mock.document.createElement("div")
|
||||
var vdom = m("div.a", {class: undefined})
|
||||
console.log(vdom)
|
||||
m.render(root, vdom)
|
||||
return root.childNodes[0].class == "a"
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue