Dynamic Fields in Ember Model

Hi, I want to add fields dynamically, basically fields details will be fetched using api call.

Can someone please guide how can we achieve this using ember.

So in the new world of Ember Data this kind of thing should be trivial. However that’s currently still in development. The closest think I can think of is ember-m3 which ships a replacement to Ember Data model that supports things like flexible or dynamic schema.

It’s still not incredibly hard to pull off if you understand how the pieces fit. Won’t be as elegant as SchemaRecord will be and won’t type check very well though.

Thanks. Can you throw some more light on this. It will be helpful if you can elaborate a bit more on what you mean by it.

@Ankit_Jain Ember Data is in the middle of a large revamp (described here: https://www.youtube.com/watch?v=KpakmlxvT0s&list=PL4eq2DPpyBbn9eFgZIxo23_q9sAtqGtvt&index=15). Basically, the issue you’re wrestling with is that classic Ember Data’s approach to data was to use a model layer that has model’s schema baked into the Javascript file. But you are wanting it to be server-defined and flexible.

In the new world those items are separate constructs that CAN be defined together as we are used to, but don’t have to be. Would suggest you watch the entire talk above, but that part where that starts getting discussed is about 15 min in.

How are the fields going to be added to the api call?

Your best bet is to just add all the fields to one property

export default class DynamicFields extends Model { @attr fields;


and the api call will return this

{ data:{ _id: “some UUID”, type: “dynamicfields” fields: { field1: “hi1”, field2: “hi2”, field3: “hi3”, } } }