我的问题是在使用 canJS Observable 时我不能在对象键中使用点,因为可以认为这里可以使用一些嵌套。
假设我创建新的可观察对象:
var obs = new can.Observe( { "div.test-class": { "color": "#000000;" } } );
可以失败并显示消息
can.Observe: Object does not exist
而且我无法仅使用
创建可观察对象var obs = new can.Observe( { ".test-class": { "color": "#000000;" } } );
因为现在可能会失败并出现以下错误:
TypeError: current._set is not a function
使用以下代码创建可观察对象
var obs = new can.Observe( { "div": {}, "div.test-class": { "color": "#000000;" } } );
工作完美,但我不需要嵌套,并且可以尝试将 test-class
嵌套到 observable 内的 div
中。
那么,有什么想法可以实现我的需求吗?
最佳答案
这确实是一个错误 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
作为属性。像 setter 一样传递它
obs.attr('my.test', 'testing');
将设置 { my: { test: 'testing' } }
。
关于javascript - CanJS Observable 和键中的点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13529342/