Comments (24)
Something like this would do.
ready(function () {
for (var sub in subs) {
subs[sub](model, msg, hooks.onError)
}
})
function ready(cb) {
document.readyState === "complete"
? cb()
: document.addEventListener("DOMContentLoaded", cb)
}
from hyperapp.
I agree with @tunnckoCore. Instead of a clunky .start() method, however, you already have a function, app
, which you can call when you are ready.
Thanks @rbiggs! I'm almost done implementing this.
I'll push in a while with a few other minor improvements.
from hyperapp.
@dodekeract Is there any reason why we can't just treat "render app()" as the first sub?
Interesting question. That way you wouldn't even need to detect DOMContentLoaded yourself.
from hyperapp.
They'll call us witches and burn us alive. See magic.
from hyperapp.
Document ready, etc. Ugh! It's complicated. Here's what I came up with to deal with the mess. Handles situations where the document is already ready for different browsers since they can't decide on how to let us now this so unimportant and insignificant condition ;-P:
function ready(callback) {
const readyRE = /complete|loaded|interactive/;
if (readyRE.test(document.readyState) && document.body) {
callback.call(callback);
} else {
document.addEventListener("DOMContentLoaded", function() {
return callback.call(callback);
});
}
}
from hyperapp.
I don't think it should be in the app/framework side. I believe it would be better to expose .start
method or just such. So user will "start" the app when and where he want. That's why I exposed .start()
in gibon router - before calling start it does not listen on any events.
from hyperapp.
@tunnckoCore Having a .start()
method would just lead to my initial question once again. Which event should that be attached to so that subs
don't break?
from hyperapp.
Hi @dodekeract, this is now fixed in the master branch. Subscriptions (not subs anymore) will work even when you run app(...)
inside DOMContentLoaded.
See https://github.com/hyperapp/hyperapp/blob/master/app.js#L8
from hyperapp.
@jbucaran thanks, will open another issue if it doesn't work, but looks good.
from hyperapp.
@dodekeract Thanks, I'll keep this open. Test it out and please close when, and if, the previous behavior is fixed.
from hyperapp.
@jbucaran I can still reproduce this when using yarn add git+ssh://[email protected]/hyperapp/hyperapp.git
. node_modules/hyperapp/app.js
also seems to be the current version.
I updated dodekeract/hyperapp-issue-61 accordingly.
from hyperapp.
@dodekeract You're using the wrong bundle in broken.html, it should be bundle.onload.js.
And as for broken.root.html, the correct code should be:
window.addEventListener('load', () => app({
view: () => html`
<div style=${{
position: 'fixed',
bottom: '8px',
right: '8px',
backgroundColor: 'yellow'
}}>
View
</div>`,
root: document.getElementById('hyperapp')
}))
from hyperapp.
Not broken.html | Not broken.root.html |
---|---|
|
from hyperapp.
@jbucaran Of course I'm using index.js
in both index.html
and broken.html
. That's why one is called broken, because it doesn't reliably work when included in the head without an event listener.
Only thing I'll change is that onload.html
should actually have the script in the head, but that doesn't make any difference.
index.js
-> index.html
+ broken.html
index.root.js
-> broken.root.html
index.onload.js
-> onload.html
Working: index.html
, onload.html
Broken: broken.html
, broken.root.html
from hyperapp.
@dodekeract Why?
from hyperapp.
@jbucaran Because the repo demonstrates the different ways to include hyperapp and every html called broken
is a demo of a way that doesn't work?
from hyperapp.
In that case, the title of this issue is misleading (or rather this issue is now fixed, we should open a new one). Subscriptions do work now when running the app via <head>
.
from hyperapp.
@jbucaran It still crashes in both broken.html
and broken.root.html
.
from hyperapp.
@dodekeract Are you using window.addEventListener("load")
or document.addEventListener("DOMContentLoaded")
to run the app?
from hyperapp.
@jbucaran In onload.html
I'm using that, yes. That's the point of having multiple slightly different files with different ways to include/call hyperapp.
Is it required now to use window.addEventListener
to make this work?
EDIT: I added subscriptions: [_ => alert(1)]
to the repo. Seems that index.html
and onload.html
now work (also execute subs). Other two still crash though.
from hyperapp.
@dodekeract We now understand each other.
At the moment, yes, it's required because when embedding hyperapp in the <head>
, hyperapp's render/patch functions will fail, since the document is not ready yet.
The question is, should we make hyperapp aware of this out of the box? and why?
@tunnckoCore @terkelg @tzellman
from hyperapp.
@jbucaran No matter what the decision is, the behavior should be documented. 🙂
from hyperapp.
@jbucaran Issue can be closed then. I'd suggest to open a new one with docs/question if this should work without window.addEventListener
or not.
Thanks. That's everything I need for the use-case.
from hyperapp.
@dodekeract That sounds like the right thing to do. Can you take care of opening it?
I'll close here then.
from hyperapp.
Related Issues (20)
- A way to insert raw Html HOT 1
- TypeError: can't access property 0, newSubs is null, when setting the state to undefined. HOT 4
- Issue with null-vnodes HOT 1
- prevent rerender node HOT 2
- The dispatch initializer ends in an endless loop on init when dispatching any action HOT 7
- Injected classes gets removed when using object/array to define class props HOT 1
- hyperapp version HOT 3
- Memo Data Gotcha HOT 5
- Confusing doc for actions -> wrapped actions HOT 5
- Passing arguments to init HOT 4
- [Question] Headless mode is still possible? HOT 1
- Destroying a child app HOT 8
- @hyperapp/html: use a Proxy? HOT 9
- Actions returning other Actions HOT 5
- Compile template tag to hyperscript HOT 17
- A challenge to hyperapp community HOT 1
- Has 2.0 been dropped from development? HOT 3
- oldSub[2] is not a function HOT 3
- Cannot read properties of null (reading 'length') HOT 5
- Unlikely Use Case bug in HTML and SVG Packages HOT 9
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from hyperapp.