mirror of
https://github.com/zhigang1992/RestKit.git
synced 2026-04-28 20:55:32 +08:00
Updates to the mapper
This commit is contained in:
@@ -94,7 +94,12 @@
|
||||
/**
|
||||
* Fetch a resource via an HTTP GET and invoke a callback with the model for the resulting payload
|
||||
*/
|
||||
- (OTRestRequest*)getAndMap:(NSString*)resourcePath delegate:(id)delegate callback:(SEL)callback;
|
||||
- (OTRestRequest*)getModel:(NSString*)resourcePath delegate:(id)delegate callback:(SEL)callback;
|
||||
|
||||
/**
|
||||
* Fetch a resource via an HTTP GET and invoke a callback with the model for the resulting payload
|
||||
*/
|
||||
- (OTRestRequest*)getModels:(NSString*)resourcePath delegate:(id)delegate callback:(SEL)callback;
|
||||
|
||||
/**
|
||||
* Register a model mapping from a domain model class to an XML element name
|
||||
|
||||
@@ -18,7 +18,7 @@ static OTRestClient* sharedClient = nil;
|
||||
|
||||
@implementation OTRestClient
|
||||
|
||||
@synthesize baseURL = _baseURL, username = _username, password = _password, HTTPHeaders = _HTTPHeaders;
|
||||
@synthesize baseURL = _baseURL, username = _username, password = _password, HTTPHeaders = _HTTPHeaders, mapper = _mapper;
|
||||
|
||||
+ (OTRestClient*)client {
|
||||
return sharedClient;
|
||||
@@ -116,9 +116,18 @@ static OTRestClient* sharedClient = nil;
|
||||
loader.delegate = delegate;
|
||||
loader.callback = callback;
|
||||
|
||||
return [self get:resourcePath delegate:loader callback:loader.mapperCallback];
|
||||
return [self get:resourcePath delegate:loader callback:loader.memberCallback];
|
||||
}
|
||||
|
||||
// Add a collection oriented accessor
|
||||
/**
|
||||
* Load a collection of models from a restful resource and invoke the callback
|
||||
*/
|
||||
- (OTRestRequest*)getModels:(NSString*)resourcePath delegate:(id)delegate callback:(SEL)callback {
|
||||
OTRestModelLoader* loader = [[OTRestModelLoader alloc] initWithMapper:self.mapper];
|
||||
loader.delegate = delegate;
|
||||
loader.callback = callback;
|
||||
|
||||
return [self get:resourcePath delegate:loader callback:loader.collectionCallback];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@@ -33,7 +33,12 @@
|
||||
/**
|
||||
* The method to invoke to trigger model mappings. Used as the callback for a restful model mapping request
|
||||
*/
|
||||
@property (nonatomic, readonly) SEL mapperCallback;
|
||||
@property (nonatomic, readonly) SEL memberCallback;
|
||||
|
||||
/**
|
||||
* The method to invoke to trigger model mappings. Used as the callback for a restful model mapping request
|
||||
*/
|
||||
@property (nonatomic, readonly) SEL collectionCallback;
|
||||
|
||||
|
||||
/**
|
||||
|
||||
@@ -20,13 +20,27 @@
|
||||
return self;
|
||||
}
|
||||
|
||||
- (SEL)mapperCallback {
|
||||
- (SEL)memberCallback {
|
||||
return @selector(loadModelFromXML:);
|
||||
}
|
||||
|
||||
- (SEL)collectionCallback {
|
||||
return @selector(loadModelsFromXML:);
|
||||
}
|
||||
|
||||
- (void)loadModelFromXML:(Element*)XML {
|
||||
id model = [_mapper buildModelFromXML:XML];
|
||||
[_delegate performSelector:self.callback withObject:model];
|
||||
}
|
||||
|
||||
- (void)loadModelsFromXML:(Element*)XML {
|
||||
NSMutableArray* models = [[[NSMutableArray alloc] init] autorelease];
|
||||
NSArray* elements = [XML syblingElements];
|
||||
for (Element* element in elements) {
|
||||
id model = [_mapper buildModelFromXML:element];
|
||||
[models addObject:model];
|
||||
}
|
||||
[_delegate performSelector:self.callback withObject:models];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user