Skip to content

Commit 88503e2

Browse files
authored
Merge pull request #6 from madmax983/Reselect
Reselect
2 parents c2f401d + e2ee8d3 commit 88503e2

File tree

15 files changed

+353
-126
lines changed

15 files changed

+353
-126
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ middleware: You can include redux middleware like redux-thunk here. Lightning-Re
2323

2424
**registerReducer(name, reducer):** Adds the specified reducer function to the redux store with the specificed name as it's slice of state.
2525

26-
**connect(mapStateToAttributes, target):** mapStateToAttributes is a key pair object where the key is the attribute on the component you want set, and the value is either the value in the redux state graph, or a callback function. This function receives the component and state to compute the value. You can pass the component you want connected, but if it isn't populated, it will get populated by event.getSource(). It also sets a dispatch expando for convenience in the component's controller methods.
26+
**connect(mapStateToAttributes, target):** mapStateToAttributes either a key pair object where the key is the attribute on the component you want set, and the value is either the value in the redux state graph, or a callback function. mapStateToAttributes can also be a function that returns this key pair value. This function receives the state and component to compute the value. You can pass the component you want connected, but if it isn't populated, it will get populated by event.getSource(). It also sets a dispatch expando for convenience in the component's controller methods.
2727

2828
**getState():** Technically not a component method, this is a convenience expando that allows access to the Redux state.
2929

3030
## Installation
31-
Lightning-Redux Unmanaged Packaged (Redux Component, redux and redux-thunk static resource): https://login.salesforce.com/packaging/installPackage.apexp?p0=04t50000000M7PT
31+
Lightning-Redux Unmanaged Packaged (Redux Component, redux, redux-thunk, and reselect static resource): https://login.salesforce.com/packaging/installPackage.apexp?p0=04t50000000M7PY
3232

33-
Lightning-Redux-Examples (Counter and Todo Examples unmanaged package): https://login.salesforce.com/packaging/installPackage.apexp?p0=04t50000000M7Pn
33+
Lightning-Redux-Examples (Counter and Todo Examples unmanaged package): https://login.salesforce.com/packaging/installPackage.apexp?p0=04t50000000M7Px
3434

3535
Otherwise, you can use the SFDX CLI to convert the source and do a metadata deploy.
3636

force-app/examples/default/aura/NewTodoApp/NewTodoAppController.js

Lines changed: 0 additions & 11 deletions
This file was deleted.

force-app/examples/default/aura/NewTodoApp/NewTodoAppHelper.js

Lines changed: 0 additions & 85 deletions
This file was deleted.

force-app/examples/default/aura/NewTodoItem/NewTodoItem.cmp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<aura:component description="NewTodoItem" controller="TodoController">
2-
<ltng:require scripts="{!$Resource.redux}"
2+
<ltng:require scripts="{!join(',',
3+
$Resource.redux,
4+
$Resource.reselect)}"
35
afterScriptsLoaded="{!c.doInit}"/>
46
<aura:attribute required="false" name="todo" description="Todo" access="public" type="Todo__c" />
57
<aura:attribute name="mode" type="String" default="view" />

force-app/examples/default/aura/NewTodoItem/NewTodoItemController.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,30 @@
22
doInit: function(component, event, helper) {
33
component.set('v.completed', component.get("v.todo.Completed__c"));
44
var store = component.find("store");
5-
var mapStateToAttributes = {
6-
"v.completed": helper.getTodoStatus
5+
6+
var getTodoStatus = Reselect.createSelector([
7+
helper.todoSelector,
8+
helper.componentSelector
9+
],
10+
helper.getTodoStatus
11+
);
12+
13+
var getTodoStatusFactory = function() {
14+
return Reselect.createSelector([
15+
helper.todoSelector,
16+
helper.componentSelector
17+
],
18+
helper.getTodoStatus
19+
);
720
}
21+
22+
var mapStateToAttributes = function(state, component) {
23+
var getTodoStatus = getTodoStatusFactory();
24+
return {
25+
"v.completed": getTodoStatus
26+
}
27+
}
28+
829
store.connect(mapStateToAttributes);
930
},
1031
edit: function(component) {

force-app/examples/default/aura/NewTodoItem/NewTodoItemHelper.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,24 @@
8787
}
8888
},
8989

