Maintaining an order with associations


I’ve got a route planner app where a Ride hasMany Waypoints. I’m talking to a rails back end with JSONAPI. I want to offer the ability to insert a new waypoint between existing ones and also to remove waypoints and have the surrounding two re-order appropriately. I was thinking of either:

  1. Having some sort of re-ordering callback on the rails model that, when a new Waypoint record is persisted with a specified position attribute, it forces a recalculation of the position attribute for all subsequent Waypoints already part of the Ride.

  2. Manage this as part of the model creation function on Ember and update multiple local models, causing N subsequent update requests where N=number of waypoints after the newly inserted. Same for deletions.

This is purely subjective but the first option feels like the right thing to do because it feels more robust but it introduces the problem of having to get the changes back reflected in the Ember store. The latter feels bit fragile and potentially heavy on server traffic given how JSONAPI triggers one query per model updated.

I also don’t really know how well ember-data manages what would need to be websocket/push requests.

I’d be interested to hear if anyone has an opinion on the above and if there is a better way I may not have considered?

Thanks, Andy.

I wouldn’t say either of the options are bad per se, I kind of agree that option 1 feels more right but I’m also probably not a great person to consult on this so take that with a grain of salt.

What I can tell you with a little more certainty is that Ember handles socket updates pretty well. I’d probably use ember-websockets and then on updates do a store.pushPayload to smash the new data into the store. You could also look at something like emberfire to see how they’re doing data updates (if you’re not familiar, emberfire is the ember adapter for firebase which uses websockets to do “real-time” data updates).

Thanks for the input. I’ve also picked up a copy of Ember Data in the Wild which will make for some interesting bedtime reading no doubt.

I’ll have a read over your suggestions this weekend but if anyone else wants to venture an opinion in the meantime, feel free! :slight_smile: