2015-11-06 17:34:07 -08:00
|
|
|
import {CONST_EXPR} from 'angular2/src/facade/lang';
|
|
|
|
import {EventEmitter, ObservableWrapper} from 'angular2/src/facade/async';
|
2015-09-02 16:43:39 -07:00
|
|
|
import {OnChanges} from 'angular2/lifecycle_hooks';
|
2015-09-23 11:43:31 -07:00
|
|
|
import {SimpleChange} from 'angular2/src/core/change_detection';
|
2015-09-03 22:01:36 -07:00
|
|
|
import {Query, Directive} from 'angular2/src/core/metadata';
|
2015-10-10 22:11:13 -07:00
|
|
|
import {forwardRef, Provider, Inject, Optional} from 'angular2/src/core/di';
|
2015-09-30 17:52:33 -07:00
|
|
|
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from './control_value_accessor';
|
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-11-02 10:00:42 -08:00
|
|
|
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
|
|
|
|
2015-10-10 22:11:13 -07:00
|
|
|
const formControlBinding =
|
2015-10-12 11:30:34 -07:00
|
|
|
CONST_EXPR(new Provider(NgControl, {useExisting: forwardRef(() => NgModel)}));
|
2015-05-31 12:24:34 -07:00
|
|
|
|
2015-06-11 09:58:53 -07:00
|
|
|
/**
|
2015-09-23 13:28:50 -07:00
|
|
|
* Binds a domain model to a form control.
|
2015-06-11 09:58:53 -07:00
|
|
|
*
|
2015-11-17 09:41:31 -08:00
|
|
|
* ### Usage
|
2015-09-23 13:28:50 -07:00
|
|
|
*
|
|
|
|
* `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
|
2015-10-11 07:41:19 -07:00
|
|
|
* @Component({
|
|
|
|
* selector: "search-comp",
|
2015-08-10 21:42:47 -07:00
|
|
|
* directives: [FORM_DIRECTIVES],
|
2015-09-01 20:59:43 -07:00
|
|
|
* template: `<input type='text' [(ng-model)]="searchQuery">`
|
|
|
|
* })
|
2015-06-11 09:58:53 -07:00
|
|
|
* 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-09-30 20:59:23 -07:00
|
|
|
inputs: ['model: ngModel'],
|
2015-10-10 19:56:22 -07:00
|
|
|
outputs: ['update: ngModelChange'],
|
2015-06-05 14:33:57 -07:00
|
|
|
exportAs: 'form'
|
2015-05-31 12:24:34 -07:00
|
|
|
})
|
2015-08-31 18:32:32 -07:00
|
|
|
export class NgModel extends NgControl implements OnChanges {
|
2015-10-09 17:21:25 -07:00
|
|
|
/** @internal */
|
2015-07-16 18:34:03 -07:00
|
|
|
_control = new Control();
|
2015-10-09 17:21:25 -07:00
|
|
|
/** @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;
|
2015-07-15 18:16:50 -07:00
|
|
|
viewModel: any;
|
2015-06-17 14:45:40 -07:00
|
|
|
|
2015-11-02 10:00:42 -08:00
|
|
|
constructor(@Optional() @Inject(NG_VALIDATORS) private _validators: any[],
|
|
|
|
@Optional() @Inject(NG_ASYNC_VALIDATORS) private _asyncValidators: any[],
|
2015-09-30 17:52:33 -07:00
|
|
|
@Optional() @Inject(NG_VALUE_ACCESSOR) valueAccessors: ControlValueAccessor[]) {
|
2015-06-17 14:45:40 -07:00
|
|
|
super();
|
2015-09-30 17:52:33 -07:00
|
|
|
this.valueAccessor = selectValueAccessor(this, valueAccessors);
|
2015-06-17 14:45:40 -07:00
|
|
|
}
|
2015-05-31 12:24:34 -07:00
|
|
|
|
2015-10-02 16:47:54 -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);
|
2015-10-27 11:20:07 -07:00
|
|
|
this._control.updateValueAndValidity({emitEvent: false});
|
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-09-01 20:59:43 -07:00
|
|
|
if (isPropertyUpdated(changes, this.viewModel)) {
|
2015-06-16 15:53:52 -07:00
|
|
|
this._control.updateValue(this.model);
|
2015-08-18 21:42:11 -07:00
|
|
|
this.viewModel = 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-11-02 10:00:42 -08:00
|
|
|
get validator(): Function { return composeValidators(this._validators); }
|
|
|
|
|
|
|
|
get asyncValidator(): Function { return composeAsyncValidators(this._asyncValidators); }
|
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
|
|
|
}
|