Merge pull request #1634 from pygy/fix-mount-class-factory

Let `m.mount` and `m.route` accept class and factory components.
This commit is contained in:
Leo Horie 2017-02-18 09:46:34 -05:00 committed by GitHub
commit b9639dc485
4 changed files with 69 additions and 5 deletions

View file

@ -10,7 +10,7 @@ module.exports = function(redrawService) {
return
}
if (component.view == null) throw new Error("m.mount(element, component) expects a component, not a vnode")
if (component.view == null && typeof component !== "function") throw new Error("m.mount(element, component) expects a component, not a vnode")
var run = function() {
redrawService.render(root, Vnode(component))

View file

@ -21,11 +21,12 @@ module.exports = function($window, redrawService) {
routeService.defineRoutes(routes, function(payload, params, path) {
var update = lastUpdate = function(routeResolver, comp) {
if (update !== lastUpdate) return
component = comp != null && typeof comp.view === "function" ? comp : "div", attrs = params, currentPath = path, lastUpdate = null
component = comp != null && (typeof comp.view === "function" || typeof comp === "function")? comp : "div"
attrs = params, currentPath = path, lastUpdate = null
render = (routeResolver.render || identity).bind(routeResolver)
run()
}
if (payload.view) update({}, payload)
if (payload.view || typeof payload === "function") update({}, payload)
else {
if (payload.onmatch) {
Promise.resolve(payload.onmatch(params, path)).then(function(resolved) {

View file

@ -32,7 +32,17 @@ o.spec("mount", function() {
o(threw).equals(true)
})
o("renders into `root`", function() {
o("throws on invalid component", function() {
var threw = false
try {
mount(root, {})
} catch (e) {
threw = true
}
o(threw).equals(true)
})
o("renders into `root` (POJO component)", function() {
mount(root, {
view : function() {
return m("div")
@ -42,6 +52,26 @@ o.spec("mount", function() {
o(root.firstChild.nodeName).equals("DIV")
})
o("renders into `root` (class component)", function() {
function Cmp(){}
Cmp.prototype.view = function(){return m("div")}
mount(root, Cmp)
o(root.firstChild.nodeName).equals("DIV")
})
o("renders into `root` factory (factory component)", function() {
mount(root, function(){
return {
view : function() {
return m("div")
}
}
})
o(root.firstChild.nodeName).equals("DIV")
})
o("mounting null unmounts", function() {
mount(root, {
view : function() {

View file

@ -51,7 +51,7 @@ o.spec("route", function() {
o(root.firstChild.nodeName).equals("DIV")
})
o("routed mount points can redraw synchronously (#1275)", function() {
o("routed mount points can redraw synchronously (POJO component)", function() {
var view = o.spy()
$window.location.href = prefix + "/"
@ -65,6 +65,39 @@ o.spec("route", function() {
})
o("routed mount points can redraw synchronously (constructible component)", function() {
var view = o.spy()
var Cmp = function(){}
Cmp.prototype.view = view
$window.location.href = prefix + "/"
route(root, "/", {"/":Cmp})
o(view.callCount).equals(1)
redrawService.redraw()
o(view.callCount).equals(2)
})
o("routed mount points can redraw synchronously (factory component)", function() {
var view = o.spy()
function Cmp() {return {view: view}}
$window.location.href = prefix + "/"
route(root, "/", {"/":Cmp})
o(view.callCount).equals(1)
redrawService.redraw()
o(view.callCount).equals(2)
})
o("default route doesn't break back button", function(done) {
$window.location.href = "http://old.com"
$window.location.href = "http://new.com"