以编程方式向插件功能添加选项

时间:2014-05-17 13:55:29

标签: javascript jquery

说我有以下代码:

 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
                }
            ]
        ]);

在上面的例子中,如果某个条件为真,我如何只为“第一个按钮”添加选项/代码。例如(if(need_first_button){})。如果条件为假,那么我们不会将该按钮传递给插件。

“另一个红色按钮”也是如此。当(if(need_red_button){})为真时,我怎么能只包括那个选项?

希望这是有道理的。

感谢。

1 个答案:

答案 0 :(得分:1)

首先创建参数,然后根据需要进行修改,最后将其传递给框架函数:

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); 

只需将数组推送到params而不是params[x]

,即可轻松添加新群组
params.push([
        {
            text: 'New group button 1',
            red: true,
            onClick: function () {
                framework7.alert('You have clicked red button!');
            }       
        }
]);

上面添加了一个包含一个按钮的新组,但您可以通过逗号分隔对象来添加更多按钮。

相关问题