Access/Set an Observer Property With a Dot in the

2019-08-11 11:39发布

问题:

I believe the title & my simplified jsFiddle Example explains my predicament.

Basically I am dealing with an extremely large & multidimensional observer object containing folders & files, all of which have dynamic property names.

When it comes to using the Observer setProperty() function, I am struggling to find a way to use this on properties with a dot in the name such as "file.png". This is obviously because when the full stop appears in the set property path JSViews believes it to be a step deeper in the object tree.

Aka:

path.fileWithoutExt.attrs = Works.

path.fileWithExt.png.attrs = Fails.

So my question is "Is there a way to achieve property setting, or is it not possible or a feature request?" Could it be something like:

root.path.[file.dot].more

or:

root.path.{{file.dot}}.more

回答1:

You can't pass in paths like "object['keyName'].foo" as first parameter of setProperty. (That parameter does expect simply dot-separated paths).

But you don't need to. Just pass the actual object to $.views.observable(...) then you only need to pass the leaf property name to setProperty:

$.observable( files.images[n] ).setProperty( 'modified', 'Modified!' );
$.observable( files.images[n] ).setProperty( 'alias', a ); 

Updated jsfiddle: all three work...



回答2:

path["fileWithExt.png"].attrs = Works.