Skip to content

Commit acdd8f2

Browse files
committed
* Upgrade to Angular RC6
* Refactor code based on changes from RC5 to RC6 * Use NgModule now in both examples and README.md since bootstrap() is deprecated * Load .js files through Babel because RC6 exports ES6 JS code
1 parent fae9743 commit acdd8f2

27 files changed

+267
-147
lines changed

.babelrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": ["es2015"]
3+
}

README.md

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -52,34 +52,44 @@ approaches, but if you are already using
5252
it into your project, then you would do something like this:
5353

5454
```typescript
55-
import { provideFormConnect } from 'ng2-redux-form';
56-
57-
const ngRedux = new NgRedux<AppState>();
58-
59-
ngRedux.configureStore(reducer, myInitialState, [], []);
60-
61-
bootstrap(MyApplication, [
62-
provide(NgRedux, {useValue: ngRedux}),
63-
provideForms(),
64-
disableDeprecatedForms(),
65-
provideFormConnect(ngRedux)
66-
]);
55+
@NgModule({
56+
imports: [
57+
BrowserModule,
58+
ReactiveFormsModule,
59+
FormsModule,
60+
NgReduxForms,
61+
],
62+
providers: [
63+
NgRedux,
64+
],
65+
bootstrap: [MyApplicationComponent]
66+
})
67+
export class ExampleModule {}
6768
```
6869

6970
Or if you are using Redux without ng2-redux, then your bootstrap call would look
7071
more like this (substitute your own store creation code):
7172

7273
```typescript
73-
import { provideFormConnect } from 'ng2-redux-form';
74+
import {provideFormConnect} from 'ng2-redux-form';
7475

7576
const storeCreator = compose(applyMiddleware(logger))(createStore);
7677
const store = create(reducers, <MyApplicationState> {});
7778

78-
bootstrap(MyApplication, [
79-
disableDeprecatedForms(),
80-
provideForms(),
81-
provideFormConnect(store),
82-
]);
79+
@NgModule({
80+
imports: [
81+
BrowserModule,
82+
ReactiveFormsModule,
83+
FormsModule,
84+
NgReduxForms,
85+
],
86+
providers: [
87+
NgRedux,
88+
provideFormConnect(store),
89+
],
90+
bootstrap: [MyApplicationComponent]
91+
})
92+
export class ExampleModule {}
8393
```
8494

8595
(Note that in these examples, we are explicitly disabling the old Angular 2 forms
@@ -218,7 +228,7 @@ your data when the user updates form inputs, then you should use this default re
218228
To use it, you need to combine it with your existing reducers like so:
219229

220230
```typescript
221-
import { composeReducers, defaultFormReducer } from 'ng2-redux-form';
231+
import {composeReducers, defaultFormReducer} from 'ng2-redux-form';
222232

223233
const reducer = composeReducers(
224234
defaultFormReducer(),

examples/.babelrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../.babelrc

examples/module-links.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ fi;
88
pkgs=`cat <<EOF
99
@angular
1010
awesome-typescript-loader
11+
babel
1112
babel-core
13+
babel-loader
1214
babel-polyfill
1315
immutable
1416
jasmine-core

examples/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
"preinstall": "./module-links.sh"
99
},
1010
"devDependencies": {
11+
"@angular/upgrade": "^2.0.0-rc.6",
12+
"cross-env": "^2.0.0",
1113
"html-webpack-plugin": "^2.22.0",
1214
"raw-loader": "^0.5.1",
13-
"cross-env": "^2.0.0",
1415
"webpack-dev-server": "^1.14.1"
1516
}
1617
}

examples/source/index.ts

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,27 @@ import 'zone.js/dist/zone';
44
import 'zone.js/dist/long-stack-trace-zone';
55
import 'ts-helpers';
66

7-
import {
8-
provideForms,
9-
REACTIVE_FORM_DIRECTIVES,
10-
disableDeprecatedForms,
11-
FormGroup,
12-
} from '@angular/forms';
13-
import { Component, provide } from '@angular/core';
14-
import { bootstrap } from '@angular/platform-browser-dynamic';
7+
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
8+
import {BrowserModule} from '@angular/platform-browser';
9+
import {Component, NgModule} from '@angular/core';
10+
import {FormsModule, ReactiveFormsModule} from '@angular/forms';
11+
12+
import {Map, fromJS} from 'immutable';
1513

16-
import { Map, fromJS } from 'immutable';
14+
import {NgRedux, select} from 'ng2-redux';
1715

18-
import { NgRedux, select } from 'ng2-redux';
16+
import {combineReducers} from 'redux';
1917

20-
import { combineReducers } from 'redux';
18+
import {
19+
Connect,
20+
ConnectArray,
21+
FormStore,
22+
NgReduxForms,
23+
composeReducers,
24+
defaultFormReducer,
25+
} from '../../source';
2126

