Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Modified the LBModelRepository to allow filter responses with the inc… #116

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions LoopBack/LBModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,11 @@
*/
- (LBModel *)modelWithDictionary:(NSDictionary *)dictionary;

/**
* Accesses the singleton dictionary of all model repositories
*
* @return A dictionary of model
*/
+ (NSMutableDictionary*) repositoriesDict;

@end
23 changes: 23 additions & 0 deletions LoopBack/LBModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ - (instancetype)initWithClassName:(NSString *)name {
if (!self.modelClass) {
self.modelClass = [LBModel class];
}

// Add this repository to the dictionary of all repositories
[LBModelRepository.repositoriesDict setObject:self forKey:name];
}

return self;
Expand Down Expand Up @@ -130,6 +133,16 @@ - (LBModel *)modelWithDictionary:(NSDictionary *)dictionary {
else if (strncmp(type, "T@\"CLLocation\",", 15) == 0) {
obj = [SLObject locationFromEncodedProperty:obj];
}
} else if ([obj isKindOfClass:[NSArray class]])
{
LBModelRepository* repository = [LBModelRepository.repositoriesDict objectForKey:key];
NSMutableArray* tmp = [NSMutableArray array];
for (NSDictionary* dict in obj)
{
LBModel* model = [repository modelWithDictionary:dict];
[tmp addObject:model];
}
obj = tmp;
}

@try {
Expand All @@ -145,4 +158,14 @@ - (LBModel *)modelWithDictionary:(NSDictionary *)dictionary {
return model;
}

+ (NSMutableDictionary *) repositoriesDict {
static NSMutableDictionary *g_modelsDict = nil;
if (g_modelsDict == nil) {
// create dict
g_modelsDict = [[NSMutableDictionary alloc] init];
}
return g_modelsDict;
}


@end
4 changes: 4 additions & 0 deletions LoopBack/LBPersistedModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,8 @@ typedef void (^LBPersistedModelFindOneSuccessBlock)(LBPersistedModel *model);
success:(LBPersistedModelAllSuccessBlock)success
failure:(SLFailureBlock)failure;

- (void)findWithFilterString:(NSString *) filter
success: (LBPersistedModelAllSuccessBlock)success
failure:(SLFailureBlock)failure;

@end
20 changes: 20 additions & 0 deletions LoopBack/LBPersistedModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,26 @@ - (void)findWithFilter:(NSDictionary *) filter
failure:failure];
}

- (void)findWithFilterString:(NSString *) filter
success: (LBPersistedModelAllSuccessBlock)success
failure:(SLFailureBlock)failure {
if(!filter) {
filter = @{};
}
[self invokeStaticMethod:@"find"
parameters:@{@"filter": filter}
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];
}


@end