ember-cli-selectize icon indicating copy to clipboard operation
ember-cli-selectize copied to clipboard

[BUG] At ember 2.0.x. Selection = PromiseManyArray, asserted not an array

Open marcemira opened this issue 9 years ago • 10 comments

At Ember 2.0.x, Ember-data 2.0.x:

I'm using selection=model.tags, where model's tag is defined like tags: DS.hasMany('tags', { async: true}) which asserts:

Uncaught Error: Assertion Failed: When ember-selectize is in multiple mode, the provided selection must be an array.

In the code, the assert uses isArray in order to check if property selection is an array, but @wecc pointed at Slack that Ember.isArray is the same as Array.isArray and it doesn't check for array-like, which in this case would result on false. Is there another way to check this assert?

Another thing I noticed, is that it should be using something like this.setAttr('selection', selection) instead of current this.set('selection', selection) at addon/components/ember-selectize.js:L360 and addon/components/ember-selectize.js:L473

Thanks for the awesome job :)

marcemira avatar Aug 31 '15 13:08 marcemira

Hi @marcemira, did you find a work around this issue?

GabKlein avatar Sep 04 '15 06:09 GabKlein

@GabKlein not yet... :/ but @miguelcobain told me that he'll take a look

marcemira avatar Sep 04 '15 15:09 marcemira

I'm also having issues with a newly created model. A category belongsTo relationship which is empty is used on selection = model.category. This throws Cannot call get with 'id' on an undefined object Does anyone have a clue? I'll have to get my hands dirty on this addon or replace it. I'll do the first...

marcemira avatar Sep 07 '15 22:09 marcemira

My workaround is to add a property in the controller that return an has_many array.

clients: function() {
  return this.get('model.clients').reject(function(object) {
    return object.get('hasDirtyAttributes');
  });
}.property('model.clients')

Note the reject, this avoid to get new objects before they get saved. eg:

this.store.createRecord('order', {
  reference: reference,
  organization: this.model
}));

GabKlein avatar Sep 08 '15 07:09 GabKlein

Yeah, getting this too. Will try @GabKlein's solution, and report back.

srsgores avatar Oct 16 '15 20:10 srsgores

Ok, so @GabKlein's solution was not helpful in my case. I had to manually update ember-selectize.js in 3 places.

It turns out that all we need to do is use this.get("selection").toArray() to convert the promisy object to an array-like object.

For example, line 328:

 if (multiple && isArray(selection.toArray()) && obj) {
          selection = selection.toArray();
        if (!selection.findBy(this.get('_valuePath'), get(obj, this.get('_valuePath')))) {
          this._addSelection(obj);
        }

Then in the corresponding spots in _onItemRemove(value) and _onItemAdd(value).

srsgores avatar Oct 16 '15 22:10 srsgores

Same issue here.

mbutsko avatar Nov 12 '15 15:11 mbutsko

Try to call toArray() before passing the array to selectize.

miguelcobain avatar Nov 12 '15 15:11 miguelcobain

+1

tobsch avatar Nov 18 '15 07:11 tobsch

My fork solves that: https://github.com/tobsch/ember-cli-selectize I don't like the idea of just converting toArray though.

tobsch avatar Nov 22 '15 08:11 tobsch