On Jun 8, 8:38 pm, Weyert de Boer <
wey...@gmail.com> wrote:
> What kind of code
> should be added to the fromUrl-selector of the object loader class
> e.g. People.
+ (id<TTPersistable>)fromURL:(NSURL*)url
{
// assume that url is "tt://people/2683"
// where the "tt" scheme has been registered as an internally-
dispatched
// url scheme with the TTNavigationCenter.
// and the class "People" has been registered as the object loader
for name "people".
// and the class "PeopleViewController" has been registered as the
view loader for view "people".
// and "2683" is a unique identifier of a specific person in the
recordset.
// take the path component of the URL and strip off the leading
slash, then convert it to an integer
int identifier = [[[url path] substringFromIndex:1] intValue];
return [People peopleForIdentifier:identifier];
}
+ (People *)peopleForIdentifier:identifier
{
// there are 2 situations possible:
// 1) all the data you need to create a "People" is already in-
memory (say, from a previous request to the server).
// 2) you do not have enough data, but can make a separate
request to your server to obtain the additional data.
//
// In either case, you should use |identifier| to do the lookup
#if 0
// For case 1, the identifier (2683) would be the index into the
array that was populated
// from your original request. So assuming that the data from the
original query was
// dumped into a dictionary for each person and stored in an
array |PeopleRecordSet|,
// you would do something like:
NSDictionary *personDict = [PeopleRecordSet
objectAtIndex:identifier];
return [[[People alloc] initWithDictionary:personDict]
autorelease];
#else
// For case 2, the identifier (2683) would be an id for a resource
on the server.
// and you would construct a new TTURLRequest to pull the data
down.
// Since TTNavigationCenter's object loader is designed to work in
a synchronous manner,
// you have no choice but to return a placeholder object from this
method
// after you send the new asynchronous request to get the
remaining data.
// Then when your request finishes, you could update (or replace)
the placeholder object,
// and notify any view observers that the model has changed.
// NOTE: that I do not implement the TTURLRequestDelegate methods
for you,
// that is up to you to figure out.
NSString *detailsURL = [NSURL URLWithString:[NSString
stringWithFormat:@"
http://myserver.com/api/people/%d", identifier]];
TTURLRequest *request = [TTURLRequest requestWithURL:detailsURL
delegate:self];
request.cachePolicy = TTURLRequestCachePolicyDefault;
request.response = [[[TTURLDataResponse alloc] init] autorelease];
request.httpMethod = @"GET";
[request send];
return [People dummyPerson];
#endif
}
-keith