axelor / axelor-open-platform

Open source Java framework for business application development
http://axelor.com
Other
391 stars 298 forks source link

axelor.$eval compatible with fields(containing dot characters), often used in conditional expressions #107

Open cabbage89 opened 2 years ago

cabbage89 commented 2 years ago

https://github.com/axelor/axelor-open-platform/blob/dev/axelor-web/src/main/webapp/js/axelor.app.js#L184

When there are dotted fields in the context object, AngularJS cannot use the $eval expression

axelor.$eval(this.scope, "user.name=='tom'", {"user.name":"tom"});

fix

Object.keys(context).filter(x=>x.includes('.')).forEach(a=>a.split('.').reduce((x,y,i,arr)=>x[y]=(i==arr.length-1?context[a]:(x[y]||{})),context));
{"user.name":"tom"}

Convert the above context object into

{"user":{"name":"tom"}}
cabbage89 commented 1 year ago

https://github.com/axelor/axelor-open-platform/blob/dev/axelor-web/src/main/webapp/js/form/form.relational.single.js#L176

_.each(relativeFields, function (field) {
        try{
          record[field] = eval(`value.${field}`);
        }catch (e){}
      });
      Object.keys(record).filter(x=>x.includes('.')).forEach(a=>a.split('.').reduce((x,y,i,arr)=>x[y]=(i==arr.length-1?record[a]:(x[y]||{})),record));