Created
July 31, 2012 18:30
-
-
Save kevinwestern/3219275 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
| #source('Model.dart'); | |
| void main() { | |
| Map<String, Dynamic> m = new Map(); | |
| m['name'] = 'Kevin'; | |
| Model simpleModel = new Model(m); | |
| simpleModel.set('name', 'kevin').set('age', 24).save().save(); | |
| print('done'); | |
| } | |
| class Model { | |
| Map<String, Dynamic> data; | |
| Model(this.data){} | |
| Dynamic operator [](String field) => this.get(field); | |
| Dynamic get(field) => data[field]; | |
| void operator []=(String field, Dynamic value) => this._setVoid(field, value); | |
| void _setVoid(field, value) { | |
| data[field] = value; | |
| } | |
| // So we can chain, e.g: person.set('name', 'kevin').set('age', '24').save() | |
| Model set(field, value) { | |
| this._setVoid(field, value); | |
| return this; | |
| } | |
| Model save () { | |
| if (this.get('id') === null) { | |
| return this.createModel(); | |
| } else { | |
| return this.updateModel(); | |
| } | |
| } | |
| Model createModel () { | |
| print('model created'); | |
| this.set('id', 1); | |
| return this; | |
| } | |
| Model updateModel () { | |
| print('model updated'); | |
| return this; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment