fetchRelated after fetch using Backbone.Relational with Backbone.Marionette and Backbone.ioSync

Question!

I'm using Backbone.Relational with Backbone.Marionette and Backbone.ioSync to sync over socket.io.

More specifically, I have a model A that hasMany instances of mode B through relation bees. On load, all is well: data initialized and rendered appropriately. Then, another client adds another instance of B to A.bees. The server sends a ping to the first client, which causes it to call A.fetch(). In the success method of the fetch, I call A.fetchRelated('bees'). The fetch method returns only a list of IDs for the bees relation, triggering a change on A, causing Backbone.Marionatte's collection view to create a new instance of the item view to display the new B, which breaks the site since the new B hasn't been fetched yet.

Trying to force the rendering of B's view to sync on a deferred object defined in A sounds like a complex hack with likely many edge cases to handle.

One idea is to allow fetch to accept the silent option. But it's probably tricky to trigger the change and add events in the success function of fetch.

Another idea is to make the serialization code on the server context aware and send the entire object, not just the IDs, when a new item is added. This is also sub optimal as it may cause unnecessary transfer of entire identical B's when they're added to different A's. It also adds a lot of complexity to the server code as the context would need to propagate throughout the serialization process.

Any thoughts or suggestions of how to approach this? I am open to solutions that may require changes to Backbone.Marionette or Backbone.Relational as long as it works.



Answers

Marionette now has Marionette.Async that makes it easier to execute async fetch and fetchRelated requests. Further there was is a bug in Backbone-Relational that didn't respect the silent option passed automatically by backbone when saving an object with wait: true. See the https://github.com/PaulUithol/Backbone-relational/pull/153 for the pull request to fix this.



This video can help you solving your question :)
By: admin