Forms with dynamic fields defined by the model

I want to create something that doesn't quite match any documentation or examples I've seen so far. Perhaps what I am trying to do is the already known Bad Idea. Let me know if so.

I am trying to figure out how to structure my controller and views around a server side API, which is a little unusual. I have a form where Im collecting some data, but the fields in the form need to be dynamic based on the data coming from the server. Here's an example of what the API returns (example to build from generated domain for simplicity). I am not using ember data.

GET /event_color_choices?event_id=42&user_id=1

{
  user_id: 1,
  event_id: 42,
  color_choices: [
    // These will become form fields in the dynamic form.
    // They won't be the same every time:
    {
      color: 'blue',
      popularity: 100,
      some_other_metadata: 'foo'
    },
    {
      color: 'green',
      popularity: 200,
      foo: 'bar'
    },
    {
      color: 'orange'
      popularity: 150,
      baz: 'qux'
    }
  ]
}

      

So in my route, this data structure becomes my model:

App.EventChoicesRoute = Ember.Route({
  model: function(params) {
    return Ember.$.getJSON('event_color_choices' {
        event_id: params.eventId,
        user_id: params.userId
    });
  }
})

      

In my opinion, I would like to do something like this (I think?):

{{#each colorChoice in color_choices}}
  <label>How much do you like the color {{colorChoice.color}}?</label>
  {{input type='text' value=???}}  // <--- This is the part I'm having trouble with
{{/each}}

      

It should look something like this:


How much do you like blue?

[[text input field here]]

How much green do you like? [[text input field here]]

How much orange do you like? [[text input field here]]


And when the user fills out the form, I would like the model to be updated to become something like this, and I will submit back to the server on the form submit:

POST /event_color_choices

{
  user_id: 1,
  event_id: 42,
  color_choices: [
    {
      color: blue,
      // ...
      value: "I like blue a lot.  It my favoriate color."
    },
    {
      color: 'green',
      // ...
      value: 'Green is just okay'
    },
    {
      color: 'orange',
      // ...
      value: 'Orange is an okay starter color, for people who are just getting into colors'
    }
  ]  
}

      

Can I get ember to do this? Should I take a different approach?

+3


source to share


1 answer


Here is a working JS Bin example .

Solution scheme:
- Saving the selected array formData

in the controller for the needs of form data binding.
- This array can be updated in the route method setupController

and cleared in the route method resetController

.
- When the form is submitted, the model is updated with the content formData

and is submitted to the server (in this example, it just prints the gated model).



* Note. The method IndexRoute.model

returns a promise to simulate an asynchronous server request.

+1


source







All Articles