2015-05-31 12:24:34 -07:00
|
|
|
import {CONST_EXPR} from 'angular2/src/facade/lang';
|
|
|
|
import {EventEmitter, ObservableWrapper} from 'angular2/src/facade/async';
|
|
|
|
import {StringMapWrapper} from 'angular2/src/facade/collection';
|
|
|
|
|
2015-06-17 14:45:40 -07:00
|
|
|
import {Directive, Ancestor, onChange, QueryList, Query} from 'angular2/angular2';
|
2015-06-11 18:49:07 -07:00
|
|
|
import {forwardRef, Binding} 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-06-17 14:45:40 -07:00
|
|
|
import {NgValidator} from './validators';
|
|
|
|
import {setUpControl, composeNgValidator} 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({
|
|
|
|
* directives: [formDirectives],
|
|
|
|
* template: `
|
|
|
|
<input type='text' [(ng-model)]="searchQuery">
|
|
|
|
* `})
|
|
|
|
* class SearchComp {
|
|
|
|
* searchQuery: string;
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @exportedAs angular2/forms
|
|
|
|
*/
|
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-05-31 12:24:34 -07:00
|
|
|
hostInjector: [formControlBinding],
|
2015-06-17 15:42:07 -07:00
|
|
|
properties: ['model: ngModel'],
|
|
|
|
events: ['update: ngModel'],
|
2015-06-05 14:33:57 -07:00
|
|
|
lifecycle: [onChange],
|
|
|
|
exportAs: 'form'
|
2015-05-31 12:24:34 -07:00
|
|
|
})
|
2015-06-10 13:51:44 -07:00
|
|
|
export class NgModel extends NgControl {
|
2015-06-16 15:53:52 -07:00
|
|
|
_control = new Control("");
|
|
|
|
_added = false;
|
2015-06-17 15:42:07 -07:00
|
|
|
update = new EventEmitter();
|
2015-05-31 12:24:34 -07:00
|
|
|
model: any;
|
2015-06-17 14:45:40 -07:00
|
|
|
ngValidators: QueryList<NgValidator>;
|
|
|
|
|
|
|
|
// Scope the query once https://github.com/angular/angular/issues/2603 is fixed
|
|
|
|
constructor(@Query(NgValidator) ngValidators: QueryList<NgValidator>) {
|
|
|
|
super();
|
|
|
|
this.ngValidators = ngValidators;
|
|
|
|
}
|
2015-05-31 12:24:34 -07:00
|
|
|
|
|
|
|
onChange(c) {
|
|
|
|
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
|
|
|
|
|
|
|
if (StringMapWrapper.contains(c, "model")) {
|
2015-06-16 15:53:52 -07:00
|
|
|
this._control.updateValue(this.model);
|
2015-05-31 12:24:34 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-16 15:53:52 -07:00
|
|
|
get control() { return this._control; }
|
|
|
|
|
2015-05-31 12:24:34 -07:00
|
|
|
get path(): List<string> { return []; }
|
|
|
|
|
2015-06-17 14:45:40 -07:00
|
|
|
get validator(): Function { return composeNgValidator(this.ngValidators); }
|
|
|
|
|
2015-06-17 15:42:07 -07:00
|
|
|
viewToModelUpdate(newValue: any): void { ObservableWrapper.callNext(this.update, newValue); }
|
2015-05-31 12:24:34 -07:00
|
|
|
}
|