Files
buuh/mount.js
T
Yoshua Wuyts a7916ec3f9 [major] choo v4 (#352)
* location: change url on location:setLocation

- [ ] don't break hashing
- [ ] allow not changing the url

fixup! move fns around

fixup! add search string

* app.start: clean

* walk: add

* location: update arg calls

* fixup! location: update

* uri-wrap: fix thunking

* router: update to latest sheet-router (#239)

* router: update to latest sheet-router

* tests: fix for latest version

* tests: fix SSR

* tests: fix history

* tests: spruce up

* docs: update example

* examples: update

* deps: bump sheet-router

* 4.0.0-0

* chore(changelog): 4.0.0 (#211)

* feat(api:) arg order (#268)

* s/data, state/state, data/

* feat(api): swap arguments

* fix(href): fix routing (#271)

* feat(http): remove (#269)

* feat(router): enable hash routing (#273)

* deps: fix mount

* 4.0.0-1

* feat(mount): copy {script,link} tags

* 4.0.0-2

* fix(mount): forEach -> for

Lol can't use forEach

* fix(router): use state.location.href (#282)

* fix(mount): use deep node clone

* 4.0.0-3

* fix(deps): remove hash-match

* 4.0.0-4

* fix(mount): return node

* 4.0.0-5

* fix(router): check if a hash is a valid selector (#339)

* 4.0.0-6

* fix(router): pass params on newstate (#343)

* 4.0.0-7

* feat(docs): update for 4.0.0 (#320)

* feat(docs): update for 4.0.0

* docs: update router example in readme (#337)

* chore(changelog): update for v4 (#351)
2016-12-11 19:35:29 +01:00

41 lines
1.4 KiB
JavaScript

// mount.js
const documentReady = require('document-ready')
const assert = require('assert')
const yo = require('yo-yo')
module.exports = mount
// (str, html) -> null
function mount (selector, newTree) {
assert.equal(typeof selector, 'string', 'choo/mount: selector should be a string')
assert.equal(typeof newTree, 'object', 'choo/mount: newTree should be an object')
const done = newTree.done
documentReady(function onReady () {
const _rootNode = document.querySelector(selector)
assert.ok(_rootNode, 'could not query selector: ' + selector)
// copy script tags from the old newTree to the new newTree so
// we can pass a <body> element straight up
if (_rootNode.nodeName === 'BODY') {
const children = _rootNode.childNodes
for (var i = 0; i < children.length; i++) {
if (children[i].nodeName === 'SCRIPT') {
newTree.appendChild(children[i].cloneNode(true))
}
}
}
const newNode = yo.update(_rootNode, newTree)
assert.equal(newNode, _rootNode, 'choo/mount: The DOM node: \n' +
newNode.outerHTML + '\n is not equal to \n' + newTree.outerHTML +
'choo cannot begin diffing.' +
' Make sure the same initial tree is rendered in the browser' +
' as on the server. Check out the choo handbook for more information')
// pass the node we mounted on back into choo
if (done) done(newNode)
})
}