5

I would like to use a combobox at the adminUI with fields that come from a webservice. I was thinking on get the data with a pre 'find' hook and then override the options atribute at the 'audience' property in the Schema.

Schema:

Compliance.add({
  title: { type: Types.Text, required: true, initial: true, index: true }, 
  url: { type: Types.Url, required: true, initial: true },
  position: { type: Types.Number, initial: true },
  audience: { type: Types.Select, options: [], many: true, initial: true},
});

Hook:

Compliance.schema.pre('find', async function(next) {
  let audiences = await audienceService.getAudiences();
  next();
})

But I didn't find the way to bind the data. Any ideas how this can be done?

Thanks

Jorkab
  • 53
  • 3

1 Answers1

3

You can try making a function from the options:

function getAudiences() {
    return ['a', 'b', 'c'];
}

Compliance.add({
  title: { type: Types.Text, required: true, initial: true, index: true }, 
  url: { type: Types.Url, required: true, initial: true },
  position: { type: Types.Number, initial: true },
  audience: { type: Types.Select, many: true, initial: true, options: getAudiences() }
});

Result as below:

enter image description here

Thomas Choy
  • 321
  • 2
  • 13
  • When I use a an asynchronous function from an external service to fill the options I get an error because js can't infer that is an array. Also I'll have the problem that with this formula the function will be executed only when the Schema is instantiated, so I only could have dynamic data if restarts the server and the schema is instantiated again. It's a good approach but I need to find the way to modify the Schema after it is instantiated or reinstantiate a new one modified. – Jorkab May 16 '18 at 12:36
  • If the data is such dynamic why not consider Types.Relationship? – Thomas Choy May 16 '18 at 17:13
  • Because if I use Types.Relationship I have to store and maintain a collection with the data that comes from the webservice. – Jorkab May 17 '18 at 06:17
  • In this case I still suggest to use Types.Relationship. You can setup a schedule job to update your Audience model by webservices and at the same time maintain the integrity by not deleting those records that are already referened by model Compliance. – Thomas Choy May 17 '18 at 06:51
  • I mark this like the most helpful answer. In this way works, and works like keystonejs is created to do it. However I'll still be working on find a solution modifiying the keystone Types. – Jorkab May 24 '18 at 10:12
  • what if the data is being queries from the model ? –  Aug 01 '19 at 08:22