and
in
at
Bob Bijvoet
Technical Lead Digital 2
Chapter Lead at BB (@Experience from jan 2017)
We will touch on Polymer 2 specifics during the talk...
Are concerned with how things work.
May contain both presentational and container components.
Provide the data and configuration to presentational or other container components.
Are often stateful, as they tend to serve as data sources.
<inbox-list> <ajax-util result="{{result}}"/>
<dom-repeat items="[[result]]"> </inbox-list>
<inbox-container> <ajax-util result="{{result}}"/> <inbox-list items="[[result]]"/> </inbox-container>
<inbox-list items="[[result]]"> <dom-repeat items="[[result]]"/> </inbox-list>
Container component
Presentational component
attrs
events
<dom-repeat items={{items}}> <inbox-item on-click="deleteMessage([[index]])">
</dom-repeat>
deleteMessage() {items.splice(index, 1);}
deleteMessage() { this.dispatchEvent( new CustomEvent('delete-message', { detail: {id: this.item.id}, bubbles: true, composed: true })); }
And emit events
<dom-repeat items=[[items]]>
Don't two-way bind
<inbox-container on-delete-message="deleteMessage"/> deleteMessage() { //logic for deleting the message }
Container component
A predictable state container for JavaScript apps.
The state of your whole application is stored in an object tree within a single store.
The only way to mutate the state is to emit an action, an object describing what happened.
Changes are made with pure functions
To specify how the state tree is transformed by actions, you write pure reducers.
Pure function
The single source of truth/place where all your application state lives
The UI subscribes to changes in the state
UI
//Inbox state
{
messages:[...],
selectedMessage:null,
totalUnread:0
}
Tells store that something happened and that the store should update itself in response.
Triggered by asynchronous interactions.
UI
//Fetch success
{
type:'FETCH_MESSAGES_SUCCESS',
items:response
}
//Open
{
type:'OPEN_MESSAGE',
item:{
id:1
}
}
//Remove
{
type:'DELETE_MESSAGE',
item:{
id:1
}
}
Responsible for mutating the store state when actions are dispatched.
Code that is run based on the action type.
UI
reducer(state, action) {
switch (action.type) {
case 'FETCH_MESSAGES_SUCCESS':
//Set messages to XHR result
return state;
break;
case 'OPEN_MESSAGE':
//Set selected message based on action.id
//Set message to unread based on action.id
return state;
break;
case 'REMOVE_MESSAGE':
//Remove message based on action.id
return state;
break;
}
return state;
}
//Always return a new object
return Object.assign({}, state, {/* state modifications */})
//It's a new assignment, thus no comparison needed
</>
If there's time left, I'll add the unread action!
Questions?