ember-admin icon indicating copy to clipboard operation
ember-admin copied to clipboard

using dynamic models

Open zardaloop opened this issue 7 years ago • 25 comments

I am creating my models dynamically after loading the schema from the server using

Ember.getOwner(store).register('model: modelName', DS.Model.extend(attributes));

It seems this addon need the actual files for models to be bale to create the models. Is there anyway around that?

Is there anyway to add them into the store which ember-admin use at the same time ?

zardaloop avatar Mar 22 '17 17:03 zardaloop

It already does pull model types dynamically: https://github.com/DockYard/ember-admin/blob/master/addon/routes/admin.js#L10

but the models do need to be registered with the admin store: https://github.com/DockYard/ember-admin/blob/master/addon/stores/admin.js you are likely only making your primary store aware of them

bcardarella avatar Mar 22 '17 17:03 bcardarella

The admin store is here: https://github.com/DockYard/ember-admin/blob/master/app/initializers/admin.js#L4

FWIW, this path is out of date as the primary ember store has been changed to service:store. This addon should be updated to do the same, likely service:store:admin

bcardarella avatar Mar 22 '17 17:03 bcardarella

Thanks @bcardarella if I understand you correct I have try to change that to service:store:admin and now I get this error

Uncaught TypeError: Invalid Fullname, expected: 'type:name' got: service:store:admin at Registry.validateFullName (:27620:15) at Registry.register (:27166:70) at Class.register (:59948:54) at Object.initialize (:137942:9) at :29899:23 at Vertices.each (:27965:13) at Vertices.topsort (:27932:14) at DAG.topsort (:27877:24) at Class._runInitializer (:29927:13) at Class.runInitializers (:29888:12)

zardaloop avatar Mar 22 '17 17:03 zardaloop

no, I was talking about a change I will likely make in the future.

For the time being you should use store:admin

bcardarella avatar Mar 22 '17 17:03 bcardarella

How do I register them with the admin store?

zardaloop avatar Mar 22 '17 17:03 zardaloop

How are you getting the primary store to recognize the models?

bcardarella avatar Mar 22 '17 17:03 bcardarella

I get store from the service by doing something like this

const { inject: { service } } = Ember;
export default Ember.Service.extend({
 store: service(),

then I have function in the service lets call it generateModels() where inside that I create an attribute object with all the attributes and their types from the schema like

var tableColumns = { name: DS.attr(string), surname: DS.attr(string)};

then inside the function I get the store and register the tableColumns

let store = this.get('store');
Ember.getOwner(store).register('model: user', DS.Model.extend(tableColumns));

obviously there is loop and I do the same for all the models inside the schema.

zardaloop avatar Mar 22 '17 17:03 zardaloop

you'll have to do something similar:

let adminStore = getOwner(this).lookup('store:admin');
adminStore.register('model:user', DS.Model.extend(tableColumns));

bcardarella avatar Mar 22 '17 17:03 bcardarella

Perfect I think there is only one little issue left here I get this error

TypeError: adminStore.register is not a function

I am using it inside a service does that make any difference when I call getOwner(this) ?

zardaloop avatar Mar 22 '17 18:03 zardaloop

Oh, it looks like it just registers on the owner which should be OK. I'll reopen this and see why its not getting the new models.

Just to be clear: is your primary store getting these dynamic models?

bcardarella avatar Mar 22 '17 18:03 bcardarella

Thanks, Yes my primary model works fine, also all the hasMany and BelongTo relations work

zardaloop avatar Mar 22 '17 18:03 zardaloop

no, I'm asking if these models are accessible via your primary store?

bcardarella avatar Mar 22 '17 18:03 bcardarella

well when I define them, then I can use all those models to create and load data etc. However ember uses lazy so they are not created until the first record is created if this is what you are asking. did I answer the question yet? But to be clear store knows about those model, because if I try to create a record for a model it doesn't know about then it complains and give error.

zardaloop avatar Mar 22 '17 18:03 zardaloop

based upon a small test, it definitely pulls in the models dynamically. Assuming the route model hook isn't cached in any way.

Would you be able to provide an example that reproduces the problem so I can see what is happening?

bcardarella avatar Mar 22 '17 18:03 bcardarella

you see I have created a file for user model only because I need that model before user register and that works fine with ember-admin. However all the other dynamically create models are not available . At what point it pulls the models? I only register them after user login successfully. Could it be that at that point it is too late to register them ? I will see if I can make an example.

zardaloop avatar Mar 22 '17 18:03 zardaloop

Unclear atm. The models would definitely have to be registered prior to hitting the admin interface.

bcardarella avatar Mar 22 '17 18:03 bcardarella

if it helps this is the error I get at the moment when I try to navigate to admin/:model

Uncaught TypeError: Cannot read property 'klass' of undefined
    at Class.<anonymous> (<anonymous>:94933:23)
    at ComputedPropertyPrototype.get (<anonymous>:41634:28)
    at Object.get (<anonymous>:46541:19)
    at Class.<anonymous> (<anonymous>:54635:31)
    at ComputedPropertyPrototype.get (<anonymous>:41634:28)
    at Object.get (<anonymous>:46541:19)
    at RootPropertyReference.compute (<anonymous>:39635:26)
    at RootPropertyReference.value (<anonymous>:39502:45)
    at ArrayIterable.iterate (<anonymous>:39258:26)
    at IterationArtifacts.isEmpty (<anonymous>:69316:58)

which I think is from this file : https://github.com/DockYard/ember-admin/blob/42aa065de33d766b5958d05ba3673f81c12e90f4/addon/mixins/model-records/model-record.js

zardaloop avatar Mar 22 '17 18:03 zardaloop

Ah I think I know what is the issue, it is to do with the type. Could be because I have introduce my own type?

zardaloop avatar Mar 22 '17 18:03 zardaloop

It seems models are created but types are not recognised.

zardaloop avatar Mar 22 '17 18:03 zardaloop

ignore my comments above, it has nothing to do with the types I am creating but still don't know why. but the error
Cannot read property 'klass' of undefined is what I am getting.

zardaloop avatar Mar 23 '17 08:03 zardaloop

@bcardarella any idea what is going wrong that I am getting the error above?

zardaloop avatar Mar 27 '17 14:03 zardaloop

I need an example to work with

bcardarella avatar Mar 27 '17 14:03 bcardarella

ok I will put together something thanks :) please keep this issue open and I will make something.

zardaloop avatar Mar 27 '17 15:03 zardaloop

hi @bcardarella sorry I couldn't get around this earlier. I have created a repository as an example project https://github.com/zardaloop/ember-admin-auto-generate . There is a simple API running on OpenShift which serves a very tiny model schema used in the service I have created called model-generator. However since the server is on another site and you are going to run the app locally, I think you might want to install this plugin to disable crossorigin. https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi?utm_source=chrome-app-launcher-info-dialog

I would highly appreciate if you could please have a look and see why you think ember admin doesn't work.

Many thanks.

zardaloop avatar Apr 14 '17 22:04 zardaloop

@bcardarella I know it has been easter holiday but I was wondering if you had a chance to look at this yet?

zardaloop avatar Apr 19 '17 18:04 zardaloop