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-11-18 15:55:43 -08:00
|
|
|
import {
|
|
|
|
OnChanges,
|
|
|
|
SimpleChange,
|
|
|
|
Query,
|
|
|
|
Directive,
|
|
|
|
forwardRef,
|
|
|
|
Provider,
|
|
|
|
Inject,
|
2015-11-23 10:03:56 -08:00
|
|
|
Optional,
|
|
|
|
Self
|
2015-11-18 15:55:43 -08:00
|
|
|
} from 'angular2/core';
|
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-12-08 11:17:39 -08:00
|
|
|
exportAs: 'ngForm'
|
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-23 10:03:56 -08:00
|
|
|
constructor(@Optional() @Self() @Inject(NG_VALIDATORS) private _validators: any[],
|
|
|
|
@Optional() @Self() @Inject(NG_ASYNC_VALIDATORS) private _asyncValidators: any[],
|
|
|
|
@Optional() @Self() @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
|
|
|
|
refactor(lifecycle): prefix lifecycle methods with "ng"
BREAKING CHANGE:
Previously, components that would implement lifecycle interfaces would include methods
like "onChanges" or "afterViewInit." Given that components were at risk of using such
names without realizing that Angular would call the methods at different points of
the component lifecycle. This change adds an "ng" prefix to all lifecycle hook methods,
far reducing the risk of an accidental name collision.
To fix, just rename these methods:
* onInit
* onDestroy
* doCheck
* onChanges
* afterContentInit
* afterContentChecked
* afterViewInit
* afterViewChecked
* _Router Hooks_
* onActivate
* onReuse
* onDeactivate
* canReuse
* canDeactivate
To:
* ngOnInit,
* ngOnDestroy,
* ngDoCheck,
* ngOnChanges,
* ngAfterContentInit,
* ngAfterContentChecked,
* ngAfterViewInit,
* ngAfterViewChecked
* _Router Hooks_
* routerOnActivate
* routerOnReuse
* routerOnDeactivate
* routerCanReuse
* routerCanDeactivate
The names of lifecycle interfaces and enums have not changed, though interfaces
have been updated to reflect the new method names.
Closes #5036
2015-11-16 17:04:36 -08:00
|
|
|
ngOnChanges(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;
|
2015-11-15 23:58:59 -08:00
|
|
|
ObservableWrapper.callEmit(this.update, newValue);
|
2015-07-15 18:16:50 -07:00
|
|
|
}
|
2015-08-27 21:19:56 -07:00
|
|
|
}
|