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

collection.create doesn't save a model's children #25

Open
e2jk opened this issue Jun 30, 2015 · 6 comments
Open

collection.create doesn't save a model's children #25

e2jk opened this issue Jun 30, 2015 · 6 comments

Comments

@e2jk
Copy link

e2jk commented Jun 30, 2015

I have a model that has children (i.e. links to other instances of 2 other model types).
The models are kept in a collection.

When calling collection.create with the data from the POST, the model is created with empty children. If I edit the model (at which point a simple model.save is performed), using the same form, the model properly gets its children populated.

I've gotten around this for now by calling collection.create which creates the model without the children and adds it to my collection, and then within that success function I'm calling model.save with the same data from the post. This is how it looks like:

prepareView: function (el) {
    return new ObjectForm({
        el: el,
        model: this.model,
        submitCallback: function (data) {
            //The form is submitted, data contains the info from the form, including the children models
            app.objects.create(data, {
                wait: true,
                success: function (model, response, options) {
                    // The children are not properly saved when using app.objects.create, so save the model again
                    // Note that the same data from the form is used, which does properly already contain the children
                    model.save(data, {
                        wait: true,
                        success: function (model, response, options) {
                          //Do other stuff
                          app.navigate('/admin/objects');

I would expect collection.create to create the model including the children, as those are present just fine in the data retrieved from the form.

@e2jk
Copy link
Author

e2jk commented Jul 15, 2015

Hi folks, any idea how to perform this?

@aaronmccall
Copy link

@e2jk, can you post your model definition and example form data?

@e2jk
Copy link
Author

e2jk commented Jul 17, 2015

The model definition: http://pastebin.com/iMkFb3TG

When posting information on the page to create a new record, the following data is posted from the form:

{"abbreviation":"Test Object","objectType":"Interface","description":null,"notes":null,"fromDate":null,"toDate":null,"numberOfRecords":null,"relatedGroup":{"abbreviation":"","name":""},"fromSystem":{"systemName":"","vendorName":"","contactName":"","contactPhone":"","contactEmail":""},"toSystem":{"systemName":"","vendorName":"","contactName":"","contactPhone":"","contactEmail":""},"responsiblePerson":{"firstName":"","lastName":""}}

As you can see, all the data from the children is empty.
Since I'm saving again the model after it has just been created (again, with the same data from the form), a PUT command is issued just after that, which has all the children item populated:

{"id":13,"abbreviation":"Test Object","objectType":"Conversion","description":null,"notes":null,"fromDate":null,"toDate":null,"numberOfRecords":null,"relatedGroup":{"id":2,"abbreviation":"ENC","name":"Afspraak en Opname","objects":[4,5,6,7,8]},"fromSystem":{"id":9,"systemName":"SchedulePlus","vendorName":"","contactName":"","contactPhone":"","contactEmail":"","fromObjects":[]},"toSystem":{"id":11,"systemName":"New EPD","vendorName":"EPD vendor","contactName":"Toto van der Been","contactPhone":"9898989898","contactEmail":"[email protected]","fromObjects":[10],"toObjects":[7]},"responsiblePerson":{"id":5,"firstName":"Rob","lastName":"van Egmond"}}

I've added a debug console log just after submitCallback, and the JS object that is the data variable does have properties named as the children, which are themselves objects that contain the data. Example from this Firefox web tools screenshot:

capture du 2015-07-17 22 38 45

@e2jk
Copy link
Author

e2jk commented Jul 18, 2015

This is the text version of the screenshot (with another object I've just created). It's the output of console.log(JSON.stringify(data)); just after submitCallback is called.
You'll notice that the children data are sent themselves as objects (e.g. embedded objects). And as mentioned, when calling model.save with the same data in the success callback of app.objects.create, the model is properly saved (i.e. the issue is not in data, but likely in the way children are saved when calling collection.create whereas model.save works flawlessly):

19:30:17,492 "{"abbreviation":"XXX","objectType":"Conversion","description":"This is a test","notes":"These are notes","fromDate":null,"toDate":null,"numberOfRecords":null,"relatedGroup":{"id":2,"abbreviation":"ENC","name":"Afspraak en Opname","objects":[4,5,6,7,8]},"fromSystem":{"id":2,"systemName":"Radiologix","vendorName":"Radiologix","contactName":"David Seller","contactPhone":"123456789","contactEmail":"[email protected]"},"toSystem":{"id":3,"systemName":"PharmaPill","vendorName":"","contactName":"","contactPhone":"","contactEmail":""},"responsiblePerson":{"id":2,"firstName":"Marcel","lastName":"Eiffel","objects":[3,9,10]}}"1 project-de-brug.nonCached.js:911:1

@e2jk
Copy link
Author

e2jk commented Sep 21, 2015

Hi there, any thoughts about this issue?

@pgilad
Copy link
Member

pgilad commented Sep 21, 2015

I couldn't understand from your pastepin what you are trying to do. Try to create a minimal example that doesn't work. I sense that you may be mixing between children and collections.

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

No branches or pull requests

3 participants