Search code examples
backbone.jsguididentifier

How to best make Backbone play nice with GUIDs?


I have a client-side application written with BackboneJS. It pulls its data from a server. The domain objects on the server identify themselves with Guids.

In Backbone, if a Model's id is not set to "null" then "isNew" returns false. So, I can't initialize my Model's id as Guid.Empty (instead of null) without side-effects.

When I fetch data from my server - the response has ids with values of Guid.Empty. Backbone automatically parses the server's response and triggers onChange events for all properties which change from "null" to Guid.Empty.

I'm wondering if anyone has any experience in handling this and what the right way to go about resolving this is. Should my Guids be nullable? Do I override the isNew() method and account for Guid.Empty? Do I stifle the onChange events on given conditions?


Solution

  • As I understand, Guid.Empty means no id and it's just a convention for naming? I suggest you to implement parse method and change Guid.Empty to null in all incoming objects. It looks to me more natural to keep null objects in JS as null, not as your backend represents it.