2015-08-20 14:28:25 -07:00
|
|
|
import {CONST_EXPR} from 'angular2/src/core/facade/lang';
|
|
|
|
import {EventEmitter, ObservableWrapper} from 'angular2/src/core/facade/async';
|
2015-05-31 12:24:34 -07:00
|
|
|
|
2015-08-14 10:03:45 -07:00
|
|
|
import {Query, Directive, LifecycleEvent} from 'angular2/metadata';
|
2015-09-02 10:24:22 -07:00
|
|
|
import {forwardRef, Binding, Inject, Optional} from 'angular2/di';
|
2015-05-31 12:24:34 -07:00
|
|
|
|
2015-06-10 13:51:44 -07:00
|
|
|
import {NgControl} from './ng_control';
|
2015-05-31 12:24:34 -07:00
|
|
|
import {Control} from '../model';
|
2015-09-02 10:24:22 -07:00
|
|
|
import {Validators, NG_VALIDATORS} from '../validators';
|
|
|
|
import {setUpControl, isPropertyUpdated} from './shared';
|
2015-05-31 12:24:34 -07:00
|
|
|
|
2015-06-11 18:50:41 -07:00
|
|
|
const formControlBinding = CONST_EXPR(new Binding(NgControl, {toAlias: forwardRef(() => NgModel)}));
|
2015-05-31 12:24:34 -07:00
|
|
|
|
2015-06-11 09:58:53 -07:00
|
|
|
/**
|
|
|
|
* Binds a domain model to the form.
|
|
|
|
*
|
|
|
|
* # Example
|
|
|
|
* ```
|
|
|
|
* @Component({selector: "search-comp"})
|
|
|
|
* @View({
|
2015-08-10 21:42:47 -07:00
|
|
|
* directives: [FORM_DIRECTIVES],
|
2015-06-11 09:58:53 -07:00
|
|
|
* template: `
|
|
|
|
<input type='text' [(ng-model)]="searchQuery">
|
|
|
|
* `})
|
|
|
|
* class SearchComp {
|
|
|
|
* searchQuery: string;
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*/
|
2015-05-31 12:24:34 -07:00
|
|
|
@Directive({
|
2015-06-02 14:53:49 -07:00
|
|
|
selector: '[ng-model]:not([ng-control]):not([ng-form-control])',
|
2015-07-29 15:01:22 -07:00
|
|
|
bindings: [formControlBinding],
|
2015-06-17 15:42:07 -07:00
|
|
|
properties: ['model: ngModel'],
|
|
|
|
events: ['update: ngModel'],
|
2015-08-27 21:19:56 -07:00
|
|
|
lifecycle: [LifecycleEvent.OnChanges],
|
2015-06-05 14:33:57 -07:00
|
|
|
exportAs: 'form'
|
2015-05-31 12:24:34 -07:00
|
|
|
})
|
2015-06-10 13:51:44 -07:00
|
|
|
export class NgModel extends NgControl {
|
2015-07-16 18:34:03 -07:00
|
|
|
_control = new Control();
|
2015-06-16 15:53:52 -07:00
|
|
|
_added = false;
|
2015-06-17 15:42:07 -07:00
|
|
|
update = new EventEmitter();
|
2015-05-31 12:24:34 -07:00
|
|
|
model: any;
|
2015-07-15 18:16:50 -07:00
|
|
|
viewModel: any;
|
2015-09-02 10:24:22 -07:00
|
|
|
validators: Function[];
|
2015-06-17 14:45:40 -07:00
|
|
|
|
2015-09-02 10:24:22 -07:00
|
|
|
constructor(@Optional() @Inject(NG_VALIDATORS) validators: Function[]) {
|
2015-06-17 14:45:40 -07:00
|
|
|
super();
|
2015-09-02 10:24:22 -07:00
|
|
|
this.validators = validators;
|
2015-06-17 14:45:40 -07:00
|
|
|
}
|
2015-05-31 12:24:34 -07:00
|
|
|
|
2015-08-27 21:19:56 -07:00
|
|
|
onChanges(c: StringMap<string, any>) {
|
2015-05-31 12:24:34 -07:00
|
|
|
if (!this._added) {
|
2015-06-16 15:53:52 -07:00
|
|
|
setUpControl(this._control, this);
|
2015-06-17 14:45:40 -07:00
|
|
|
this._control.updateValidity();
|
2015-05-31 12:24:34 -07:00
|
|
|
this._added = true;
|
2015-06-17 14:45:40 -07:00
|
|
|
}
|
2015-05-31 12:24:34 -07:00
|
|
|
|
2015-07-15 18:16:50 -07:00
|
|
|
if (isPropertyUpdated(c, this.viewModel)) {
|
2015-06-16 15:53:52 -07:00
|
|
|
this._control.updateValue(this.model);
|
2015-05-31 12:24:34 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-06 12:23:11 -07:00
|
|
|
get control(): Control { return this._control; }
|
2015-06-16 15:53:52 -07:00
|
|
|
|
2015-08-28 11:29:19 -07:00
|
|
|
get path(): string[] { return []; }
|
2015-05-31 12:24:34 -07:00
|
|
|
|
2015-09-02 10:24:22 -07:00
|
|
|
get validator(): Function { return Validators.compose(this.validators); }
|
2015-06-17 14:45:40 -07:00
|
|
|
|
2015-07-15 18:16:50 -07:00
|
|
|
viewToModelUpdate(newValue: any): void {
|
|
|
|
this.viewModel = newValue;
|
|
|
|
ObservableWrapper.callNext(this.update, newValue);
|
|
|
|
}
|
2015-08-27 21:19:56 -07:00
|
|
|
}
|