Skip to content Skip to sidebar Skip to footer

Backbone.js: Overwritting Tojson

I am trying to implement some sort of nested collections in a Backbone.Model To do this I have to overwrite the adapter functions which parse the server response and wrap the array

Solution 1:

Underscore's has does this:

has_.has(object, key)

Does the object contain the given key? Identical to object.hasOwnProperty(key), but uses a safe reference to the hasOwnProperty function, in case it's been overridden accidentally.

But your value won't have a toJSON property since toJSON comes from the prototype (see http://jsfiddle.net/ambiguous/x6577/).

You should use _(value.toJSON).isFunction() instead:

if(_(value.toJSON).isFunction()) {
    // execute toJSON and overwrite the value in attributes
    attributes[key] = value.toJSON();
}

Post a Comment for "Backbone.js: Overwritting Tojson"