https://github.com/derekli66/babymodel
An easy model to have abilities to monitor property changes.
https://github.com/derekli66/babymodel
block dynamic kvo model objc-runtime objective-c
Last synced: 12 months ago
JSON representation
An easy model to have abilities to monitor property changes.
- Host: GitHub
- URL: https://github.com/derekli66/babymodel
- Owner: derekli66
- License: mit
- Created: 2016-06-13T20:16:00.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2018-08-16T06:58:28.000Z (over 7 years ago)
- Last Synced: 2025-01-14T10:32:11.935Z (about 1 year ago)
- Topics: block, dynamic, kvo, model, objc-runtime, objective-c
- Language: Objective-C
- Homepage:
- Size: 56.6 KB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
## Description
BabyModel is a model class to help create model class easily. Normally, MVC pattern ask us to create a signal structure for Model-View-Controller that don't let Model-View have strong coupling. But, hooking up the signal structure through KVO is a little bit.... not so convenient. Therefore, that comes with this BabyModel for easy to use.
## What We Have
### OBModel
OBModel is a basic class that help you create your own model subclass with desired properties.
### OBCollection
OBCollection subclassed from OBModel and provides collection changes monitoring.
### KVO Behind the Scene
Yes. They are all backed up by KVO behind the scene.
## How to Use
### Make a Subclass
```objc
// Create your own model class
@interface VGModel : OBModel
@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) UIImage *backgroundImage;
@end
```
```objc
// Start monitor property change in your viewcontroller or elsewhere
VGModel *aModel = [[VGModel alloc] init];
aModel.title = @"Bob";
aModel.backgroundImage = [UIImage imageNamed:@"green.png"];
[aModel addObserver:self forProperty:@"title" withEvent:^(NSString * _Nonnull propertyName, NSDictionary * _Nonnull changes) {
NSLog(@"Change property: %@, changes: %@", propertyName, changes);
}];
[aModel addObserver:self forProperty:@"backgroundImage" withEvent:^(NSString * _Nonnull propertyName, NSDictionary * _Nonnull changes) {
NSLog(@"I got the images: %@", changes);
}];
self.testModel = aModel;
```