Skip to content

Commit 80c25e3

Browse files
committed
rename routeresolver methods to {onmatch,view}
expose some piecemeal modules rename internal xhr to request mirror internal stream api to match public api
1 parent 8bb8131 commit 80c25e3

18 files changed

+272
-260
lines changed

api/mount.js

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ module.exports = function(renderer, pubsub) {
1818
run()
1919

2020
if (component === null) {
21-
pubsub.unsubscribe(root.redraw)
2221
delete root.redraw
2322
}
2423
}

api/router.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@ module.exports = function($window, renderer, pubsub) {
99
var route = function(root, defaultRoute, routes) {
1010
var current = {path: null, component: "div"}
1111
var replay = router.defineRoutes(routes, function(payload, args, path, route) {
12-
if (typeof payload.view !== "function") {
13-
if (typeof payload.render !== "function") payload.render = function(vnode) {return vnode}
14-
var use = function(component) {
12+
args.path = path, args.route = route
13+
if (typeof payload.onmatch === "function") {
14+
if (typeof payload.view !== "function") payload.view = function(vnode) {return vnode}
15+
var resolve = function(component) {
1516
current.path = path, current.component = component
16-
renderer.render(root, payload.render(Vnode(component, null, args, undefined, undefined, undefined)))
17+
renderer.render(root, payload.view(Vnode(component, null, args, undefined, undefined, undefined)))
1718
}
18-
if (typeof payload.resolve !== "function") payload.resolve = function() {use(current.component)}
19-
if (path !== current.path) payload.resolve(use, args, path, route)
20-
else use(current.component)
19+
if (typeof payload.onmatch !== "function") payload.onmatch = function() {resolve(current.component)}
20+
if (path !== current.path) payload.onmatch(Vnode(payload, null, args, undefined, undefined, undefined), resolve)
21+
else resolve(current.component)
2122
}
2223
else {
2324
renderer.render(root, Vnode(payload, null, args, undefined, undefined, undefined))

api/tests/test-router.js

+32-32
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ o.spec("route", function() {
8383
})
8484

8585
function init(vnode) {
86-
o(vnode.attrs).deepEquals({})
86+
o(vnode.attrs.foo).equals(undefined)
8787

8888
done()
8989
}
@@ -231,8 +231,8 @@ o.spec("route", function() {
231231
})
232232
})
233233

234-
o("accepts object as payload", function(done) {
235-
var resolveCount = 0
234+
o("accepts RouteResolver", function(done) {
235+
var matchCount = 0
236236
var renderCount = 0
237237
var Component = {
238238
view: function() {
@@ -243,36 +243,36 @@ o.spec("route", function() {
243243
$window.location.href = prefix + "/"
244244
route(root, "/abc", {
245245
"/:id" : {
246-
resolve: function(use, args, path, route) {
247-
resolveCount++
246+
onmatch: function(vnode, resolve) {
247+
matchCount++
248248

249-
o(args).deepEquals({id: "abc"})
250-
o(path).equals("/abc")
251-
o(route).equals("/:id")
249+
o(vnode.attrs.id).equals("abc")
250+
o(vnode.attrs.path).equals("/abc")
251+
o(vnode.attrs.route).equals("/:id")
252252

253-
use(Component)
253+
resolve(Component)
254254
},
255-
render: function(vnode) {
255+
view: function(vnode) {
256256
renderCount++
257257

258-
o(vnode.attrs).deepEquals({id: "abc"})
258+
o(vnode.attrs.id).equals("abc")
259259

260260
return vnode
261261
},
262262
},
263263
})
264264

265265
setTimeout(function() {
266-
o(resolveCount).equals(1)
266+
o(matchCount).equals(1)
267267
o(renderCount).equals(1)
268268
o(root.firstChild.nodeName).equals("DIV")
269269

270270
done()
271271
}, FRAME_BUDGET)
272272
})
273273

274-
o("accepts object without `render` method as payload", function(done) {
275-
var resolveCount = 0
274+
o("accepts RouteResolver without `view` method as payload", function(done) {
275+
var matchCount = 0
276276
var Component = {
277277
view: function() {
278278
return m("div")
@@ -282,28 +282,28 @@ o.spec("route", function() {
282282
$window.location.href = prefix + "/"
283283
route(root, "/abc", {
284284
"/:id" : {
285-
resolve: function(use, args, path, route) {
286-
resolveCount++
285+
onmatch: function(vnode, resolve) {
286+
matchCount++
287287

288-
o(args).deepEquals({id: "abc"})
289-
o(path).equals("/abc")
290-
o(route).equals("/:id")
288+
o(vnode.attrs.id).equals("abc")
289+
o(vnode.attrs.path).equals("/abc")
290+
o(vnode.attrs.route).equals("/:id")
291291

292-
use(Component)
292+
resolve(Component)
293293
},
294294
},
295295
})
296296

297297
setTimeout(function() {
298-
o(resolveCount).equals(1)
298+
o(matchCount).equals(1)
299299

300300
o(root.firstChild.nodeName).equals("DIV")
301301

302302
done()
303303
}, FRAME_BUDGET)
304304
})
305305

306-
o("accepts object without `resolve` method as payload", function(done) {
306+
o("object without `onmatch` method acts as component", function(done) {
307307
var renderCount = 0
308308
var Component = {
309309
view: function() {
@@ -314,10 +314,10 @@ o.spec("route", function() {
314314
$window.location.href = prefix + "/"
315315
route(root, "/abc", {
316316
"/:id" : {
317-
render: function(vnode) {
317+
view: function(vnode) {
318318
renderCount++
319319

320-
o(vnode.attrs).deepEquals({id: "abc"})
320+
o(vnode.attrs.id).equals("abc")
321321

322322
return m(Component)
323323
},
@@ -331,8 +331,8 @@ o.spec("route", function() {
331331
}, FRAME_BUDGET)
332332
})
333333

334-
o("calls resolve and render correct number of times", function(done) {
335-
var resolveCount = 0
334+
o("calls onmatch and view correct number of times", function(done) {
335+
var matchCount = 0
336336
var renderCount = 0
337337
var Component = {
338338
view: function() {
@@ -343,25 +343,25 @@ o.spec("route", function() {
343343
$window.location.href = prefix + "/"
344344
route(root, "/", {
345345
"/" : {
346-
resolve: function(use) {
347-
resolveCount++
348-
use(Component)
346+
onmatch: function(vnode, resolve) {
347+
matchCount++
348+
resolve(Component)
349349
},
350-
render: function(vnode) {
350+
view: function(vnode) {
351351
renderCount++
352352
return vnode
353353
},
354354
},
355355
})
356356

357357
callAsync(function() {
358-
o(resolveCount).equals(1)
358+
o(matchCount).equals(1)
359359
o(renderCount).equals(1)
360360

361361
redraw.publish()
362362

363363
setTimeout(function() {
364-
o(resolveCount).equals(1)
364+
o(matchCount).equals(1)
365365
o(renderCount).equals(2)
366366

367367
done()

docs/components.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,9 @@ m(Header, {
239239

240240
#### Avoid component factories
241241

242-
Component diffing relies on strict equality checking, so you should avoid recreating components. Instead, consume components idiomatically.
242+
If you create a component from within a `view` method (either directly inline or by calling a function that does so), each redraw will have a different clone of the component. When diffing component vnodes, if the component referenced by the new vnode is not strictly equal to the one referenced by the old component, the two are assumed to be different components even if they ultimately run equivalent code. This means components created dynamically via a factory will always be re-created from scratch.
243+
244+
For that reason you should avoid recreating components. Instead, consume components idiomatically.
243245

244246
```javascript
245247
// AVOID

0 commit comments

Comments
 (0)