hyperapp icon indicating copy to clipboard operation
hyperapp copied to clipboard

Nested model reducer question

Open cdeutmeyer opened this issue 9 years ago • 6 comments

I'm unsure how to update just a part of a complex model and could not find any examples. This is a bit contrived, but what would the reducer look like that changes the name property on the "d" object. What's the best practice for something like this? Thanks!

app({    
  model: { 
    a: {
        name: "a",
        b: {
            name:"b",
            c: {
                name: "c",
                d: {
                    name:"d",
                    e: {
                        name:"e",
                        f: {
                            name:"f"
                        }
                    }
                }
            }
        }        
    }
  },
  
  reducers: {
      changeDsName: (val) => ({ /* not sure what goes here */ }) 
  },
  
  view: (model, actions) => (
      <div>
          <button onclick={ _ => actions.changeDsName("g") }>Change D</button>
          <div>{ model.a.b.c.d.name }</div> 
      </div>
   )  
})

cdeutmeyer avatar Feb 27 '17 01:02 cdeutmeyer

You should use a recursive algorithm to find what you want to change and then change that. I'd suggest to also clone the model in the process.

In addition to that I doubt that your data structure actually makes sense. Could you share the details or is this a purely theoretical question?

FlorianWendelborn avatar Feb 27 '17 02:02 FlorianWendelborn

It's a theoretical question. I'm giving a HyperApp presentation to the other developers at work and I want to be able to answer questions they are going to have. We all work on an extremely large web app and they are naturally going to wonder how HyperApp could scale to to something of that size.

cdeutmeyer avatar Feb 27 '17 02:02 cdeutmeyer

@cdeutmeyer Well, hyperapp only expects a new, modified model. So I'd say the question itself isn't really related to hyperapp. It's a question about this form of recursive data-structure.

I'd say answering that you need 2 things:

  1. a cloning algorithm, since technically hyperapp reducers are supposed to be pure functions, which means that the model is immutable
  2. an algorithm that iterates the object to find the spot that has to be changed

Funny thing is that coincidentally both of these algorithms are basically doing the same thing. I personally would use a function that you call recursively for all keys in the current sub-object you're in. Then that function could either clone or modify its part and return the result.

If you don't know what such a function would look like I suggest googling for "JavaScript object deep clone". That'll lead a ton of results and most of them will be recursive.

FlorianWendelborn avatar Feb 27 '17 03:02 FlorianWendelborn

OK, I'll see what I can come up with.. I wan't to make sure I'm understanding something. Should the entire model be cloned or just the object (and it's children) that are being updated. I had been using the following pattern to just clone the object being changed.

  model: { 
    greeting: {
        happy: "hello sunshine",
        mad: "go away"
    }, 
    otherjunk: {
	foo: "baz"		
     }
  },
  reducers: {
  	changeGreeting : (model, g) => ({ greeting: Object.assign({}, model.greeting, { happy: g }) })
  }

cdeutmeyer avatar Feb 27 '17 03:02 cdeutmeyer

@cdeutmeyer I was under the impression that the whole model should be cloned. If not cloning works then @jbucaran should be able to answer if this is an intended, supported feature and not an implementation detail.

FlorianWendelborn avatar Feb 27 '17 03:02 FlorianWendelborn

@cdeutmeyer This looks good.

From the docs:

Reducers return a new model or part of it. If it returns part of a model, that part will be merged with the current model.

So, HyperApp is doing Object.assign({}, model, newModel) for you already.

Your example:

changeGreeting : (model, g) => ({ greeting: Object.assign({}, model.greeting, { happy: g }) })

Is equivalent to:

changeGreeting : (_, g) => ({ happy: g })

jorgebucaran avatar Feb 27 '17 03:02 jorgebucaran