22-
import { Connect } from '../../source/connect';
23-
import { ConnectArray } from '../../source/connect-array';
24-
import { composeReducers } from '../../source/compose-reducers';
25-
import { defaultFormReducer } from '../../source/form-reducer';
26-
import { provideFormConnect } from '../../source/configure';
27-
import { logger } from '../../source/tests.utilities';
27+
import {logger} from '../../source/tests.utilities';
2828

2929
@Component({
3030
selector: 'form-example',
@@ -74,11 +74,6 @@ import { logger } from '../../source/tests.utilities';
7474
</div>
7575
</div>
7676
`,
77-
directives: [
78-
Connect,
79-
ConnectArray,
80-
REACTIVE_FORM_DIRECTIVES,
81-
],
8277
styles: [require('./index.css')]
8378
})
8479
export class FormExample {
@@ -96,10 +91,6 @@ export class FormExample {
9691
type: 'ADD_FORM_ENTRY'
9792
});
9893
}
99-
100-
stringify(obj) {
101-
return JSON.stringify(obj);
102-
}
10394
}
10495

10596
@Component({
@@ -117,7 +108,6 @@ export class FormExample {
117108
</li>
118109
</ul>
119110
`,
120-
directives: [Connect, REACTIVE_FORM_DIRECTIVES]
121111
})
122112
export class TodoExample {
123113
@select(s => s.todos.get('list')) private list;
@@ -142,9 +132,12 @@ export class TodoExample {
142132
<form-example></form-example>
143133
<todo-example></todo-example>
144134
`,
145-
directives: [FormExample, TodoExample]
146135
})
147-
export class Example {}
136+
export class Example {
137+
constructor(private ngRedux: NgRedux<AppState>) {
138+
ngRedux.configureStore(reducer, {form1, todos}, [logger], []);
139+
}
140+
}
148141

149142
interface AppState {
150143
form1?: {
@@ -215,13 +208,23 @@ function todoReducer(state = todos, action: {type: string, payload?}) {
215208
return state;
216209
}
217210

218-
const ngRedux = new NgRedux<AppState>();
219-
220-
ngRedux.configureStore(reducer, {form1, todos}, [logger], []);
211+
@NgModule({
212+
imports: [
213+
BrowserModule,
214+
ReactiveFormsModule,
215+
FormsModule,
216+
NgReduxForms,
217+
],
218+
declarations: [
219+
FormExample,
220+
TodoExample,
221+
Example,
222+
],
223+
providers: [
224+
NgRedux,
225+
],
226+
bootstrap: [Example]
227+
})
228+
export class ExampleModule {}
221229

222-
bootstrap(Example, [
223-
provide(NgRedux, {useValue: ngRedux}),
224-
disableDeprecatedForms(),
225-
provideForms(),
226-
provideFormConnect(ngRedux),
227-
]);
230+
platformBrowserDynamic().bootstrapModule(ExampleModule);

examples/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"compilerOptions": {
33
"module": "commonjs",
4-
"target": "es2015",
4+
"target": "es6",
55
"moduleResolution": "node",
66
"emitDecoratorMetadata": true,
77
"experimentalDecorators": true,

index.d.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
declare namespace 'ng2-redux-form' {
2-
import { Provider } from '@angular/core';
3-
import { QueryList } from '@angular/core';
4-
import { NgForm, NgControl } from '@angular/forms';
5-
import { Action, Store } from 'redux';
6-
import { NgRedux } from 'ng2-redux';
7-
import { Iterable } from 'immutable';
2+
import {Provider} from '@angular/core';
3+
import {QueryList} from '@angular/core';
4+
import {NgForm, NgControl} from '@angular/forms';
5+
import {Action, Store} from 'redux';
6+
import {NgRedux} from 'ng2-redux';
7+
import {Iterable} from 'immutable';
88

99
export const provideFormConnect: <T>(arg: Store<T> | NgRedux<T>) => Provider[];
1010

package.json

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22
"name": "ng2-redux-form",
33
"description": "Build Angular 2 forms with Redux",
44
"devDependencies": {
5-
"@angular/common": "^2.0.0-rc.5",
6-
"@angular/compiler": "^2.0.0-rc.5",
7-
"@angular/core": "^2.0.0-rc.5",
8-
"@angular/forms": "^0.3.0",
9-
"@angular/platform-browser": "^2.0.0-rc.5",
10-
"@angular/platform-browser-dynamic": "^2.0.0-rc.5",
5+
"@angular/common": "2.0.0-rc.6",
6+
"@angular/compiler": "2.0.0-rc.6",
7+
"@angular/core": "2.0.0-rc.6",
8+
"@angular/forms": "2.0.0-rc.6",
9+
"@angular/platform-browser": "2.0.0-rc.6",
10+
"@angular/platform-browser-dynamic": "2.0.0-rc.6",
1111
"awesome-typescript-loader": "1.1.1",
1212
"babel-core": "^6.11.4",
13+
"babel-loader": "^6.2.5",
14+
"babel-preset-es2015": "^6.14.0",
1315
"chai": "^3.5.0",
1416
"cross-env": "^2.0.0",
1517
"immutable": "^3.8.1",
@@ -33,7 +35,7 @@
3335
"rxjs": "^5.0.0-beta.6",
3436
"ts-helpers": "^1.1.1",
3537
"typings": "^1.0",
36-
"webpack": "^1.13.1",
38+
"webpack": "1.13.1",
3739
"webpack-dev-middleware": "^1.6.1",
3840
"zone.js": "^0.6.12"
3941
},

source/compose-reducers.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { expect } from 'chai';
1+
import {expect} from 'chai';
22

33
import {
44
fromJS,
@@ -8,7 +8,7 @@ import {
88
Set
99
} from 'immutable';
1010

11-
import { composeReducers } from './compose-reducers';
11+
import {composeReducers} from './compose-reducers';
1212

1313
describe('composeReducers', () => {
1414
const compose = (s1, s2, s3) => {

source/compose-reducers.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { Reducer, Action } from 'redux';
1+
import {Reducer, Action} from 'redux';
22

3-
import { Iterable } from 'immutable';
3+
import {Iterable} from 'immutable';
44

5-
import { FormException } from './form-exception';
6-
import { State } from './state';
5+
import {FormException} from './form-exception';
6+
import {State} from './state';
77

88
export const composeReducers =
99
<State>(...reducers: Reducer<State>[]): Reducer<State> => {

source/configure.ts

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,34 @@
1-
import {
2-
Provider,
3-
provide,
4-
} from '@angular/core';
1+
import {FormControl} from '@angular/forms';
2+
import {NgRedux} from 'ng2-redux';
53

6-
import { FormControl } from '@angular/forms';
7-
import { NgRedux } from 'ng2-redux';
8-
import { Store } from 'redux';
4+
import {
5+
Action,
6+
Store,
7+
} from 'redux';
98

10-
import { AbstractStore, FormStore } from './form-store';
9+
import {
10+
AbstractStore,
11+
FormStore,
12+
} from './form-store';
1113

12-
export const provideFormConnect = <T>(arg: Store<T> | NgRedux<T>) => {
13-
const abstractStore: AbstractStore<T> = {
14-
dispatch: action => arg.dispatch(action),
15-
getState: () => arg.getState(),
16-
subscribe: fn => arg.subscribe(() => fn(arg.getState()))
17-
};
14+
/// Use this function in your providers list if you are not using ng2-redux.
15+
/// This will allow you to provide a preexisting store that you have already
16+
/// configured, rather than letting ng2-redux creating one for you.
17+
export const provideReduxForms = <T>(store: Store<T> | NgRedux<T>) => {
18+
const abstractStore = wrap(store);
1819

1920
return [
20-
provide(FormStore, {
21-
useValue: new FormStore(abstractStore)
22-
})
21+
{provide: FormStore, useValue: new FormStore(<any> abstractStore)}
2322
];
2423
};
24+
25+
const wrap = <T>(store: Store<T> | NgRedux<T>): AbstractStore<T> => {
26+
const dispatch = (action: Action) => store.dispatch(action);
27+
28+
const getState = () => <T> store.getState();
29+
30+
const subscribe =
31+
(fn: (state: T) => void) => store.subscribe(() => fn(store.getState()));
32+
33+
return {dispatch, getState, subscribe};
34+
};

source/connect-array.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ import {
3636
NG_VALIDATORS
3737
} from '@angular/forms/src/validators';
3838

39-
import { Subscription } from 'rxjs';
39+
import {Subscription} from 'rxjs';
4040

41-
import { Connect } from './connect';
42-
import { FormStore } from './form-store';
43-
import { State } from './state';
41+
import {Connect} from './connect';
42+
import {FormStore} from './form-store';
43+
import {State} from './state';
4444

4545
export class ConnectArrayTemplate {
4646
constructor(
@@ -87,7 +87,11 @@ export class ConnectArray<RootState> extends ControlContainer {
8787
ngOnInit() {
8888
this.formDirective.form.addControl(this.key, this.array);
8989

90-
const ctrl = this.formDirective.form.find(this.path);
90+
const ctrl = this.formDirective.form.get(this.path);
91+
if (ctrl == null) {
92+
throw new Error(`No control found for ${this.path}`);
93+
}
94+
9195
setUpFormContainer(<any> ctrl, this.formArray);
9296

9397
ctrl.updateValueAndValidity({

0 commit comments

Comments
 (0)