Skip to content

$add silently ignores action if record is also in another collection. #387

@werner291

Description

@werner291

Hello,

I ran into the situation where there were two models: "House" and "HouseGroup".

I fetched a list of houses using House.$search();, then took one of the records in that collection and $add-ed it to a HouseGroup (which has a hasMany relation to House).

The action fails silently because that house was already in the House collection.

This is the code in the library that causes it:

$add:` function(_obj, _idx) {
  Utils.assert(_obj.$type && _obj.$type === this.$type, 'Collection $add expects record of the same $type');

  return this.$action(function() {
    if(_obj.$position === undefined) { // <--- Here's the problem.
      if(_idx !== undefined) {
        this.splice(_idx, 0, _obj);
      } else {
        this.push(_obj);
      }
      _obj.$position = true; // use true for now, keeping position updated can be expensive
      this.$dispatch('after-add', [_obj]);
    }
  });
},

Proposed solution: allow records to be in multiple collections, or document why this is problematic and throw an exception instead of failing silently.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions