CanJS Observable and dots in keys

人走茶凉 提交于 2019-12-11 12:27:31

问题


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' } }.



来源:https://stackoverflow.com/questions/13529342/canjs-observable-and-dots-in-keys

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!