CanJS - Incorrect list objects type after mapping

43 views Asked by At

If I have this instantiation const someObj = new MyObj({...someParams}) and MyObj is defined this way:

MyObj = DefineMap.extend("MyObj", {
  myProp: {
    Type: AnotherType.List
  }
})

and

AnotherType.List = CanList.extend("AnotherType.List", {
  Map: AnotherType
}, {
  serialize() {
    const res = [];
    for (let i = 0; i < this.attr("length"); i++) {
      res.push(this.attr(i.toString()).serialize());
    }
    return res;
  }
})

then the someObj.myProp[0] type is DefineMap instead of AnotherType. How can I fix this?

I already tried this but still not working: const someObj = canReflect.assignDeep(new MyObj(), {...someParams})

1

There are 1 answers

1
Ninita On BEST ANSWER

With the help of Bitovi community I solved it (thanks to Brad Momberger).

So Brad explained that it happens because of the DefineList's bubble binding and the same doesn't happen with DefineMap objects.

So we always need to serialize the lists first the get the correct type of the list entries at the end, like this:

const someObj = new MyObj({
  ...someParams,
  myProp: someParams.myProp.serialize()
})

You can see the full explanation here.