-
Notifications
You must be signed in to change notification settings - Fork 122
Use inherited models in two-way databinding #33
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
Comments
Your model should be just this:
and, as you already read, you should create it with:
If you do this, everything should go well. At the moment, the error you receive is because you are overwriting the |
Hi @ghidoz Well, actually, I commented the constructor that was in my model but maybe you didn't notice. It was just to show how it was before. So no, I didn't overwrite the constructor. After that, the error message is pretty explicit, as it says that I don't provide the right arguments (logical, since I instanciate an empty Note object without any argument) I don't know if I was clear enough about the blocking point I'm encountering, or maybe it's me who's taking this problem the wrong way. Let's try to be simple : How can I do two-way databinding, using the model that extends JsonApiModel ? Before using angular2-jsonapi, I did it in the standard way (the way Angular2 describe in the official doc):
Now I can't do this anymore, as I can't instantiate a Note object in my component, as it now inherits from JsonApiModel. I hope my problem is clearer. Thx to correct me if I take the problem in the wrong way, I'm still new in Angular2. |
Ok, I got the point: you need the export class CreateNoteComponent {
public note: Note;
constructor(private datastore: Datastore) {
this.note = this.datastore.createRecord(Note);
}
onSubmit() {
this.note.save().subscribe(() => {
// saved!
});
}
} |
It looks better, but another problem : createRecord() method returns a jsonApiModel object, whereas I expect note to be a Note object :
If I remove the typing Any other solution or am I forced to not type any of my objects ? |
If you upgrade to the new v.3.2.0 you shouldn't have the type error anymore. |
Allright. I just updated my package.json with the last version : I have an unexpected : When I open the angular2-jsonapi folder in my node_modules, I've got like a not compiled repository :
Same thing after an uninstall and reinstall of the package. |
Mmm... it seems the same problem of #13. Try to put the importing of angular2-jsonapi as the first import. Btw there are no problems with the release, as long as you have the |
Well, strange. But now, the same message remains. Also I tried to move the Strange thing also is that my VS code underline the import with
I use webpack 1.13.2, but not angular-cli |
Later I'll try to check. Meanwhile you can downgrade again to v3.1.0 and use this workaround: this.note = <Note>this.datastore.createRecord(Note); |
Yeah I think I don't have other choice than downgrade. I tried every fix proposed in the issues related to #13, like adding I'll do the downgrade this afternoon and try the transtyping as your proposed, and come back to you |
@ghidoz your workaround seems to make the job. Good :) I hope I'll be able to migrate to the last version soon. Another question, more about the initial topic : does angular2-jsonapi preconise to not use any services between view component and Datastore ? In other terms, should components call directly the Datastore service ? I have a NoteService that I instanciate in my CreateNoteComponent, from my point of view this service should be responsible of passing datas from client to API, with the help of Datastore.
|
I tried this approach, to keep my service : create-note.component.ts
note.service.ts
I used an empty object as a member of my component to get the databinding, and transtyped it to to pass the typing. |
Yes, the idea is having just one service that manage everything, called directly from the components.
Exactly. There's no point in having a specific service for each resource, mapping the datastore methods. |
Allright. Is it not a bit risky ? Well I'm interested by your thought, as it's the first time I have to deal with that kind of architecture. Maybe I make it more complex in my mind than it is, and the mapping in the Datastore is largely enough (as long as you define correctly your entities & relations)... |
Well, maybe it depends on how complex is your application, yes. In some cases it may be useful creating custom services, when doing filtering, editing... as you suggested. Btw, what I recommend you is to not subscribe the Observer within a service. You should do it in a component, as the service should just return the cold Observer. I don't know where do you find this recommendation, but I assure you it is the opposite. Have a look to this Angular style guide, for example: they refactor all the logic inside the service, but the |
You're right, the service should only return a cold Observer. I don't remember where I saw a service subscribing, but maybe I've been mistaken by all these updates :) Then I think this discussion is closed. It's clearer for me, thanks for your help :) |
Well, the right version should be without the subscribe. Btw in the future may be a new interface that will let you do better what you are trying to do: #34 |
Uh oh!
There was an error while loading. Please reload this page.
Hi,
I created a Datastore service, and updated a model extending JsonApiModel, as described in the doc.
Now, I don't know how to make the model works in a basic two-way databinding form.
In my component :
My Note model :
My view :
Before I change my model and make it inherit from JsonApiModel, everything worked fine. Each change I typed in the inputs was reflected in the model and displayed in the debug string interpolation.
Now Typescript gives me this error :
It comes from the
note: Note = new Note()
line.Seems normal as the JsonApiModel classes has its own constructor signature...
So my questions are :
I'd like to pass my model as the second argument and not have to explicitely describe each values (especially in a Service). How can I make it ?
Thx a lot for your help
The text was updated successfully, but these errors were encountered: