Created
September 3, 2009 14:06
-
-
Save jfahrenkrug/180314 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@import <Foundation/CPObject.j> | |
@import "TripDaysController.j" | |
@import "Trip.j" | |
@import "DemoData.j" | |
@import "Settings.j" | |
@implementation TripsController : CPObject | |
{ | |
TripDaysController tripDaysController @accessors; | |
CPTextField tripLabel @accessors; | |
CPString receivedData; | |
CPURLConnection loadConnection; | |
} | |
- (id)init | |
{ | |
self = [super init]; | |
if (self) | |
{ | |
receivedData = @""; | |
loadConnection = nil; | |
} | |
return self; | |
} | |
- (void)loadTrip:(CPString)number { | |
if (number && [number length] == 7) { | |
var request = [CPURLRequest requestWithURL:[Settings baseURL] + "/maps/1" + number + "00.json"]; | |
[request setHTTPMethod:@"GET"]; | |
[request setValue:"application/json" forHTTPHeaderField:"Accept"]; | |
receivedData = nil; | |
loadConnection = [CPURLConnection connectionWithRequest:request delegate:self]; | |
} else { | |
alert("Bitte eine 7-stellige Reisenummer eingeben!"); | |
} | |
} | |
- (void)loadExampleTrip:(CPString)number { | |
var json = [[DemoData exampleJSONString] objectFromJSON]; | |
var trips = [Trip tripsFromJSONObjects:json]; | |
if (number && [number length] == 7) { | |
trip = nil; | |
// This will later be replaced by the real JSON-finding mechanism | |
for (i = 0; i < [trips count]; i += 1) { | |
var current_trip = [trips objectAtIndex:i]; | |
if ([current_trip identifier] == '1' + number + '00') { | |
trip = current_trip; | |
break; | |
} | |
} | |
if (trip) { | |
selectedTrip = trip; | |
[tripLabel setStringValue:[trip title]]; | |
[tripDaysController setTrip:trip]; | |
} else { | |
alert("Nicht gefunden."); | |
} | |
} else { | |
alert("Bitte eine 7-stellige Reisenummer eingeben!"); | |
} | |
} | |
/* CPURLConnection delegate methods */ | |
- (void)connection:(CPURLConnection)connection didReceiveData:(CPString)data | |
{ | |
if (!receivedData) { | |
receivedData = data; | |
} else { | |
receivedData += data; | |
} | |
} | |
- (void)connection:(CPURLConnection)connection didFailWithError:(CPString)error | |
{ | |
alert("Connection did fail with error : " + error) ; | |
receivedData = nil; | |
} | |
- (void)connectionDidFinishLoading:(CPURLConnection)aConnection | |
{ | |
console.log("Connection did finish loading.") ; | |
if (aConnection===loadConnection) | |
{ | |
var result = CPJSObjectCreateWithJSON(receivedData); | |
var trip = [[Trip alloc] initFromJSONObject:result]; | |
if (trip) { | |
selectedTrip = trip; | |
[tripLabel setStringValue:[trip title]]; | |
[tripDaysController setTrip:trip]; | |
} else { | |
alert("Nicht gefunden."); | |
} | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment