.extend将值更改为NaN

时间:2015-09-02 09:57:08

标签: javascript jquery backbone.js

我正在使用骨干模型来检索一些数据。 在我的视图中,我有一个配置对象,我想用我模型中的数据进行扩展。

现在几乎(几乎是)一切都按计划进行。 除了对象中从100到NaN的一个值。

要扩展我使用jQuery.extend的对象。 在延伸之后,数据在NaN之后是正确的。

以下是代码:

extendData: function () {
    var opt = this.model.get('Data'),
    $.extend(this.config, opt);
},

config: {
    circleThickness: 0.08,
    animationTime: 2000,
    textSize: 1,
    labelSize: 0.5
}

这是model.attributes.Data中的数据:

minValue: 0,
maxValue: 100,
value: 42,
displayUnit: true,
unit: "%"

这是我在扩展后控制台登录时的对象:

{
    circleThickness: 0.08,
    animationTime: 2000,
    textSize: 1,
    labelSize: 0.5
    minValue: 0,
    maxValue: NaN,
    value: 42,
    displayUnit: true,
    unit: "%"
}

正如你所看到的,maxValue现在突然变成了NaN ......

我已经尝试了以下内容:

this.config = $.extend(true, this.config, opt);

this.config = $.extend({}, this.config, opt);

this.config.extend(opt);

this.config = $.extend(this.createConfig(), opt);
createConfig: function () {
    return {
        circleThickness: 0.08,      // The outer circle thickness as a percentage of it's radius.
        innerCircleThickness: 0.25, // The inner circle thickness as a percentage of it's radius.
        circleFillGap: 0.05,        // The size of the gap between the outer circle and wave circle as a percentage of the outer circles radius.
        animationTime: 2000,        // The amount of time in milliseconds for the transition to take place.
        textVertPosition: .5,       // The height at which to display the percentage text withing the wave circle. 0 = bottom, 1 = top.
        textSize: 1,                // The relative height of the text to display in the wave circle. 1 = 50%
        labelSize: 0.5             // The relative height of the label text to display outside the circle 1 = 25%
    };
}

现在我只是尝试创建一个变量,然后将此值放在变量中并且它可以正常工作。当我想将这些信息放在骨干视图中时,它再次提供了一个NaN。这是解释我的胡言乱语的代码:

//declared config
config: {},

//changed config to basicConfig:
basicConfig: {
    circleThickness: 0.08,
    animationTime: 2000,
    textSize: 1,
    labelSize: 0.5
}

//changed function to:
extendData: function () {
    var opt = this.model.get('Data'),
    var config = $.extend({}, this.basicConfig, opt);

    console.log(config); //this shows maxValue = 100

    this.config = config;
    console.log(this.config); //this shows maxValue = NaN

}

所以当我把数据放在骨干视图中时......它从100变为NaN?

这里是在模型中检索数据的方式。

initialize: function () {
    this.getData();
}
getData: function () {

    var self = this;
    var key = this.get('itemKey');

    var data = {
        name: globals.name,
        key: key,
        isFunction: this.get('itemIsFunction').toString().toLowerCase() === 'true',
        latitude: app.location.get('latitude'),
        longitude: app.location.get('longitude')
    };

    this.fetch({
        dataType: 'jsonp',
        //dataType: 'json',
        crossDomain: true,
        data: data
    });
}
parse: function (response) {

    if (response) {
        var utcPublishdate = new Date(response.PublishDate);

        var day = utcPublishdate.getDate();
        var month = utcPublishdate.getUTCMonth() + 1; //months are zero based
        var year = utcPublishdate.getUTCFullYear();
        var hour = utcPublishdate.getUTCHours();

        var minute = ('0' + utcPublishdate.getUTCMinutes()).slice(-2);
        response.PublishDate = (day + "-" + month + "-" + year);
        response.PublishTime = (hour + ":" + minute);

        if (response.Data && !_.isObject(response.Data)) {
            response.Data = JSON.parse(response.Data);
        }

        if (response.DataTile && !_.isObject(response.DataTile)) {
            response.DataTile = JSON.parse(response.DataTile);
        }
    }

    return response;
},

导致此问题的原因是什么? 我该如何预防?

所有帮助将不胜感激

0 个答案:

没有答案