DjebbZ
DjebbZ

Reputation: 1604

Backbone model default values : null ? empty string ? empty array?

In my Backbone app, models are instantiated when I retrieve data from an HTTP JSON API. Values vary from this service, so I decided for consistency to provide defaults so all the models share at least the same base attributes. Useful for templating for instance.

I'm just wondering : what's the best value to assign to each attribute ? Some will be strings, some numbers, others arrays. Not sure if I should assign null by default for everyone (this is what I'm doing now), or if I should use the empty string "" for future strings and the empty array [] for future arrays.

Upvotes: 1

Views: 4844

Answers (1)

freejosh
freejosh

Reputation: 11383

I usually assign null values. I'm unlikely to use null as an actual value to pass around so if something in my application remains null I can tell that something went wrong somewhere.

By the way be careful when assigning empty arrays or objects via the defaults attribute. When you do this the array/object is referenced in each instance, instead of copied to each instance, of your model so they will all modify the same data.

Upvotes: 3

Related Questions