1

I am trying to use multiple returns but just keep breaking the code. I have tried a few examples, but cant find the right combination.

How can I combine these two return statements into one?

$(".bar").popover({
content: 
    function (){
        return $(this).data('dataObj').status;
        return $(this).data('dataObj').timeline;
    }
});
2
  • Doesn't popover content attribute require a string? Commented Mar 14, 2013 at 14:04
  • just use return $(this).data('dataObj') Commented Mar 14, 2013 at 14:15

4 Answers 4

11

Use

function (){
    return $(this).data('dataObj');
}

OR

function (){
    // return an array
    return [ $(this).data('dataObj').status, $(this).data('dataObj').timeline ]
}

OR

function (){
    // return a associative array
    return { "status": $(this).data('dataObj').status, "timeline": $(this).data('dataObj').timeline }
}

And process the components in the caller.

Update

The content parameter for popover needs a string as argument, you can do this:

function (){
    return $(this).data('dataObj').status + " " + $(this).data('dataObj').timeline;
}
Sign up to request clarification or add additional context in comments.

4 Comments

-1 Who knows how much data is inside dataObj - there is no reason to return the whole object if you only need two of it's properties.
By your logic why not simply return the entire data() object.
I've removed my vote because you've added some better suggestions, but my comment still stands...
Returning dataObj is probably better. It's an already allocated object. It's already consuming memory. Creating a new array or object to return only certain values takes more memory. The only scenario where it would make sense to do that is when dataObj is very big and has no other references pointing to it.
4

Putting aside this specific case, where the plugin demands a certain type of return value (apparently a string in this case), you can't really... A return statement terminates the function. What you'll have to do is return an object (or an array) containing those two values -

var status = $(this).data('dataObj').status;
var timeline = $(this).data('dataObj').timeline;
return [status,timeline];

Or

var status = $(this).data('dataObj').status;
var timeline = $(this).data('dataObj').timeline;
var returnObj = {'status':status, 'timeline':timeline};
return returnObj;

Comments

2

You can return objext ir array containig those two items

$(".bar").popover({
content: 
    function (){
        return 
        {
        status: $(this).data('dataObj').status;
        timeline: $(this).data('dataObj').timeline;
        }
    }
});

1 Comment

This is a clean solution. But I want to understand the role that plays these colons in status: and timeline:, it seems to be case statements.
0

Try returning an array with .status and .timeline as elements. Ok Lix was faster.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.