Are you sure you're talking Javascript, and not Java?
I'm used to valueForKey from Apple Objective C and Java code. It's a pattern/idea originally imported from NextStep/Objective C, and put into Apple classes even in Java. something.valueForKey(key) is nothing more or less than calling something.key() (except I think it'll default to something.key for public instance variables when no method is available), but let's you do it in a way that conveniently ignores Java's form of strong typing. That is, it's not actually pure Java, but relies on some Apple classes being loaded to provide the behavior.
As far as the Java method, they are documented here:
http://developer.apple.com/mac/library/documentation/cocoa/reference/foundation/Protocols/NSKeyValueCoding_Protocol/Reference/Reference.html (ObjectiveC documentation, but you can extrapolate to Java. I know that at least at one point these methods were provided on Apple library objects in Java too, but I'm not up to date on the state of Apple OS development in Java these days).
I'm not familiar with them in Javascript. Perhaps on the iPhone, Apple provides similar methods in Javascript? Since Javascript isn't strongly typed like Java to begin with, less neccesary to avoid the inconvenience of Java strong typing, but perhaps still useful, for instance so you can easily pass this kind of "method invocation instruction" as a string, or store it in a config file.
Note if it were the Java/ObjectiveC methods we were talking about, as you can see from the documentation, you would not be able to call valueForKey("some.key.path") -- you would need to instead use valueForKeyPath() for that. And something.valueForKeyPath("some.key.path") is no more and no less than: something.some().key().path() , it's just a convenience. (in Java letting you avoid the class-casting at every step of invocation; in any language letting you easily pass around method invocation instructions as data, or store them in config files).
Are you sure these methods exist in iPhone Javascript in the first place? I am not familiar with iPhone development or apple-specific Javascript, but googling around I only see valueForKey in reference to Java or ObjectiveC, as per the documentation cited above.
Jonathan
________________________________________
From: Code for Libraries [[log in to unmask]] On Behalf Of Francis Kayiwa [[log in to unmask]]
Sent: Saturday, March 27, 2010 9:33 PM
To: [log in to unmask]
Subject: Re: [CODE4LIB] valueforkey in javascript
On 3/27/10 6:05 PM, Eric Lease Morgan wrote:
> Does anybody here know how to use the valueForKey method (or some other method) in Javascript, specifically for the iPhone and iPod Touch?
>
> I have successfully defined a datasource (an XML stream). Here is an XML snippet:
>
> <waters>
> <water>
> <name water_id='79'>Agean Sea at Kos, Greece</name>
> </water>
> <water>
> <name water_id='37''>Amazon River, Peru</name>
> </water>
> <water>
> <name water_id='100'>Atlantic Ocean</name>
> </water>
> </waters>
>
> I am able to successfully extract the values for each water's name, like this:
>
> function coolHandler(event)
> {
> var d = dashcode.getDataSource("list");
> var n = d.selection().valueForKey("name");
> alert( n );
> }
>
> The result of this handler is an echoing of the value for the name element.
>
> Unfortunately, I want to get the value for the water_id attribute, but I can't for the life of me figure out how to access it.
>
I confess I am not the JavaScript expert, but I suspect the reason
you're having difficulty is because an element attribute is usually
considered a child of the element. Were I to hazard a guess, you'd need
to look at something like:
function coolHandler(event)
{
var d = dashcode.getDataSource("list");
var n = d.selection().valueForKey("name.water_id");
alert( n );
}
--
The Ranger isn't gonna like it, Yogi.
|