Martin
Martin

Reputation: 795

Adding options programmatically to plugin function

Say I had the following code:

 framework7.actions([
            // First buttons group
            [
                // Group Label
                {
                    text: 'Here comes some optional description or warning for actions below',
                    label: true
                },
                // First button
                {
                    text: 'Alert',
                    onClick: function () {
                        framework7.alert('He Hoou!');
                    }
                },
                // Another red button
                {
                    text: 'Nice Red Button ',
                    red: true,
                    onClick: function () {
                        framework7.alert('You have clicked red button!');
                    }
                },
            ],
            // Second group
            [
                {
                    text: 'Cancel',
                    bold: true
                }
            ]
        ]);

In the above instance, how would I only add the option/code for "First button" if a certain condition was true. eg (if (need_first_button) {} ). If the condition is false then we don't pass that button to the plugin.

And the same goes for "Another red button". How could I only include that option when ( if (need_red_button) {} ) was true?

Hope that makes sense.

Thanks.

Upvotes: 0

Views: 74

Answers (1)

MrCode
MrCode

Reputation: 64526

Create the argument first, then modify it as needed, finally pass it to the framework function:

var params = [
            // First buttons group
            [
                // Group Label
                {
                    text: 'Here comes some optional description or warning for actions below',
                    label: true
                }
            ],
            // Second group
            [
                {
                    text: 'Cancel',
                    bold: true
                }
            ]
        ];

if(needFirstButton){
    params[0].push({
        text: 'Alert',
        onClick: function () {
            framework7.alert('He Hoou!');
        }        
    });
}

if(needRedButton){
        params[0].push({
        text: 'Nice Red Button ',
        red: true,
        onClick: function () {
            framework7.alert('You have clicked red button!');
        }       
    }); 
}

framework7.actions(params); 

You can easily add new groups, just by pushing an array to params instead of params[x]

params.push([
        {
            text: 'New group button 1',
            red: true,
            onClick: function () {
                framework7.alert('You have clicked red button!');
            }       
        }
]);

The above adds a new group containing one button, but you could add more buttons to it by comma separating the objects.

Upvotes: 1

Related Questions