90-
getTodoStatus: function(component, state) {
90+
todoSelector: function(state) {
91+
return state.todos;
92+
},
93+
94+
componentSelector: function(state, component) {
95+
if(component.isInstanceOf("c:NewTodoItem")) {
96+
return component;
97+
}
98+
},
99+
100+
getTodoStatus: function(todos, component) {
91101
var status = component.get("v.completed");
92102
var currentTodo = component.get("v.todo");
93-
state.todos.map(function(todo) {
94-
if(currentTodo && currentTodo.Id && currentTodo.Id === todo.Id) {
95-
status = todo.Completed__c;
96-
}
103+
todos.map(function(todo) {
104+
if(currentTodo.Id === todo.Id) {
105+
status = todo.Completed__c;
106+
}
97107
});
98-
99108
return status;
100109
}
101110
})

force-app/examples/default/aura/NewTodoList/NewTodoList.cmp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<aura:component description="NewTodoList" controller="TodoController">
22
<ltng:require scripts="{!join(',',
33
$Resource.redux,
4-
$Resource.reduxThunk)}"
4+
$Resource.reduxThunk,
5+
$Resource.reselect)}"
56
afterScriptsLoaded="{!c.doInit}"/>
67
<aura:attribute required="false" name="todoList" description="Todos" access="public" type="Todo__c[]" />
78

force-app/examples/default/aura/NewTodoList/NewTodoListController.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,15 @@
22
doInit: function(component, event, helper) {
33
var store = component.find("store");
44
store.createStore("todos", helper.todoReducer, {}, ReduxThunk.default);
5+
6+
var getVisibleTodos = Reselect.createSelector([
7+
helper.todoSelector,
8+
helper.visibilitySelector],
9+
helper.getVisibileTodos
10+
);
11+
512
var mapStateToAttributes = {
6-
"v.todoList": helper.getVisibileTodos
13+
"v.todoList": getVisibleTodos
714
}
815
store.connect(mapStateToAttributes);
916
store.dispatch(helper.receiveTodos(component));

force-app/examples/default/aura/NewTodoList/NewTodoListHelper.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,24 @@
6868
}
6969
},
7070

71-
getVisibileTodos: function(component, state) {
72-
switch(state.visibilityFilter){
71+
todoSelector: function(state, component) {
72+
return state.todos;
73+
},
74+
75+
visibilitySelector: function(state, component) {
76+
return state.visibilityFilter;
77+
},
78+
79+
getVisibileTodos: function(todos, visibilityFilter) {
80+
switch(visibilityFilter){
7381
case "SHOW_ALL":
74-
return state.todos;
82+
return todos;
7583
case "SHOW_ACTIVE":
76-
return state.todos.filter(function(todo){
84+
return todos.filter(function(todo){
7785
return !todo.Completed__c
7886
});
7987
case "SHOW_COMPLETED":
80-
return state.todos.filter(function(todo){
88+
return todos.filter(function(todo){
8189
return todo.Completed__c
8290
});
8391
}

force-app/main/default/aura/Redux/ReduxController.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -123,17 +123,27 @@
123123
var mapStateToAttributes = params ? params.mapStateToAttributes : null;
124124
var target = params && params.target ? params.target : event.getSource();
125125

126+
if(typeof mapStateToAttributes === "function") {
127+
mapStateToAttributes = mapStateToAttributes();
128+
}
129+
126130
if(window.reduxStore) {
127131
function handleChanges(){
128-
129-
var state = window.reduxStore.getState();
130-
131-
for(var key in mapStateToAttributes) {
132-
if(mapStateToAttributes.hasOwnProperty(key)) {
133-
if(typeof mapStateToAttributes[key] === "function") {
134-
target.set(key, mapStateToAttributes[key](target, state))
135-
} else {
136-
target.set(key, state[mapStateToAttributes[key]]);
132+
if(target.isValid()) {
133+
var state = window.reduxStore.getState();
134+
135+
for(var key in mapStateToAttributes) {
136+
if(mapStateToAttributes.hasOwnProperty(key)) {
137+
if(typeof mapStateToAttributes[key] === "function") {
138+
try {
139+
var returnedFunction = mapStateToAttributes[key]();
140+
target.set(key, returnedFunction(state, target));
141+
} catch(e) {
142+
target.set(key, mapStateToAttributes[key](state, target))
143+
}
144+
} else {
145+
target.set(key, state[mapStateToAttributes[key]]);
146+
}
137147
}
138148
}
139149
}

0 commit comments

Comments
 (0)