https://htmx.org logo
Join Discord
Powered by
# htmx-general
  • t

    tall-sundown-41732

    03/22/2023, 11:43 AM
    Ok... So that might've been why my other experiment failed πŸ˜‚
  • t

    tall-sundown-41732

    03/22/2023, 11:44 AM
    I just have some working knowledge of htmx and Flask, but I don't know much about web dev overall... So, I have these gaps in my knowledge.
  • g

    gorgeous-airport-54386

    03/22/2023, 11:44 AM
    Copy code
    ts
    app.get('/track/stream', (req, res) => {
        const ticketId = req.query['ticket'] as string
        const ticket = validateTicket(ticketId)
        if (typeof ticket === 'string') return res.render('err.njk', { error: ticket, bare: true })
        
        const tracker = new RailwayTracker(ticketId, trains, routes, tickets)
    
        // Headers as per Server Sent Events spec
        res.setHeader('Cache-Control', 'no-cache')
        res.setHeader('Content-Type',  'text/event-stream')
        res.setHeader('Connection',    'keep-alive')
        res.flushHeaders()
    
        res.write('retry: 8000\n\n')
    
        const onReport = (e: Event) => {
            // @ts-ignore
            const r = e.detail as TrainReport
            console.log('sending')
            res.write('event: report\n' +
                      `data: ${nunjucks.render('track-stream.njk', { report: r })}\n\n`)
        }
    
        tracker.addEventListener('report', onReport)
        tracker.addEventListener('closed', function listener() {
            tracker.removeEventListener('report', onReport)
            tracker.removeEventListener('closed', listener)
            res.end()
        })
    })
  • t

    tall-sundown-41732

    03/22/2023, 11:45 AM
    Thanks.
  • g

    gorgeous-airport-54386

    03/22/2023, 11:46 AM
    One mistake I made is that you need to put
    data:
    before every line of the data. I just rewrote my template to all be in one line since I couldn't understand what was happening :]
  • t

    tall-sundown-41732

    03/22/2023, 11:49 AM
    I see, thanks
  • t

    tall-sundown-41732

    03/22/2023, 11:49 AM
    From looking at your code, I see you have the onReport function which is responsible for sending the events, correct?
  • g

    gorgeous-airport-54386

    03/22/2023, 11:50 AM
    Yep
  • t

    tall-sundown-41732

    03/22/2023, 11:50 AM
    But I don’t understand what actually triggers it.
  • g

    gorgeous-airport-54386

    03/22/2023, 11:50 AM
    See below, the RailwayTracker class emits events
    Copy code
    ts
    tracker.addEventListener('report', onReport)
  • g

    gorgeous-airport-54386

    03/22/2023, 11:51 AM
    You need a way to run some code when you have some more data to send
  • t

    tall-sundown-41732

    03/22/2023, 11:51 AM
    Right
  • t

    tall-sundown-41732

    03/22/2023, 11:51 AM
    The railway tracker emits report events, is that it?
  • g

    gorgeous-airport-54386

    03/22/2023, 11:51 AM
    Yep
  • g

    gorgeous-airport-54386

    03/22/2023, 11:52 AM
    This was a mock app so it just waits a random amount before moving the train a little
  • t

    tall-sundown-41732

    03/22/2023, 11:53 AM
    Right.
  • t

    tall-sundown-41732

    03/22/2023, 11:53 AM
    Well, this was helpful.
  • t

    tall-sundown-41732

    03/22/2023, 11:53 AM
    I might be able to get things to work, now! πŸ˜† I appreciate your time.
  • h

    happy-knife-63802

    03/22/2023, 1:49 PM
    im still having trouble with sortable not working after swapping in new content - it works fine on a full page refresh but if new content is swapped in, sortable breaks. at the moment there is an htmx.onLoad callback which initialises all matching classes (call it initSortables), as demonstrated in the docs. i can see in the logs that when the new content is swapped in, initSortables is being called multiple times, but the error in the console still shows up. the only way to get it to work is to either manually call initSortables from the console or to do a full page refresh. i just cant figure this out!
  • h

    happy-knife-63802

    03/22/2023, 2:03 PM
    [Error] TypeError: undefined is not an object (evaluating 'dragEl.parentNode[expando]._isOutsideThisEl') _checkOutsideTargetEl (output.a716e34dc60d.js:97:232)
  • h

    happy-knife-63802

    03/22/2023, 2:09 PM
    i noted with htmx.logAll() that the last event shown is pushedIntoHistory so i tried attaching initSortables to that, but it still doesn't work
  • k

    kind-account-66413

    03/22/2023, 5:45 PM
    hi all, thank you for existing as a Discord server! I recently started a greenfield project with HTMX, and I want to address a general feeling I have that the docs haven't provided a strong direction for. That is, am I doing things the right way? Specifically, building a backend-driven SPA with HTMX. Previously I've used Flask/Django templates to build server-rendered pages, but for this project I wanted to try something new. I initially started by developing just one page of my application, and then I used HTMX to show a modal. Then I moved onto the login page where I realised I'll need to introduce some form of partial vs complete rendering depending on if I'm refreshing the page or navigating to a URL through the browser's URL bar. I came to these conclusions myself through reasoning over time, but I did try to search around to find a definite source telling me this is the way to go if I wanted to replicate a SPA. Additionally I'm still uncertain if I should be using things like
    HX-Location
    . I think definitive or recommended direction would help users and folks on the fence and I would be interested in hearing your thoughts. Thanks!
  • s

    some-airline-73512

    03/22/2023, 5:54 PM
    Anyone tried https://imba.io/ ? Curious what are the thoughts from the htmx-minded people
  • r

    ripe-action-67367

    03/22/2023, 6:06 PM
    curious syntax
  • n

    numerous-agent-22346

    03/22/2023, 6:06 PM
    I am a new user myself and I got a lot of ideas just from reading the essays https://htmx.org/essays/ and parts of the book: https://hypermedia.systems/book/contents/
  • m

    mysterious-toddler-20573

    03/22/2023, 6:09 PM
    first impression is that it looks like an interesting, reactive thick client library w/ some funky syntax. It looks to me like a lot of stuff is hiding inside stuff like
    app-canvas
    etc.
  • s

    some-airline-73512

    03/22/2023, 6:17 PM
    Yeaah.., complexity is bad
  • s

    some-airline-73512

    03/22/2023, 6:18 PM
    I would like to understand where is the core value proposition there.
  • m

    mysterious-toddler-20573

    03/22/2023, 6:19 PM
    generally I would recommend sticking w/ the MPA approach you are used to and using htmx to spice it up, starting w/ something as simple as
    hx-boost
    and then getting more elaborate from there. Most folks end up w/ multiple templates and controller end points to take advantage of the natural shape of web applications (w/ URLs for resources) etc. Broadly my htmx apps look a lot like my pre-SPA applications, just w/ some finer-grained templating and end points.
  • k

    kind-account-66413

    03/22/2023, 6:30 PM
    this is great feedback thank you so much
1...107610771078...1146Latest