Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using dynamic models #82

Open
zardaloop opened this issue Mar 22, 2017 · 25 comments
Open

using dynamic models #82

zardaloop opened this issue Mar 22, 2017 · 25 comments

Comments

@zardaloop
Copy link

zardaloop commented Mar 22, 2017

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 ?

@bcardarella
Copy link
Member

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
Copy link
Member

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

@zardaloop
Copy link
Author

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)

@bcardarella
Copy link
Member

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

For the time being you should use store:admin

@zardaloop
Copy link
Author

How do I register them with the admin store?

@bcardarella
Copy link
Member

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

@zardaloop
Copy link
Author

zardaloop commented Mar 22, 2017

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.

@bcardarella
Copy link
Member

you'll have to do something similar:

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

@zardaloop
Copy link
Author

zardaloop commented Mar 22, 2017

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) ?

@bcardarella
Copy link
Member

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 bcardarella reopened this Mar 22, 2017
@zardaloop
Copy link
Author

zardaloop commented Mar 22, 2017

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

@bcardarella
Copy link
Member

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

@zardaloop
Copy link
Author

zardaloop commented Mar 22, 2017

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.

@bcardarella
Copy link
Member

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?

@zardaloop
Copy link
Author

zardaloop commented Mar 22, 2017

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.

@bcardarella
Copy link
Member

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

@zardaloop
Copy link
Author

zardaloop commented Mar 22, 2017

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
Copy link
Author

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
Copy link
Author

It seems models are created but types are not recognised.

@zardaloop
Copy link
Author

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
Copy link
Author

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

@bcardarella
Copy link
Member

I need an example to work with

@zardaloop
Copy link
Author

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

@zardaloop
Copy link
Author

zardaloop commented Apr 14, 2017

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants