ember-rapid-forms icon indicating copy to clipboard operation
ember-rapid-forms copied to clipboard

Use em-select to fill a hasMany

Open flyrev opened this issue 8 years ago • 3 comments

Let's say that a user should choose his favorite artist, yet, we're not going to use the name but rather a reference to another artist.

    {{em-select
        label="Artist"
        property="artist"
        content=artists
        optionValuePath="id"
        optionLabelPath="name"
        prompt="Who's your favorite artist?"
    }}

So, we have // models/artist.js export default Model.extend({ name: attr('string') })

// models/user.js export default Model.extend({ artists: hasMany('artist', { embedded: 'always', async: true }) });

Now, if I pass store.findAll('artist') to the dropdown, and try to update it, we will get the following: Assertion Failed: You must pass an array of records to set a hasMany relationship

How can I make it work if I want to update a relationship on an object instead of just a value? Preferably what I would want in this example is to either overwrite the list entirely by the selected dropdow, or append to it.

flyrev avatar Jun 30 '16 21:06 flyrev

Hi and sorry for the late answer (I forgot you).

Can you try to use propertyIsModel=true attribute? You will have something like :

{{em-select
    label="Artist"
    property="artist"
    content=artists
    optionValuePath="id"
    optionLabelPath="name"
    prompt="Who's your favorite artist?"
    propertyIsModel=true
}}

I think it will work.

GCorbel avatar Jul 04 '16 17:07 GCorbel

No luck.

I created a repo for it: https://github.com/flyrev/ember-rapid-forms-select-hasMany

flyrev avatar Jul 05 '16 09:07 flyrev

For a quick workaround (for belongsTo)

artistId: Ember.computed({
    get: function() {
      return this.get('artist.id');
    },
    set: function(key, value) {
      if(value){
        this.set('artist', this.store.peekRecord('artist', value));
      }
      return value;
    }
  }),

I gonna look a little further into it to find out how to do this the best way.

spruce avatar Aug 03 '16 16:08 spruce