strongloop-community / loopback-sdk-ios

iOS Client SDK for the LoopBack framework.
Other
76 stars 39 forks source link

Examples showing how to query for object relations from loopback SDK #2

Open abbasmousavi opened 10 years ago

abbasmousavi commented 10 years ago

I am trying to query a loopback server for relations between models, I have a "Section" Model and an "item" model, and a has me nay relation between them, so I can query for all items in a section with /sections/:id/items from a rest client.

How can I do the same query from loopback iOS client? any documentation or code examples?

ritch commented 10 years ago

This isn't supported yet in the SDK. You should be able to accomplish this by subclassing LBModel and adding a method similar to this...


- (void)relatedItems:(LBModelAllSuccessBlock)success
                   failure:(SLFailureBlock)failure {
    [self invokeMethod:@"items"
            parameters:@{}
               success:^(id value) {
                   NSAssert([[value class] isSubclassOfClass:[NSArray class]], @"Received non-Array: %@", value);

                   NSMutableArray *models = [NSMutableArray array];

                   [value enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                       [models addObject:[self modelWithDictionary:obj]];
                   }];

                   success(models);
               }
               failure:failure];
}

/cc @Schoonology @raymondfeng @mschmulen

baldoph commented 9 years ago

@ritch I can't get this to work. I have the very same goal and when I call -invokeMethod:@"items"... on my Section (: LBModel) the server returns a 404 status code: "message":"could not find a model with id prototype","statusCode":404,"code":"MODEL_NOT_FOUND" The URL the iOS SDK built is URL: http://0.0.0.0:3000/api/sections/prototype/items