简体   繁体   中英

Calling javascript Service/Method from array object

I have this array:

[
   {service: "AssignmentService", method: "saveMillaje", params: [1, 'some param'], id: 1},
   {service: "AssignmentService", method: "saveMillaje", params: [1, 'some param', { otherData: 'data' }], id: 2}
]

Where "service" is a wrapper file for axios like:

// AssignmentService.js
import Http from './Http';
const endpoint = 'assignment';
export default {
   saveMillaje(param1, param2, param3) {
      // do something
      // this will return a PROMISE
   },
};

I need in a certain point of my app, call and execute for example, the position 1 of the array with all params.

Transform this:

{service: "AssignmentService", method: "saveMillaje", params: [1, 'some param', { otherData: 'data' }], id: 2}

To:

import AssignmentService from 'services/AssignmentService';
...
someMethod(){
   AssignmentService.saveMillaje(1, 'some param', {otherData: 'data'})
   then((response)=> {
     // So something
   });
}

I have no idea to achieve this. Can you help me? Thanks a lot for any help about it.

import AssignmentService from 'services/AssignmentService';
import OtherService from 'services/OtherService';
var services = {AssignmentService, OtherService}

someMethod(obj){
   services[obj.service][obj.method].apply(null, obj.params).
   then((response)=> {
     // So something
   });
}

Then you can just pass in the objects from your array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM