2
\$\begingroup\$

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:

  1. Is there a way to improve the code? Also renaming the name of variables to make it more clear.
  2. Since this code should be reused from other collections, what is the best way to generalise it?
\$\endgroup\$
3
\$\begingroup\$

Based on the way your parse function works you're worried about mutability. Your code can be much shorter. Also, your use of _.clone is wasted since it's only a shallow copy. I've rewritten parse code and now it looks like this:

// 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)
{
    return _.map(response, function(obj) {
        obj = _.clone(obj);
        obj.isProp = obj.prop !== "" && obj.prop !== null;
        return obj;
    });
}

If you want further explanation let me know :)

The only thing I'm concerned with is how your prop works? Can you just check for a falsey value here (i.e. !obj.prop) or do you really need to explicitly check for an empty string or null?

In term of re-usability, you're covered here. parse() doesn't reference this so it can more or less be called statically from anywhere in your code. But be careful where you use it from, if you start calling this all over the place, it would make more sense to refactor this parse() function to a more generic object.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Just one question: What is the goalp of writing obj = _.clone(obj); since the result is the same? \$\endgroup\$ – underscore999 May 15 '12 at 4:54
  • \$\begingroup\$ Calling clone will modify cloned objects, leaving the original response objects alone. You have to do it on the each object since _.clone will only shallow copy. If you don't care about modifying the original response, take the clone line out and switch to _.each. \$\endgroup\$ – Cobby May 15 '12 at 5:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.