Merge remote-tracking branch 'origin/rewrite' into rewrite
This commit is contained in:
commit
d4e8911c9c
7 changed files with 43 additions and 28 deletions
|
|
@ -139,7 +139,7 @@ var component = {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
m("div", m(component, { fooga : 1 }));
|
m("div", m.component(component, { fooga : 1 }));
|
||||||
```
|
```
|
||||||
|
|
||||||
### `v1.x`
|
### `v1.x`
|
||||||
|
|
|
||||||
48
mithril.js
48
mithril.js
|
|
@ -86,6 +86,7 @@ function changeNS(ns, vnode) {
|
||||||
var m = hyperscript
|
var m = hyperscript
|
||||||
var renderService = function($window) {
|
var renderService = function($window) {
|
||||||
var $doc = $window.document
|
var $doc = $window.document
|
||||||
|
var $emptyFragment = $doc.createDocumentFragment()
|
||||||
var onevent
|
var onevent
|
||||||
function setEventCallback(callback) {return onevent = callback}
|
function setEventCallback(callback) {return onevent = callback}
|
||||||
|
|
||||||
|
|
@ -172,10 +173,13 @@ var renderService = function($window) {
|
||||||
|
|
||||||
initLifecycle(vnode.tag, vnode, hooks)
|
initLifecycle(vnode.tag, vnode, hooks)
|
||||||
vnode.instance = Node.normalize(vnode.tag.view.call(vnode.state, vnode))
|
vnode.instance = Node.normalize(vnode.tag.view.call(vnode.state, vnode))
|
||||||
var element = createNode(vnode.instance, hooks)
|
if (vnode.instance != null) {
|
||||||
vnode.dom = vnode.instance.dom
|
var element = createNode(vnode.instance, hooks)
|
||||||
vnode.domSize = vnode.instance.domSize
|
vnode.dom = vnode.instance.dom
|
||||||
return element
|
vnode.domSize = vnode.instance.domSize
|
||||||
|
return element
|
||||||
|
}
|
||||||
|
else return $emptyFragment
|
||||||
}
|
}
|
||||||
//update
|
//update
|
||||||
function updateNodes(parent, old, vnodes, hooks, nextSibling) {
|
function updateNodes(parent, old, vnodes, hooks, nextSibling) {
|
||||||
|
|
@ -311,9 +315,11 @@ var renderService = function($window) {
|
||||||
function updateComponent(parent, old, vnode, hooks, nextSibling, recycling) {
|
function updateComponent(parent, old, vnode, hooks, nextSibling, recycling) {
|
||||||
vnode.instance = Node.normalize(vnode.tag.view.call(vnode.state, vnode))
|
vnode.instance = Node.normalize(vnode.tag.view.call(vnode.state, vnode))
|
||||||
updateLifecycle(vnode.tag, vnode, hooks, recycling)
|
updateLifecycle(vnode.tag, vnode, hooks, recycling)
|
||||||
updateNode(parent, old.instance, vnode.instance, hooks, nextSibling, recycling)
|
if (vnode.instance != null) {
|
||||||
vnode.dom = vnode.instance.dom
|
updateNode(parent, old.instance, vnode.instance, hooks, nextSibling, recycling)
|
||||||
vnode.domSize = vnode.instance.domSize
|
vnode.dom = vnode.instance.dom
|
||||||
|
vnode.domSize = vnode.instance.domSize
|
||||||
|
}
|
||||||
}
|
}
|
||||||
function isRecyclable(old, vnodes) {
|
function isRecyclable(old, vnodes) {
|
||||||
if (old.pool != null && Math.abs(old.pool.length - vnodes.length) <= Math.abs(old.length - vnodes.length)) {
|
if (old.pool != null && Math.abs(old.pool.length - vnodes.length) <= Math.abs(old.length - vnodes.length)) {
|
||||||
|
|
@ -595,9 +601,9 @@ var buildQueryString = function(object) {
|
||||||
else args.push(encodeURIComponent(key) + (value != null && value !== "" ? "=" + encodeURIComponent(value) : ""))
|
else args.push(encodeURIComponent(key) + (value != null && value !== "" ? "=" + encodeURIComponent(value) : ""))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
m.request = function($window, Promise) {
|
var requestService = function($window, Promise) {
|
||||||
var callbackCount = 0
|
var callbackCount = 0
|
||||||
function ajax(args) {
|
function xhr(args) {
|
||||||
return new Promise(function(resolve, reject) {
|
return new Promise(function(resolve, reject) {
|
||||||
var useBody = args.useBody != null ? args.useBody : args.method !== "GET" && args.method !== "TRACE"
|
var useBody = args.useBody != null ? args.useBody : args.method !== "GET" && args.method !== "TRACE"
|
||||||
|
|
||||||
|
|
@ -632,7 +638,7 @@ m.request = function($window, Promise) {
|
||||||
response[i] = new args.type(response[i])
|
response[i] = new args.type(response[i])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else response = new args.type(response[i])
|
else response = new args.type(response)
|
||||||
}
|
}
|
||||||
|
|
||||||
resolve(response)
|
resolve(response)
|
||||||
|
|
@ -651,21 +657,21 @@ m.request = function($window, Promise) {
|
||||||
}
|
}
|
||||||
function jsonp(args) {
|
function jsonp(args) {
|
||||||
return new Promise(function(resolve, reject) {
|
return new Promise(function(resolve, reject) {
|
||||||
var callbackKey = "_mithril_" + Math.round(Math.random() * 1e16) + "_" + callbackCount++
|
var callbackName = args.callbackName || "_mithril_" + Math.round(Math.random() * 1e16) + "_" + callbackCount++
|
||||||
var script = $window.document.createElement("script")
|
var script = $window.document.createElement("script")
|
||||||
$window[callbackKey] = function(data) {
|
$window[callbackName] = function(data) {
|
||||||
script.parentNode.removeChild(script)
|
script.parentNode.removeChild(script)
|
||||||
resolve(data)
|
resolve(data)
|
||||||
$window[callbackKey] = undefined
|
delete $window[callbackName]
|
||||||
}
|
}
|
||||||
script.onerror = function() {
|
script.onerror = function() {
|
||||||
script.parentNode.removeChild(script)
|
script.parentNode.removeChild(script)
|
||||||
reject(new Error("JSONP request failed"))
|
reject(new Error("JSONP request failed"))
|
||||||
$window[callbackKey] = undefined
|
delete $window[callbackName]
|
||||||
}
|
}
|
||||||
if (args.data == null) args.data = {}
|
if (args.data == null) args.data = {}
|
||||||
args.url = interpolate(args.url, args.data)
|
args.url = interpolate(args.url, args.data)
|
||||||
args.data[args.callbackKey || "callback"] = callbackKey
|
args.data[args.callbackKey || "callback"] = callbackName
|
||||||
script.src = assemble(args.url, args.data)
|
script.src = assemble(args.url, args.data)
|
||||||
$window.document.documentElement.appendChild(script)
|
$window.document.documentElement.appendChild(script)
|
||||||
})
|
})
|
||||||
|
|
@ -697,8 +703,10 @@ m.request = function($window, Promise) {
|
||||||
}
|
}
|
||||||
function extract(xhr) {return xhr.responseText}
|
function extract(xhr) {return xhr.responseText}
|
||||||
|
|
||||||
return {ajax: ajax, jsonp: jsonp}
|
return {xhr: xhr, jsonp: jsonp}
|
||||||
}(window, Promise).ajax
|
}(window, Promise)
|
||||||
|
m.request = requestService.xhr
|
||||||
|
m.jsonp = requestService.jsonp
|
||||||
var parseQueryString = function(string) {
|
var parseQueryString = function(string) {
|
||||||
if (string === "" || string == null) return {}
|
if (string === "" || string == null) return {}
|
||||||
if (string.charAt(0) === "?") string = string.slice(1)
|
if (string.charAt(0) === "?") string = string.slice(1)
|
||||||
|
|
@ -771,7 +779,7 @@ var coreRouter = function($window) {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case "#": return normalize("hash").slice(prefix.length)
|
case "#": return normalize("hash").slice(prefix.length)
|
||||||
case "?": return normalize("search").slice(prefix.length) + normalize("hash")
|
case "?": return normalize("search").slice(prefix.length) + normalize("hash")
|
||||||
default: return normalize("pathname") + normalize("search") + normalize("hash")
|
default: return normalize("pathname").slice(prefix.length) + normalize("search") + normalize("hash")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
function setPath(path, data, options) {
|
function setPath(path, data, options) {
|
||||||
|
|
@ -909,7 +917,9 @@ m.prop = function(store) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
m.withAttr = function(attrName, callback, context) {
|
m.withAttr = function(attrName, callback, context) {
|
||||||
return callback.call(context || this, attrName in e.currentTarget ? e.currentTarget[attrName] : e.currentTarget.getAttribute(attrName))
|
return function(e) {
|
||||||
|
return callback.call(context || this, attrName in e.currentTarget ? e.currentTarget[attrName] : e.currentTarget.getAttribute(attrName))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
m.render = renderService.render
|
m.render = renderService.render
|
||||||
m.redraw = redrawService.publish
|
m.redraw = redrawService.publish
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,8 @@
|
||||||
"name": "mithril",
|
"name": "mithril",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"description": "A framework for building brilliant applications",
|
"description": "A framework for building brilliant applications",
|
||||||
|
"author": "Leo Horie",
|
||||||
|
"license": "MIT",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "node bundler/bundler",
|
"build": "node bundler/bundler",
|
||||||
|
|
@ -9,10 +11,11 @@
|
||||||
"test": "node ospec/bin/ospec",
|
"test": "node ospec/bin/ospec",
|
||||||
"cover": "istanbul cover --print both ospec/bin/ospec"
|
"cover": "istanbul cover --print both ospec/bin/ospec"
|
||||||
},
|
},
|
||||||
"author": "Leo Horie",
|
|
||||||
"license": "MIT",
|
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"eslint": "^2.10.2",
|
"eslint": "^2.10.2",
|
||||||
"istanbul": "^0.4.3"
|
"istanbul": "^0.4.3"
|
||||||
|
},
|
||||||
|
"publishConfig": {
|
||||||
|
"tag": "beta"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -36,7 +36,7 @@ module.exports = function($window) {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case "#": return normalize("hash").slice(prefix.length)
|
case "#": return normalize("hash").slice(prefix.length)
|
||||||
case "?": return normalize("search").slice(prefix.length) + normalize("hash")
|
case "?": return normalize("search").slice(prefix.length) + normalize("hash")
|
||||||
default: return normalize("pathname") + normalize("search") + normalize("hash")
|
default: return normalize("pathname").slice(prefix.length) + normalize("search") + normalize("hash")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ var pushStateMock = require("../../test-utils/pushStateMock")
|
||||||
var Router = require("../../router/router")
|
var Router = require("../../router/router")
|
||||||
|
|
||||||
o.spec("Router.defineRoutes", function() {
|
o.spec("Router.defineRoutes", function() {
|
||||||
void ["#", "?", "", "#!", "?!"].forEach(function(prefix) {
|
void ["#", "?", "", "#!", "?!", "/foo"].forEach(function(prefix) {
|
||||||
o.spec("using prefix `" + prefix + "`", function() {
|
o.spec("using prefix `" + prefix + "`", function() {
|
||||||
var $window, router, onRouteChange, onFail
|
var $window, router, onRouteChange, onFail
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ var pushStateMock = require("../../test-utils/pushStateMock")
|
||||||
var Router = require("../../router/router")
|
var Router = require("../../router/router")
|
||||||
|
|
||||||
o.spec("Router.getPath", function() {
|
o.spec("Router.getPath", function() {
|
||||||
void ["#", "?", "", "#!", "?!"].forEach(function(prefix) {
|
void ["#", "?", "", "#!", "?!", '/foo'].forEach(function(prefix) {
|
||||||
o.spec("using prefix `" + prefix + "`", function() {
|
o.spec("using prefix `" + prefix + "`", function() {
|
||||||
var $window, router, onRouteChange, onFail
|
var $window, router, onRouteChange, onFail
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ var pushStateMock = require("../../test-utils/pushStateMock")
|
||||||
var Router = require("../../router/router")
|
var Router = require("../../router/router")
|
||||||
|
|
||||||
o.spec("Router.setPath", function() {
|
o.spec("Router.setPath", function() {
|
||||||
void ["#", "?", "", "#!", "?!"].forEach(function(prefix) {
|
void ["#", "?", "", "#!", "?!", "/foo"].forEach(function(prefix) {
|
||||||
o.spec("using prefix `" + prefix + "`", function() {
|
o.spec("using prefix `" + prefix + "`", function() {
|
||||||
var $window, router, onRouteChange, onFail
|
var $window, router, onRouteChange, onFail
|
||||||
|
|
||||||
|
|
@ -79,7 +79,9 @@ o.spec("Router.setPath", function() {
|
||||||
router.setPath("/other", null, {replace: false})
|
router.setPath("/other", null, {replace: false})
|
||||||
$window.history.back()
|
$window.history.back()
|
||||||
|
|
||||||
o($window.location.href).equals("http://localhost/" + (prefix ? prefix + "/" : "") + "test")
|
var slash = prefix[0] === "/" ? "" : "/"
|
||||||
|
|
||||||
|
o($window.location.href).equals("http://localhost" + slash + (prefix ? prefix + "/" : "") + "test")
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue