reggie
reggie

Reputation: 3674

Backbone datatypes - type casting?

It just took me over an hour to find out that a Backbone query on a collection was failing because I queried the wrong data type. So this query failed because I used the wrong data type for id:

element = collection.findWhere({id: "123", att: true});

This one worked and returned a model from the collection.

element = collection.findWhere({id: 123, att: true});

The reason was that I took the id from a DOM element.

I am interfacing a lot with the DOM in my application. Is there an option in Backbone that would allow me to make it more lenient in regards to data types?

Upvotes: 0

Views: 276

Answers (1)

One option is to override your models parse method and so a toString on the id coming in. Then you'll have strings from the dom and in your model.

http://backbonejs.org/#Model-parse

Personally I'd prefer to explicitly cast my queries instead using parseInt but they're both options

Upvotes: 1

Related Questions