JS model set hasMany 'xxxIds' field not working

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Firyo
    Senior Member
    • Jun 2022
    • 134

    JS model set hasMany 'xxxIds' field not working

    Hi there,

    I'm trying to do a this.model.set('xxxxxIds', [...]); on a "hasMany" field but the 'views/fields/link-multiple' view doesn't seem to take my .set() into concideration.
    It was working very well when this field was a "belongsTo" but now it doesn't. Am I doing the wrong thing ?

    The code is below

    Click image for larger version

Name:	image.png
Views:	234
Size:	42.3 KB
ID:	97309


    Regards,
    Firyo.
  • rabii
    Active Community Member
    • Jun 2016
    • 1250

    #2
    you try this (if the field view is discountsIds) and extends (views/fields/link-multiple)

    PHP Code:
    this.model.set(this.idsName, discountsIds);
    this.model.set(this.nameHashName, nameMap); // get some name mapper for the discount collection 
    
    Rabii
    Web Dev

    Comment

    • Firyo
      Senior Member
      • Jun 2022
      • 134

      #3
      The code I screenshoted is inside a view setup handler not a views/fields/link-multiple.
      I already tried to do both .set('discountsIds, [...]) and .set('discountsNames, {...}) but it didn't work either.

      Comment

      • yuri
        Member
        • Mar 2014
        • 8440

        #4
        You set the same value. Arrays in JS are objects. Try to clone it right after 'get'.

        Code:
        const discountsIds = Espo.Utils.clone(this.model.get('discountsIds'));
        or

        Code:
        const discountsIds = [...this.model.get('discountsIds')];
        If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

        Comment

        • Firyo
          Senior Member
          • Jun 2022
          • 134

          #5
          Originally posted by yuri
          You set the same value. Arrays in JS are objects. Try to clone it right after 'get'.

          Code:
          const discountsIds = Espo.Utils.clone(this.model.get('discountsIds'));
          or

          Code:
          const discountsIds = [...this.model.get('discountsIds')];
          Well, I always forgot about this JS specificity, thank you Yuri.

          Comment

          Working...