CanJS Observable和键中的点 [英] CanJS Observable and dots in keys
问题描述
我的问题是在使用canJS Observable时,我不能在对象键中使用点,因为可以认为这里有一些嵌套。
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.Observe: Object does not exist
我无法创建只需使用
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;" } } );
工作正常,但我不需要嵌套,可以尝试嵌套将测试类
放入可观察的 div
中。
works perfectly but I DON'T NEED nesting, and can tries to nest test-class
into div
inside observable.
所以,任何想法都可以
推荐答案
这确实是一个错误,并已在1.1.5版中修复。现在的一般规则是:
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;" } } );
将创建您期望的观察。将对象传递给 .attr
,例如
Will create the observe you expect. Passing an object to .attr
like
obs.attr({ 'my.test': 'testing' });
还将 my.test
设置为属性。作为设置器传递,例如
Will also set my.test
as the property. Passing it as a setter like
obs.attr('my.test', 'testing');
将设置 {my:{test:'testing'}}
。
这篇关于CanJS Observable和键中的点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!