Using can.js I have this control:
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);
}
});
.. and this markup:
<div class="instance1" data-newtext="newtext"></div>
<div class="instance2"></div>
..then if I create instances of that control like this:
var instance1 = new Test_Controller('.instance1', {});
var instance2 = new Test_Controller('.instance2', {});
What I would expect to see is 2 divs, one with the word newtext
inserted and the other with the word nested2
but what I actually see is 2 divs with the word newtext
inserted.
If I change my options object so that it doesn't use nesting, but puts all the options at the top level, then I have no problem.
So it would seem that canJS doesn't merge nested objects correctly when merging options with defaults. Is this the case? Does anyone have any clever ideas how I might be able to support this feature without forking? Or am I missing something obvious? It'd save me having to write lots of laborious options creation code if I could do this.