Re: Web scripting key does not work
Christiaan Hofman <[email protected]> Wed, 20 Oct 2010 23:22:29 +0200
| Newsgroups | gmane.comp.macosx.devel |
|---|---|
| Message-ID | <[email protected]> |
On Oct 20, 2010, at 18:52, aglee wrote: > On Oct 20, 2010, at 11:17 AM, Christiaan Hofman <[email protected]> wrote: >>> is its value the property value, or is it the *function* that retrieves the property value? >> >> >> This is the property, that should also be obvious from the JS statement I gave. As i said, I am talking about the key access, not the selector access. > > I understood what you were *trying* to get, it just wasn't clear to me how that could work. > The point is that the docs say that it can work, as it says you can treat Cocoa properties (as in KVC properties) as if they were JavaScript properties (which uses the dot notation). > My flawed assumption was that if calling the setter via setFoo_() worked, calling the getter via foo() must work too. But it doesn't, as I just checked. Dot notation for properties works as you'd expect. > Well, foo() also does work if you expose the foo selector to JS. Just properties in general do not work, they just happen in some cases, as I've now discovered. And indeed, you're right that is a documentation bug, because from reading the docs I get the impression that ALL Cocoa KVC compliant properties could be exposed. I'll file a bug report on that. >>> To illustrate, if in the JavaScript I do alert(window.myObject.foo), the alert message is: >>> >>> function foo() { >>> [native code] >>> } > > This was my fault. I forgot to implement isKeyExcludedFromWebScript:. Or rather, I forgot to uncomment it, having commented it out after some experiments a while back. Therefore "foo" didn't get the special "this is a property" treatment and the method -foo was assigned as the "foo" property of the JavaScript object. Once I fixed that, my JavaScript property access worked just like in the CallJS example. > > Christiaan, it looks to me as it looks to you: your code "should" absolutely work. The only things I can think of (in increasing order of desperation): > > * Make sure you aren't getting a JavaScript error, which might be silently killing your script. By doing this -- http://twitter.com/danielpunkass/status/7502286887 -- you can right-click the WebView and open a console that will log any JavaScript errors. Come to think of it, this is a quick way to test various JavaScript statements too on the fly. > > * Make sure isKeyExcludedFromWebScript: is being called, with an NSLog. > It is called, but not for the right key. > * Try having it return NO always, just for grins. > > And with that, I'm out of ideas, so I hope you (or someone else) figures it out. > > --Andy > Anyway, thanks for your comments, at least it pointed me to the cause. What happened is that the (bar) property I am trying to expose to JS does not correspond to an ivar, but to a generated value (in fact a value in another object; my exposed Foo object is mostly a wrapper to expose stuff to JS). And apparently web object scripting checks /ivars/ of the class, rather than (KVC) /properties/ of the class. Of course there is in general no reason why ivars and properties need to have any relation. Certainly, the docs are wrong on this point. And apparently what I want to do is not supported. Christiaan