ember-admin
ember-admin copied to clipboard
using dynamic models
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 ?
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
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
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)
no, I was talking about a change I will likely make in the future.
For the time being you should use store:admin
How do I register them with the admin store?
How are you getting the primary store to recognize the models?
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.
you'll have to do something similar:
let adminStore = getOwner(this).lookup('store:admin');
adminStore.register('model:user', DS.Model.extend(tableColumns));
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) ?
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?
Thanks, Yes my primary model works fine, also all the hasMany and BelongTo relations work
no, I'm asking if these models are accessible via your primary store?
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.
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?
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.
Unclear atm. The models would definitely have to be registered prior to hitting the admin interface.
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
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?
It seems models are created but types are not recognised.
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.
@bcardarella any idea what is going wrong that I am getting the error above?
I need an example to work with
ok I will put together something thanks :) please keep this issue open and I will make something.
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.
@bcardarella I know it has been easter holiday but I was wondering if you had a chance to look at this yet?