能.控件.默认值和选项合并

can.Control.defaults and options merging

本文关键字:选项 合并 默认值 控件      更新时间:2023-09-26

使用可以.js我有这个控件:

var Test_Controller = can.Control({
    defaults: {
        option1: 'option1',
        option2: 'option2',
        option3: {
            nested1: 'nested1',
            nested2: 'nested2',
            nested3: 'nested3'
        }
    }
}, {
    init: function() {
        if ($(this.element).attr('data-newtext')) {
            this.options.option3.nested2 = $(this.element).data('newtext');
        }
        $(this.element).text(this.options.option3.nested2);
    }
});

.. 和这个标记:

<div class="instance1" data-newtext="newtext"></div>
<div class="instance2"></div>

..那么如果我像这样创建该控件的实例:

var instance1 = new Test_Controller('.instance1', {});
var instance2 = new Test_Controller('.instance2', {});

我希望看到的是 2 个div,一个插入了单词 newtext,另一个插入了单词 nested2,但我实际看到的是 2 个插入了单词newtext的div。

如果我更改我的选项对象,使其不使用嵌套,而是将所有选项放在顶层,那么我没有问题。

因此,在将选项与默认值合并时,canJS 似乎没有正确合并嵌套对象。是这样吗?有没有人有任何聪明的想法,我如何在不分叉的情况下支持此功能?还是我错过了一些明显的东西?如果我能做到这一点,这将节省我编写大量费力的选项创建代码。

正如@tikider指出的,can.extend使用原生的 .extend 库(在大多数情况下可能是$.extend ),默认情况下不执行深度克隆。但是,您应该能够覆盖控件设置并进行深度扩展:

var Test_Controller = can.Control({
    defaults: {
        option1: 'option1',
        option2: 'option2',
        option3: {
            nested1: 'nested1',
            nested2: 'nested2',
            nested3: 'nested3'
        }
    }
}, {
    setup: function( element, options ) {
        var allOptions = $.extend(true, {}, this.constructor.defaults, options);
        return can.Control.prototype.setup.call(this, element, allOptions);
    },
    init: function() {
        if (this.element.attr('data-newtext')) {
            this.options.option3.nested2 = this.element.data('newtext');
        }
        this.element.text(this.options.option3.nested2);
    }
});