CanJS Observable and dots in keys

2019-07-22 09:36发布

问题:

My problem is while using canJS Observable I can't use dots in object keys, because can think that some nesting available here.

So let's say if I create new observable:

var obs = new can.Observe( { "div.test-class": { "color": "#000000;" } } );

can fails with message

can.Observe: Object does not exist

And I can't create observable using just

var obs = new can.Observe( { ".test-class": { "color": "#000000;" } } );

because now can fails with the following error:

TypeError: current._set is not a function

Creating observable using following code

var obs = new can.Observe( { "div": {}, "div.test-class": { "color": "#000000;" } } );

works perfectly but I DON'T NEED nesting, and can tries to nest test-class into div inside observable.

So, any thoughts how I can achieve what I need?

回答1:

This was indeed a bug and has been fixed in version 1.1.5. The general rule now is:

var obs = new can.Observe( { "div": {}, "div.test-class": { "color": "#000000;" } } );

Will create the observe you expect. Passing an object to .attr like

obs.attr({ 'my.test': 'testing' });

Will also set my.test as the property. Passing it as a setter like

obs.attr('my.test', 'testing');

Will set { my: { test: 'testing' } }.