├── .gitignore ├── README.md ├── bower.json ├── lazyModel.js └── test ├── angular-1.2.26.js ├── angular-1.3.0-rc.5.js ├── index.html └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | dev.html -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | lazy-model 2 | ========== 3 | 4 | AngularJS directive that works like `ng-model` but accept changes only when form is submitted (otherwise changes are cancelled). 5 | 6 | -------------------- 7 | 8 | ##UPDATE!!! 9 | As of Angular 1.3 there is [ngModelOptions](https://docs.angularjs.org/api/ng/directive/ngModelOptions) directive that allows to achive the same behavior natively 10 | ````html 11 |
16 | ```` 17 | jsfiddle: http://jsfiddle.net/8btk5/104/ 18 | So you don't need lazy-model any more! 19 | ---------------------- 20 | 21 | ### Why this is needed? 22 | AngularJS 2-way binding is good feature: you change model - and all views are updated instantly. 23 | But when dealing with forms I often need more transactional way: input something and accept changes or decline it. Official way to do it requires additional code in controller: create copy of model, link it with form and write changes back to original model when form is submitted (see http://docs.angularjs.org/guide/forms). 24 | Being too lazy, I tried to put all that stuff into **lazy-model** directive. 25 | 26 | ### How to use it? 27 | 1. Create form with **submit** and **reset** buttons 28 | 2. In controls use `lazy-model` instead of `ng-model` 29 | 30 | ````html 31 | 36 | ```` 37 | 38 | Now you can change username, but it will be saved to model only when you press **save**. 39 | If you press **cancel** - your changes will be declined. 40 | Try out demo: http://jsfiddle.net/8btk5/6/ 41 | 42 | It can be useful for **popup forms** and **modal dialogs.** 43 | For example, popup form: 44 | ````html 45 | 50 | 51 | ```` 52 | Live demo: http://jsfiddle.net/8btk5/7/ 53 | 54 | ### How to validate? 55 | Basically there are two ways of validation: 56 | 57 | #### 1. On-change validation (instant) 58 | Use normal AngularJS validation [described in docs](http://docs.angularjs.org/guide/forms). 59 | For example, `ng-maxlength` validator: 60 | ````html 61 |