How to specify include field in member function

#1

If I want to get value of a field inside the member function, I could do

data = this.get('myField');
// data.myField will contain value of myField

Is it possible we could put some annotation when declaring member function, so that value of myField is automatically available on this, and we don’t need to do this.get('myField')?

P.S., I tried below annotations, but none of them work

@dependency(include='myField')
myFunction: member function(): js server
@db(include='myField')
myFunction: member function(): js server
0 Likes

#2

The get function, as you observed does allow you to specify an include spec. Without the include spec, all fields that are part of the obj (e.g. fkey arrays are not included and external references return only the id). If you are defining your own function you can of course have an include parameter as well or do whatever you want in your function.

0 Likes

#3

@trothwein to follow-up on this, very often we start the body of a function foo(arg1: string, arg2: SomeType, arg3: SomeOtherType) with something like:

if (arg2.some_other_field_that_we_need == undefined) {
arg2 = arg2.get(“this, some_other_field_that_we_need, …”) ;
}
if (arg3.some_other_field_that_we_need == undefined) {
arg3 = arg3.get(“this, some_other_field_that_we_need, …”) ;
}

this adds complexity to the code.

Would that make sense to add a functionality on function declaration so that:

  1. We require some fields on some of the parameters in order to run the function. Otherwise an error message will be thrown (similar to !)
    and/or
  2. The platform will automatically check that the objects passed contained required fields, if not it will include them automatically.
0 Likes