2015-05-30 11:56:00 -07:00
|
|
|
import {CONST_EXPR} from 'angular2/src/facade/lang';
|
2015-05-31 12:24:34 -07:00
|
|
|
import {EventEmitter, ObservableWrapper} from 'angular2/src/facade/async';
|
|
|
|
|
import {List, StringMapWrapper, StringMap} from 'angular2/src/facade/collection';
|
|
|
|
|
import {Directive, Ancestor, onDestroy, onChange} from 'angular2/angular2';
|
2015-05-30 11:56:00 -07:00
|
|
|
import {FORWARD_REF, Binding, Inject} from 'angular2/di';
|
|
|
|
|
|
|
|
|
|
import {ControlContainerDirective} from './control_container_directive';
|
|
|
|
|
import {ControlDirective} from './control_directive';
|
|
|
|
|
import {controlPath} from './shared';
|
2015-06-03 11:56:01 -07:00
|
|
|
import {Control} from '../model';
|
2015-05-30 11:56:00 -07:00
|
|
|
|
|
|
|
|
const controlNameBinding =
|
|
|
|
|
CONST_EXPR(new Binding(ControlDirective, {toAlias: FORWARD_REF(() => ControlNameDirective)}));
|
|
|
|
|
|
|
|
|
|
/**
|
2015-05-31 12:24:34 -07:00
|
|
|
* Binds a control with the specified name to a DOM element.
|
2015-05-30 11:56:00 -07:00
|
|
|
*
|
|
|
|
|
* # Example
|
|
|
|
|
*
|
2015-05-31 12:24:34 -07:00
|
|
|
* In this example, we bind the login control to an input element. When the value of the input
|
|
|
|
|
* element
|
2015-05-30 11:56:00 -07:00
|
|
|
* changes, the value of
|
|
|
|
|
* the control will reflect that change. Likewise, if the value of the control changes, the input
|
|
|
|
|
* element reflects that
|
|
|
|
|
* change.
|
|
|
|
|
*
|
|
|
|
|
* Here we use {@link formDirectives}, rather than importing each form directive individually, e.g.
|
|
|
|
|
* `ControlDirective`, `ControlGroupDirective`. This is just a shorthand for the same end result.
|
|
|
|
|
*
|
|
|
|
|
* ```
|
|
|
|
|
* @Component({selector: "login-comp"})
|
|
|
|
|
* @View({
|
|
|
|
|
* directives: [formDirectives],
|
2015-05-31 12:24:34 -07:00
|
|
|
* template:
|
2015-06-02 14:53:49 -07:00
|
|
|
* "<form [ng-form-model]='loginForm'>" +
|
|
|
|
|
* "Login <input type='text' ng-control='login'>" +
|
2015-05-31 12:24:34 -07:00
|
|
|
* "<button (click)="onLogin()">Login</button>" +
|
|
|
|
|
* "</form>"
|
2015-05-30 11:56:00 -07:00
|
|
|
* })
|
|
|
|
|
* class LoginComp {
|
2015-05-31 12:24:34 -07:00
|
|
|
* loginForm:ControlGroup;
|
2015-05-30 11:56:00 -07:00
|
|
|
*
|
|
|
|
|
* constructor() {
|
2015-05-31 12:24:34 -07:00
|
|
|
* this.loginForm = new ControlGroup({
|
|
|
|
|
* login: new Control(""),
|
|
|
|
|
* });
|
|
|
|
|
* }
|
|
|
|
|
*
|
|
|
|
|
* onLogin() {
|
|
|
|
|
* // this.loginForm.value
|
2015-05-30 11:56:00 -07:00
|
|
|
* }
|
|
|
|
|
* }
|
|
|
|
|
*
|
|
|
|
|
* ```
|
|
|
|
|
*
|
|
|
|
|
* @exportedAs angular2/forms
|
|
|
|
|
*/
|
|
|
|
|
@Directive({
|
2015-06-02 14:53:49 -07:00
|
|
|
selector: '[ng-control]',
|
2015-05-30 11:56:00 -07:00
|
|
|
hostInjector: [controlNameBinding],
|
2015-06-02 14:53:49 -07:00
|
|
|
properties: ['name: ng-control', 'model: ng-model'],
|
2015-05-31 12:24:34 -07:00
|
|
|
events: ['ngModel'],
|
2015-06-05 14:33:57 -07:00
|
|
|
lifecycle: [onDestroy, onChange],
|
|
|
|
|
exportAs: 'form'
|
2015-05-30 11:56:00 -07:00
|
|
|
})
|
|
|
|
|
export class ControlNameDirective extends ControlDirective {
|
|
|
|
|
_parent: ControlContainerDirective;
|
2015-05-31 12:24:34 -07:00
|
|
|
ngModel: EventEmitter;
|
|
|
|
|
model: any;
|
|
|
|
|
_added: boolean;
|
|
|
|
|
|
2015-05-30 11:56:00 -07:00
|
|
|
constructor(@Ancestor() _parent: ControlContainerDirective) {
|
|
|
|
|
super();
|
|
|
|
|
this._parent = _parent;
|
2015-05-31 12:24:34 -07:00
|
|
|
this.ngModel = new EventEmitter();
|
|
|
|
|
this._added = false;
|
2015-05-30 11:56:00 -07:00
|
|
|
}
|
|
|
|
|
|
2015-05-31 12:24:34 -07:00
|
|
|
onChange(c: StringMap<string, any>) {
|
|
|
|
|
if (!this._added) {
|
|
|
|
|
this.formDirective.addControl(this);
|
|
|
|
|
this._added = true;
|
|
|
|
|
}
|
|
|
|
|
if (StringMapWrapper.contains(c, "model")) {
|
|
|
|
|
this.formDirective.updateModel(this, this.model);
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-05-30 11:56:00 -07:00
|
|
|
|
2015-06-03 11:56:01 -07:00
|
|
|
|
2015-05-30 11:56:00 -07:00
|
|
|
onDestroy() { this.formDirective.removeControl(this); }
|
|
|
|
|
|
2015-05-31 12:24:34 -07:00
|
|
|
viewToModelUpdate(newValue: any): void { ObservableWrapper.callNext(this.ngModel, newValue); }
|
|
|
|
|
|
2015-05-30 11:56:00 -07:00
|
|
|
get path(): List<string> { return controlPath(this.name, this._parent); }
|
|
|
|
|
|
|
|
|
|
get formDirective(): any { return this._parent.formDirective; }
|
2015-06-03 11:56:01 -07:00
|
|
|
|
|
|
|
|
get control(): Control { return this.formDirective.getControl(this); }
|
2015-05-30 11:56:00 -07:00
|
|
|
}
|