-
Notifications
You must be signed in to change notification settings - Fork 601
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Recommended view nesting pattern is incompatible with onload #324
Comments
believe you might need cache-element for this to work well - could you give On Tue, Nov 15, 2016 at 1:37 PM Tim Wisniewski notifications@github.com
|
@timwis yeah i'd expect that to work as well. you're probably right, it might be because of how |
Hey @timwis, I've just started playing around with Choo for a week, and I also used the same pattern you described for layouts! I've been contemplating another idea though, and it seems to solve your problem (although, I haven't really put it through the grind to see if it falls apart anywhere else). const choo = require('choo')
const html = require('choo/html')
const app = choo()
app.model({
state: {},
reducers: {
receive: (data, state) => ({})
},
effects: {
fetch: (data, state, send, done) => {
console.log('fetch')
send('receive', done)
}
}
})
const Layout = (view, state, prev, send) => {
return html`
<main onload=${onload}>
<nav class="nav has-shadow">
<div class="container">
<a href="/" class="nav-item is-tab">Home</a>
<a href="/add" class="nav-item is-tab">Add</a>
</div>
</nav>
${view(state, prev, send)}
</main>
`
function onload () {
console.log('onload')
send('fetch')
}
}
const Home = (state, prev, send) => html`<div>Home</div>`
const Add = (state, prev, send) => html`<div>Add</div>`
app.router([
['/', Layout.bind(this, Home)],
['/add', Layout.bind(this, Add)]
])
const tree = app.start()
document.body.appendChild(tree) Instead of returning a function that takes in Side Note, I had to change your router syntax, it was breaking on me. Either way, I'd love to hear what you think, and if this solves the problem, without introducing it's own! I really liked the pattern you first listed, and I kinda like the consistent method definitions in choo. |
@timwis you should use |
nested onload calls is a super hard problem to solve - there's usually a workaround for nested onload calls tho - feel like we should document this at some point, but closing for now |
I've recommended and used this pattern in multiple projects, and I'm just realising that it's incompatible with onload :-/ Hopefully it's just early and I'm missing something here...
The below test case illustrates the concept: a "Layout" view acts as a parent view and houses the common container, such as the nav bar. Since the layout is on every page, it's otherwise a good place to house the "fetch all records" action.
What you'll see is that navigating between "Home" and "Add," the console logs "onload" and "fetch" every time, despite the fact that
Layout
is used for both views. I think the problem has something to do with the fact thatindex.js
(below) executesLayout
twice as a function, so bel doesn't know that they are the same onload functions. Er, not really explaining that well, but I think it has something to do withLayout
being a function.The text was updated successfully, but these errors were encountered: