我的问题是在使用canJS Observable时我不能在对象键中使用点,因为可以认为这里有一些嵌套。
所以,假设我创建了新的observable:
var obs = new can.Observe( { "div.test-class": { "color": "#000000;" } } );
可能会失败并显示消息
can.Observe: Object does not exist
我无法使用
创建observablevar obs = new can.Observe( { ".test-class": { "color": "#000000;" } } );
因为现在可能会因以下错误而失败:
TypeError: current._set is not a function
使用以下代码创建observable
var obs = new can.Observe( { "div": {}, "div.test-class": { "color": "#000000;" } } );
完美无缺,但我不需要嵌套,并且可以尝试将test-class
嵌套到div
内的可观察内容中。
那么,有什么想法可以实现我的需要吗?
答案 0 :(得分:2)
这确实是一个错误and has been fixed in version 1.1.5。现在的一般规则是:
var obs = new can.Observe( { "div": {}, "div.test-class": { "color": "#000000;" } } );
将创建您期望的观察。将对象传递给.attr
,如
obs.attr({ 'my.test': 'testing' });
还会将my.test
设置为属性。将其作为像
obs.attr('my.test', 'testing');
将设置{ my: { test: 'testing' } }
。