Giter Club home page Giter Club logo

Comments (7)

toddself avatar toddself commented on May 11, 2024 1

#52 resolves the final question in this thread :)

from choo.

timwis avatar timwis commented on May 11, 2024

I recall from the code that #choo-root is only the default name of the container. You should be able to have multiple components on a page via:

const app1 = choo({ name: 'app1' })
const app2 = choo({ name: 'app2' })

from choo.

toddself avatar toddself commented on May 11, 2024

Ah. Somehow missed that. It means you also have to namespace your model it seems? Thanks @timwis

from choo.

timwis avatar timwis commented on May 11, 2024

Nope - I don't believe name and namespace are connected.

from choo.

toddself avatar toddself commented on May 11, 2024

I mean namespacing for issues like this:

const choo = require('choo')

const app1 = choo({name: 'app1'})
const app2 = choo({name: 'app2'})

app1.model({
  state: {
    data: 'app1'
  },
  reducers: {
    update: (action, state) => ({data: action.value})
  }
})

app2.model({
  state: {
    data: 'app2'
  },
  reducers: {
    update: (action, state) => ({data: action.value})
  }
})

function app1main (params, state, send) {
  return choo.view`<button onclick=${(evt) => send('update', {value: 'app1main'})}>${state.data}</button>`
}

function app2main (params, state, send) {
  return choo.view`<button onclick=${(evt) => send('update', {value: 'app2main'})}>${state.data}</button>`
}

app1.router((route) => [
  route('/', app1main)
])

app2.router((route) => [
  route('/', app2main)
])

const tree1 = app1.start()
const tree2 = app2.start()

document.body.appendChild(tree1)
document.body.appendChild(tree2)

If you click on the second button, it changes the state of the first button. Just adding a namespace to each new call to choo#model and the send events doesn't rectify this situation.

app1.model({
  namespace: 'app1',
  state: {
    data: 'app1'
  },
  reducers: {
    update: (action, state) => ({data: action.value})
  }
})

app2.model({
  namespace: 'app2',
  state: {
    data: 'app2'
  },
  reducers: {
    update: (action, state) => ({data: action.value})
  }
})

function app1main (params, state, send) {
  console.log(state)
  return choo.view`<button onclick=${(evt) => send('app1:update', {value: 'app1main'})}>${state.app1.data}</button>`
}

function app2main (params, state, send) {
  console.log(state)
  return choo.view`<button onclick=${(evt) => send('app2:update', {value: 'app2main'})}>${state.app2.data}</button>`
}

Still only ever effects the first button.

from choo.

toddself avatar toddself commented on May 11, 2024

This actually seems to be a bug in choo. If I look at the DOM I get:

image

So regardless of what I'm passing in for name it doesn't seem to change how choo is identifying the DOM nodes?

from choo.

toddself avatar toddself commented on May 11, 2024

Ah!

Upon closer inspection, you need to do:

const app1 = choo()
const app2 = choo()
app1.start({name: 'app1'})
app2.start({name: 'app2'})

Passing in just the string as the name does not work there, but it might be more discoverable if it did? Or perhaps it should be an argument when you're invoking choo like @timwis has in his example?

from choo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.