Comments (12)
What about either using something like dom-delegator
, or data-*
attributes and bubbling?
I really don't know, this isn't my area of expertise.
Maybe @garyb would be interested in this thread too.
from purescript-halogen.
I don't insist on using hooks but switching from on*
attributes. So, any other libs that allow to add multiple handlers and don't force to rerender elements with handlers would be nice.
from purescript-halogen.
Yes, I agree it's not really ideal right now :)
from purescript-halogen.
@cryogenian What are the features you desire? Just that the on*
attributes are not used, and instead, addEventListener
etc., are used for adding these handlers?
The handlers are basically Event -> i
, these can compose nicely since the library can generate multiple i
per single event (so in theory multiple handlers can be supported).
The tricky bit is that these handlers are functions. So if you don't want to remove them, and re-add them, for every re-render, then you have to have some way of knowing when they change. That's function equality. 😱 Maybe there's some clever way to do it or some constraints that can be imposed to make the problem tractable.
from purescript-halogen.
@jdegoes But they don't need to be functions. With the data-*
approach, we can set attributes which basically represent the i
s, have the events bubble and catch them at the root, then pass the event with the input to the handler.
Edit: type safety for different types of events could get tricky though.
from purescript-halogen.
Well, let's figure out how to hide all this complexity as well as provide a nice type-safe API. If at all humanly possible. 😄
from purescript-halogen.
The problem is not in removing/adding listeners, it's acceptable. But in patch
es. When one get diff
from one vdom
with onclick
and this onclick
unchanged he has to know that functions are equal, vdom
just says that it's impossible and put elements with such handlers in patch
. I don't know is there performance issues with addEventListener
but updating DOM is slow. So, I suppose, that hooks will work better.
Bubbling/delegating will work also.
One example of composing:
- Need to trigger file upload dialog
- Define
HTML i -> HTML i
where result has hidden<input type="file" />
- Make listeners on parent click to trigger
input
's click - Compose this listener with parent
click
listeners - After file is processed trigger custom event on parent
from purescript-halogen.
I just made an attempt at using hooks. Maybe I'm missing something, but here is the issue that I'm seeing - we need to attach the hook only on the initial render, which is fine, but the event handler can depend on the current state, so in some cases, it is correct that it be reattached, because the function itself is different (its logic might be equal, but it closes over a different state, so its behavior is different).
See the demo in the example
folder as an example of this issue. Since the event handler closes over the current state, the first argument to AddService
is always 0
.
This seems to cause odd bugs, and for that reason, I'm going to try data
attributes instead.
I'll stick the hooks code which I have in the hooks
branch.
Edit: here is the code: 507459b#diff-4ea58380fc9463e65f7a7d526d41db2aR33
from purescript-halogen.
Ok, I've managed to get something working in the hooks
branch:
I'm still not very pleased with this though - I feel like there must be a better approach.
Right now, I avoid using on*
attributes, which is good, but I still have to re-hook the event handler every time the state changes. I also don't really like the fact that I have to stick the event handler in a property of the node in order to maintain it between calls.
Any ideas?
from purescript-halogen.
Another option is:
- Stick a value of type
i
inside adata-
attribute.i
doesn't depend on the event here, and might stand for "intent" instead of "input". - Let the event bubble to the top level.
- In
runUIEff
, we change the handler function to also take an eventi -> Event -> Eff ...
.
The reason I don't like this approach is because it fails to sufficiently separate the view and handler functions - having the handler know about the events just seems wrong - the events belong in the view and should be sanitized before hitting the handler function.
Again, these are both isomorphic by Yoneda, and I hate to invoke standard MVC type arguments, but it really does feel incorrect.
from purescript-halogen.
Perhaps worth looking at dom-delegator
again? That way you can use ev-*
props instead which I think will behave the way we want.
from purescript-halogen.
👍 Yeah, handler shouldn't know about events.
I do think (probably) that using global listeners and handling the dispatch on the halogen
side may be the correct way to avoid DOM madness (you want to be able to support composable, multiple listeners, some of which are functions defined by the capture of ever-changing state; no way I can see to do that in a robust way with add/remove listener because assuming you don't want to screw with 3rd party hooks, you need a way to know which listeners need to be removed and which added in the current update, which seems intractable for a robust solution).
from purescript-halogen.
Related Issues (20)
- touchenter and touchleave are deprecated
- `tellAll` function is not re-exported in Halogen module
- Enabling `StateT` with `HalogenM` HOT 1
- Export tellAll from Halogen.Query to Halogen HOT 2
- Reading Effects chapter -> Could not match type ResponseFormat String with type AffjaxDriver HOT 4
- Question: how to handle events coming from a js app? HOT 2
- Communication with JS loaded via CDN HOT 2
- Child component gets rendered outside of parent, being moved to bottom of HTML body HOT 6
- `raise` should not be a blocking operation HOT 8
- Discussion: CSS strategy for halogen applications HOT 20
- Question: body-level events HOT 3
- Order of properties matters when using `value` with `min` / `max` for `InputRange` HOT 3
- Change kind of slots to not be `Type`
- `RenderSpec` doc comment still mentions `h` parameter
- Doc: the examples of "An Aff Example: HTTP Requests" give TypesDoNotUnify Error on Halogen v7 HOT 1
- Select Multiple Selected does not work HOT 4
- A bug? Weird behavior of text input fields. HOT 4
- Child component is destroyed while parent component handles output from that child HOT 5
- Array state updates HOT 4
- open and showModal are missing for dialog HOT 6
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 purescript-halogen.