In my Backbone.Collection I need to parse the response before to render it in Backbone.View. The following code works, but it will be great to have some suggestions:
// response is array of object
// [{id:1, prop: null },{id:2, prop: "bar" }]
// the output parsed_response can be
//[{id:1, prop: null, isProp: true },
// {id:2, prop: "bar", isProp: false }]
parse: function(response)
{
var parsed_response;
parsed_response = _(response).clone();
var parsed_response = _.map(parsed_response, function (obj) {
return function (_obj) {
if (_obj.prop === "" || typeof _obj.prop === "null" ) {
_obj.isProp = false;
}
else {
_obj.isProp = true;
}
return _obj;
}(obj)
});
return parsed_response;
}
My questions are:
- Is there a way to improve the code? Also renaming the name of variables to make it more clear.
- Since this code should be reused from other collections, what is the best way to generalise it?