如何更新代码以避免"未定义的值"?

时间:2016-01-11 12:33:15

标签: javascript

最近我正在开发一个带有此扩展的项目,我注意到当我选择" FIELD"选项并输入一切正常。但是在我从字段中删除值并推送因为未定义的值而进入整个应用程序崩溃。也许有人可以帮我代码如何避免这种烦人的崩溃?

提前谢谢!

这是完整的代码。

/*global define*/
define(["qlik"], function(qlik) {
    'use strict';
    var BTN_SELECTED = 'qui-button-selected',
        BTN = 'qui-button',
        SELECT = 'qui-select',
         INPUT = 'qui-input';

    function createVariable(name) {
        var app = qlik.currApp();
        //from 2.1: check if variable exists
        if (app.variable.getByName) {
            app.variable.getByName(name).then(function() {
                //variable already exist
            }, function() {
                //create variable
                app.variable.create(name);
            });
        } else {
            //create variable - ignore errors
            app.variable.create(name);
        }
    }

    function createElement(tag, cls, html) {
        var el = document.createElement(tag);
        if (cls) {
            el.className = cls;
        }
        if (html !== undefined) {
            el.innerHTML = html;
        }
        return el;
    }
    return {
        initialProperties: {
            variableValue: {},
            variableName: "",
            render: "f",
            defaultValue: 0,
            alternatives: []
        },
        definition: {
            type: "items",
            component: "accordion",
            items: {
                settings: {
                    uses: "settings",
                    items: {
                        variable: {
                            type: "items",
                            label: "Variable",
                            items: {
                                name: {
                                    ref: "variableName",
                                    label: "Name",
                                    type: "string",
                                    change: function(data) {
                                        createVariable(data.variableName);
                                        data.variableValue.qStringExpression = '=' + data.variableName;
                                    }
                                },
                                render: {
                                    type: "string",
                                    component: "dropdown",
                                    label: "Render as",
                                    ref: "render",
                                    options: [{
                                        value: "b",
                                        label: "Button"
                                    }, {
                                        value: "s",
                                        label: "Select"
                                    }, {
                                        value: "f",
                                        label: "Field"
                                    }, {
                                        value: "l",
                                        label: "Slider"
                                    }],
                                    defaultValue: "f"
                                },
                                emptyVal: {
                                    ref: "emptyVal",
                                    label: "default(when empty)",
                                    type: "string",
                                    defaultValue: 0
                                },
                                alternatives: {
                                    type: "array",
                                    ref: "alternatives",
                                    label: "Alternatives",
                                    itemTitleRef: "label",
                                    allowAdd: true,
                                    allowRemove: true,
                                    addTranslation: "Add Alternative",
                                    items: {
                                        value: {
                                            type: "string",
                                            ref: "value",
                                            label: "Value"
                                        },
                                        label: {
                                            type: "string",
                                            ref: "label",
                                            label: "Label",
                                            expression: "optional"
                                        }
                                    },
                                    show: function(data) {
                                        return data.render === "b" || data.render === "s";
                                    }
                                },
                                min: {
                                    ref: "min",
                                    label: "Min",
                                    type: "number",
                                    defaultValue: 0,
                                    show: function(data) {
                                        return data.render === "l";
                                    }
                                },
                                max: {
                                    ref: "max",
                                    label: "Max",
                                    type: "number",
                                    defaultValue: 100,
                                    show: function(data) {
                                        return data.render === "l";
                                    }
                                },
                                step: {
                                    ref: "step",
                                    label: "Step",
                                    type: "number",
                                    defaultValue: 1,
                                    show: function(data) {
                                        return data.render === "l";
                                    }
                                }
                            }
                        }
                    }
                }
            }
        },
        paint: function($element, layout) {
            var wrapper = createElement('div'),
                ext = this;
            empty = layout.emptyVal;
            if (layout.render === 'b') {
                layout.alternatives.forEach(function(alt) {
                    var clazz = alt.value === layout.variableValue ? BTN_SELECTED : BTN;
                    var btn = createElement('button', clazz, alt.label);
                    btn.onclick = function() {
                        qlik.currApp(ext).variable.setContent(layout.variableName, alt.value);
                    }
                    wrapper.appendChild(btn);
                });
            } else if (layout.render === 's') {
                var sel = createElement('select', SELECT);
                layout.alternatives.forEach(function(alt) {
                    var opt = createElement('option', undefined, alt.label);
                    opt.value = alt.value;
                    opt.selected = alt.value === layout.variableValue;
                    sel.appendChild(opt);
                });
                sel.onchange = function() {
                    qlik.currApp(ext).variable.setContent(layout.variableName, this.value);
                }
                wrapper.appendChild(sel);
            } else if (layout.render === 'l') {
                var range = createElement('input');
                range.type = 'range';
                range.min = layout.min || 0;
                range.max = layout.max || 100;
                range.step = layout.step || 1;
                range.value = layout.variableValue;
                range.title = layout.variableValue;
                range.style.width = '98%';
                range.onchange = function() {
                    qlik.currApp(ext).variable.setContent(layout.variableName, this.value);
                }
                wrapper.appendChild(range);
            } else {
                var fld = createElement('input', INPUT);
                fld.type = 'number';
                fld.value = layout.variableValue;
                fld.onchange = function () {
                    if (this.value != "undifined") {
                        qlik.currApp(ext).variable.setContent(layout.variableName, this.value);
                    } else {
                        qlik.currApp(ext).variable.setContent(layout.variableName, 0);
                    }
                }
                wrapper.appendChild(fld);
            }
            var elem = $element[0];
            if (elem.childNodes.length === 0) {
                elem.appendChild(wrapper);
            } else {
                elem.replaceChild(wrapper, elem.childNodes[0]);
            }
        }
    };

});

1 个答案:

答案 0 :(得分:0)

if (this.value != "undifined") {

在这里,您要将value字符串 "undifined"进行比较(也是错误的)。

您似乎想要与 undefined进行比较:

if (this.value !== undefined) {

或检查value的数据类型:

if (typeof this.value != "undefined") {