Updating already existing event with relationship when a booking is saved in Ember
Asked Answered
M

2

16

I have two models:

Event.js:

export default Model.extend({
  checked       : attr({ defaultValue: false }),
  isActive      : attr(),
  createdAt     : attr(),
  updatedAt     : attr(),
  start         : attr(),
  end           : attr(),
  menPrice      : attr(),
  womenPrice    : attr(),
  information   : attr(),
  meetingPoint  : attr(),
  title         : attr(),
  hiw           : attr(),
  tip           : attr(),
  bookings      : hasMany('booking', { async: true})
});

and Booking.js:

export default Model.extend({
  checked       : attr({ defaultValue: false }),
  isActive      : attr(),
  createdAt     : attr(),
  updatedAt     : attr(),
  participants  : attr(),
  email         : attr(),
  locale        : attr(),
  name          : attr(),
  observation   : attr(),
  phoneNumber   : attr(),
  event         : belongsTo('event')
});

And I would like to create the relationship referring the bookings in my events, but I just can't figure out how!

How it works: When I create the event in my admin dashboard, there isn't bookings to refer, this only happens in the site, when the user makes a reservation.

I am using this code to save the booking (reservation) and refer the owner event:

let booking =  this.get('store').createRecord('booking', {
    event: this.get('event')
});

booking.save();

And it's working. That's how the booking looks like:

This is my booking JSON:

{
    "_id": {
        "$oid": "56beb58da080cf2c2d46065b"
    },
    "relationships": {
        "event": {
            "data": {
                "id": "56baa0cdd79cd63c7a0f0065",
                "type": "events"
            }
        }
    },
    "type": "bookings",
    "attributes": {
        "created-at": {
            "$date": "2016-02-13T04:48:13.489Z"
        },
        "gender": "null",
        "hostel": "null",
        "phone-number": "918918918118",
        "observation": "obs",
        "name": "Marcelo",
        "locale": "es",
        "email": "[email protected]",
        "participants": 10,
        "value": 0,
        "is-active": false,
        "vip": []
    },
    "__v": 0
}

As you can see, the relationship is working with the booking.

Now I need the opposite, I mean.. update the event with the booking inside .. but I can't, I just can't figure it out how! I'm stuck on this at least for 3 weeks.

I already tried several things, including using the embedded option, updating manually, but nothing works.

I appreciate the help!

Malvern answered 5/2, 2016 at 20:4 Comment(6)
You will have to use a callback function or promises. I recommend you read this page :) strongloop.com/strongblog/…Meroblastic
why is javascript tagged in this question, do you think the solution can be acheived in javascript?Coagulum
obvious, i'm using node.js.Malvern
what do you mean by updating event with booking inside ? a change in a booking will be reflected in event ? if so , how would this happen , and you haven't created this relation ?Floyfloyd
The exiftool tag on this question seems misplaced.Headwards
Maybe I understand the question wrongly but is there a point in actually saving the event with all the bookings ?Derian
B
1

The question is quiet old but let me give an answer anyways as it's highly upvoted.

Ember Data does not serialize the has-many site of one-to-many relationships by default in update requests. This could be customized by overriding shouldSerializeHasMany method of the responsible serializer.

Please be aware that Ember Data does not serialize the many-part of one-to-many relationships by default cause that is likely to cause race conditions and conflicts. Imagine two users A and B are fetching the current relationships from backend at the same time. A adds a new related record. A little bit later B adds another related record by replacing the existing relationships with a new list adding one resource. As B is not aware of the record added by A it will be silently removed. B overrides the change of A.

JSON:API specification supports relationship links to address these issue even for many-to-many and many-to-none relationships. But for many-to-one only updating one-side is the easiest solution.

Blench answered 26/2, 2020 at 21:48 Comment(0)
D
0

Maybe I understand the question wrongly but is there a point in actually saving the event with all the bookings ?

I mean usually for a one to many relationship like this one, the bookings will have an eventId pointing to the event they are related to but the event doesn't hold a reference to the bookings. Saving changes on an event should automatically be reflected on all the bookings referencing it.

Derian answered 13/12, 2016 at 16:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.