92 lines
2.9 KiB
TypeScript
Raw Normal View History

import {CONST_EXPR} from 'angular2/src/facade/lang';
import {EventEmitter, ObservableWrapper} from 'angular2/src/facade/async';
import {OnChanges} from 'angular2/lifecycle_hooks';
import {SimpleChange} from 'angular2/src/core/change_detection';
import {Query, Directive} from 'angular2/src/core/metadata';
import {forwardRef, Provider, Inject, Optional} from 'angular2/src/core/di';
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from './control_value_accessor';
import {NgControl} from './ng_control';
2015-05-31 12:24:34 -07:00
import {Control} from '../model';
import {Validators, NG_VALIDATORS, NG_ASYNC_VALIDATORS} from '../validators';
import {
setUpControl,
isPropertyUpdated,
selectValueAccessor,
composeValidators,
composeAsyncValidators
} from './shared';
2015-05-31 12:24:34 -07:00
const formControlBinding =
CONST_EXPR(new Provider(NgControl, {useExisting: forwardRef(() => NgModel)}));
2015-05-31 12:24:34 -07:00
/**
* Binds a domain model to a form control.
*
* ### Usage
*
* `ng-model` binds an existing domain model to a form control. For a
* two-way binding, use `[(ng-model)]` to ensure the model updates in
* both directions.
*
* ### Example ([live demo](http://plnkr.co/edit/R3UX5qDaUqFO2VYR0UzH?p=preview))
* ```typescript
* @Component({
* selector: "search-comp",
* directives: [FORM_DIRECTIVES],
* template: `<input type='text' [(ng-model)]="searchQuery">`
* })
* class SearchComp {
* searchQuery: string;
* }
* ```
*/
2015-05-31 12:24:34 -07:00
@Directive({
selector: '[ng-model]:not([ng-control]):not([ng-form-control])',
bindings: [formControlBinding],
inputs: ['model: ngModel'],
outputs: ['update: ngModelChange'],
exportAs: 'form'
2015-05-31 12:24:34 -07:00
})
export class NgModel extends NgControl implements OnChanges {
/** @internal */
_control = new Control();
/** @internal */
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;
viewModel: any;
constructor(@Optional() @Inject(NG_VALIDATORS) private _validators: any[],
@Optional() @Inject(NG_ASYNC_VALIDATORS) private _asyncValidators: any[],
@Optional() @Inject(NG_VALUE_ACCESSOR) valueAccessors: ControlValueAccessor[]) {
super();
this.valueAccessor = selectValueAccessor(this, valueAccessors);
}
2015-05-31 12:24:34 -07:00
onChanges(changes: {[key: string]: SimpleChange}) {
2015-05-31 12:24:34 -07:00
if (!this._added) {
2015-06-16 15:53:52 -07:00
setUpControl(this._control, this);
this._control.updateValueAndValidity({emitEvent: false});
2015-05-31 12:24:34 -07:00
this._added = true;
}
2015-05-31 12:24:34 -07:00
if (isPropertyUpdated(changes, this.viewModel)) {
2015-06-16 15:53:52 -07:00
this._control.updateValue(this.model);
this.viewModel = this.model;
2015-05-31 12:24:34 -07:00
}
}
get control(): Control { return this._control; }
2015-06-16 15:53:52 -07:00
get path(): string[] { return []; }
2015-05-31 12:24:34 -07:00
get validator(): Function { return composeValidators(this._validators); }
get asyncValidator(): Function { return composeAsyncValidators(this._asyncValidators); }
viewToModelUpdate(newValue: any): void {
this.viewModel = newValue;
ObservableWrapper.callNext(this.update, newValue);
}
}