ahmet_y
ahmet_y

Reputation: 112

target.status value is not defined

var getOptArray = function (opt) {
    var len = opt.length;
    var res = [];
    for (var i = 0; i < len; i++) {
        res.push({text: (opt[i].text), value: opt[i].value, icon: opt[i].icon, color: opt[i].color});
    }
    return res;
}

var BLOG_CATEGORY_STATUS = {
    prop: {text: "status", defval: false, class: "", size: "text-sm", margin: "m-r-xs", editable: [1, 2], tooltip: true},
    publish: {text: "publishing", value: 1, icon: "fa fa-play", color: "green", question: "Publish"},
    pause: {text: "paused", value: 2, icon: "fa fa-pause", color: "warning", question: "Pause"},
    deleted: {text: "deleted", value: 3, icon: "fa fa-trash", color: "danger", question: "Delete"},
    optArr: function () {
        return getOptArray([this.publish, this.pause, this.deleted]);
    },
    /**
     * opt example -> {status: foo, categoryId: fooValue, callback: fooFunction}
     * @param {type} opt
     */
    buildButton: function (opt) {
        opt = opt || {};
        var optArray = this.optArr();
        var targetStatus = optArray[(opt.status) - 1];
        console.log(targetStatus);
        console.log(targetStatus.color); //prints "warning"
        console.log(targetStatus.question); //prints "undefined"

        var but = {}; 
        return but;
    }
};


$(function () { 
    BLOG_CATEGORY_STATUS.buildButton({categoryId: 1 
            , status: 2
            , callback: function () {
                console.log("test");
            }})
});

I edit code to add as question in jsfiddle. it is ready to run in jsfiddle

in buildButton function, targetStatus.color is warning, for the same object, targetStatus.question is undefined. Can't see what i am missing.

Upvotes: 1

Views: 49

Answers (1)

Jeto
Jeto

Reputation: 14927

You're probably missing the question data in the returned value from getOptArray:

res.push({
  text: opt[i].text, 
  value: opt[i].value, 
  icon: opt[i].icon, 
  color: opt[i].color, 
  question: opt[i].question    // <- here
});

Which, while you're at it, you should probably just rewrite this line as:

res.push(opt[i]);

Upvotes: 3

Related Questions