Skip to content

Commit 5b8382d

Browse files
committed
find controller as key
1 parent 7703c47 commit 5b8382d

File tree

6 files changed

+40
-20
lines changed

6 files changed

+40
-20
lines changed

examples/counter/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "ng-redux counter example",
55
"main": "index.js",
66
"scripts": {
7-
"start": "webpack && webpack-dev-server --content-base build/ --hot"
7+
"start": "webpack && webpack-dev-server --content-base dist/ --hot"
88
},
99
"repository": {
1010
"type": "git",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ng-redux",
3-
"version": "1.0.0-rc.2",
3+
"version": "1.0.0-rc.4",
44
"description": "Redux bindings for Angular.js",
55
"main": "./lib/index.js",
66
"scripts": {

src/components/connector.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import shallowEqual from '../utils/shallowEqual';
22
import wrapActionCreators from '../utils/wrapActionCreators';
3+
import findControllerAsKey from '../utils/findControllerAsKey';
34
import invariant from 'invariant';
45
import _ from 'lodash';
56

67
export default function Connector(store) {
7-
return (scope, mapStateToScope, mapDispatchToScope = {}, propertyKey) => {
8+
return (scope, mapStateToScope, mapDispatchToScope = {}) => {
89

910
invariant(
1011
scope && _.isFunction(scope.$on) && _.isFunction(scope.$destroy),
@@ -19,11 +20,10 @@ export default function Connector(store) {
1920
'mapDispatchToScope must be a plain Object or a Function. Instead received $s.', mapDispatchToScope
2021
);
2122

23+
const propertyKey = findControllerAsKey(scope);
24+
2225
let slice = getStateSlice(store.getState(), mapStateToScope);
2326
let target = propertyKey ? scope[propertyKey] : scope;
24-
if(!target) {
25-
target = scope[propertyKey] = {};
26-
}
2727

2828
const finalMapDispatchToScope = _.isPlainObject(mapDispatchToScope) ?
2929
wrapActionCreators(mapDispatchToScope) :
@@ -39,7 +39,6 @@ export default function Connector(store) {
3939
_.assign(target, slice);
4040
}
4141
});
42-
4342
}
4443
}
4544

src/utils/findControllerAsKey.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import _ from 'lodash';
2+
3+
export default function findControllerAsKey(scope) {
4+
let propertyKey;
5+
_.forOwn(scope, (v, k) => {
6+
if (scope[k] && scope[k].constructor && scope[k].constructor.$inject) {
7+
propertyKey = k;
8+
}
9+
});
10+
return propertyKey;
11+
}

test/components/connector.spec.js

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ describe('Connector', () => {
1414
baz: action.payload
1515
}));
1616
scopeStub = {
17-
$on: () => { },
18-
$destroy: () => { }
17+
$on: () => {},
18+
$destroy: () => {}
1919
};
2020
connect = Connector(store);
2121
});
@@ -43,17 +43,6 @@ describe('Connector', () => {
4343
expect(scopeStub.vm).toEqual({ test: 1 });
4444
});
4545

46-
it('Should extend scope[propertyKey] if propertyKey is passed', () => {
47-
connect(
48-
scopeStub,
49-
() => ({ test: 1 }),
50-
() => { },
51-
'vm'
52-
);
53-
54-
expect(scopeStub.vm).toEqual({ test: 1 });
55-
});
56-
5746
it('Should update the scope passed to connect when the store updates', () => {
5847
connect(scopeStub, state => state);
5948
store.dispatch({ type: 'ACTION', payload: 0 });

test/utils/findControllerAs.spec.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import expect from 'expect';
2+
import findControllerAsKey from '../../src/utils/findControllerAsKey';
3+
4+
describe('Utils', () => {
5+
describe('findControllerAsKey', () => {
6+
it('Should return the property key of the controller', () => {
7+
8+
let controllerStub = () => {};
9+
controllerStub.constructor.$inject = ['$scope', '$ngRedux'];
10+
11+
let propertyKey = findControllerAsKey({
12+
$apply: () => {},
13+
$on: () => {},
14+
$$id: 2,
15+
vm: controllerStub
16+
});
17+
18+
expect(propertyKey).toBe('vm');
19+
});
20+
});
21+
});

0 commit comments

Comments
 (0)