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';
|
|
|
|
|
|
|
|
import {Directive, Ancestor, onChange} 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';
|
|
|
|
import {setUpControl} from './shared';
|
|
|
|
|
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],
|
|
|
|
properties: ['model: ng-model'],
|
|
|
|
events: ['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;
|
|
|
|
ngModel = new EventEmitter();
|
2015-05-31 12:24:34 -07:00
|
|
|
model: any;
|
|
|
|
|
|
|
|
onChange(c) {
|
|
|
|
if (!this._added) {
|
2015-06-16 15:53:52 -07:00
|
|
|
setUpControl(this._control, this);
|
2015-05-31 12:24:34 -07:00
|
|
|
this.control.updateValidity();
|
|
|
|
this._added = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
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 []; }
|
|
|
|
|
|
|
|
viewToModelUpdate(newValue: any): void { ObservableWrapper.callNext(this.ngModel, newValue); }
|
|
|
|
}
|