Last active
September 15, 2017 18:45
-
-
Save dbashford/dc620079b4de87b29d6dedc4b00cbac0 to your computer and use it in GitHub Desktop.
BEFORE-Ember-Redux
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as ACTION_TYPES from 'twiddle/actions/types'; | |
const goToPage = (pageNumber) => ({ | |
type: ACTION_TYPES.SET_PAGE, | |
payload: pageNumber | |
}); | |
const updatePageSize = (newPageSize) => ({ | |
type: ACTION_TYPES.SET_PAGE_SIZE, | |
payload: newPageSize | |
}); | |
export { | |
goToPage, | |
updatePageSize | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const SET_PAGE = 'TWIDDLE::SET_PAGE'; | |
export const SET_PAGE_SIZE = 'TWIDDLE::SET_PAGE_SIZE'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
import { connect } from 'ember-redux'; | |
const stateToComputed = ({ page: { pageNumber, pageSize, items } }) => ({ | |
pageNumber, | |
pageSize, | |
items | |
}); | |
const ItemsComponent = Ember.Component.extend({ | |
itemsToRender: Ember.computed('pageSize', 'pageNumber', 'items', function() { | |
const pageSize = this.get('pageSize'); | |
const beginIndex = (this.get('pageNumber') - 1) * pageSize + 1; | |
return this.get('items').slice(beginIndex - 1, beginIndex + pageSize); | |
}) | |
}); | |
export default connect(stateToComputed)(ItemsComponent); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
import { connect } from 'ember-redux'; | |
import { goToPage } from 'twiddle/actions'; | |
const stateToComputed = ({ page: { pageNumber, pageSize, items } }) => ({ | |
pageNumber, | |
pageSize, | |
items | |
}); | |
const dispatchToActions = { | |
goToPage | |
}; | |
const PagerComponent = Ember.Component.extend({ | |
numberItems: Ember.computed('items', function() { | |
return this.get('items').length; | |
}), | |
lastPageNumber: Ember.computed('pageSize', 'numberItems', function() { | |
return Math.ceil(this.get('numberItems') / this.get('pageSize')); | |
}), | |
isFirstPage: Ember.computed('pageNumber', function() { | |
return this.get('pageNumber') === 1; | |
}), | |
isLastPage: Ember.computed('pageNumber', 'lastPageNumber', function() { | |
return this.get('pageNumber') === this.get('lastPageNumber'); | |
}), | |
actions: { | |
firstPage() { | |
this.send('goToPage', 1); | |
}, | |
nextPage() { | |
this.send('goToPage', this.get('pageNumber') + 1); | |
}, | |
previousPage() { | |
this.send('goToPage', this.get('pageNumber') - 1); | |
}, | |
lastPage() { | |
this.send('goToPage', this.get('lastPageNumber')); | |
} | |
} | |
}); | |
export default connect(stateToComputed, dispatchToActions)(PagerComponent); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
import { connect } from 'ember-redux'; | |
import { updatePageSize } from 'twiddle/actions'; | |
const PAGE_SIZES = [10, 25, 50, 100]; | |
const stateToComputed = ({ page: { pageSize } }) => ({ | |
pageSize | |
}); | |
const dispatchToActions = { | |
updatePageSize | |
}; | |
const PageSizer = Ember.Component.extend({ | |
pageSizes: Ember.computed('pageSize', function() { | |
const pageSize = this.get('pageSize'); | |
return PAGE_SIZES.map((size) => { | |
return { | |
size, | |
selected: size === pageSize | |
} | |
}); | |
}), | |
actions: { | |
pageSizeSelected(pageSize) { | |
// proxy action to convert to int | |
this.send('updatePageSize', parseInt(pageSize, 10)); | |
} | |
} | |
}); | |
export default connect(stateToComputed, dispatchToActions)(PageSizer); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
import { connect } from 'ember-redux'; | |
const stateToComputed = ({ page: { pageNumber, pageSize, items } }) => ({ | |
pageNumber, | |
pageSize, | |
items | |
}); | |
const PagerStatusComponent = Ember.Component.extend({ | |
numberItems: Ember.computed('items', function() { | |
return this.get('items').length; | |
}), | |
beginPageIndex: Ember.computed('pageSize', 'pageNumber', function() { | |
return this.get('pageSize') * (this.get('pageNumber') - 1) + 1; | |
}), | |
endPageIndex: Ember.computed('beginPageIndex', 'pageSize', 'numberItems', function() { | |
const calculatedPageEnd = this.get('beginPageIndex') + this.get('pageSize') - 1; | |
const numberItems = this.get('numberItems'); | |
return Math.min(calculatedPageEnd, numberItems); | |
}) | |
}); | |
export default connect(stateToComputed)(PagerStatusComponent); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
export default Ember.Controller.extend({ | |
appName: 'Ember Twiddle' | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { combineReducers } from 'redux'; | |
import { handleActions } from 'redux-actions'; | |
import * as ACTION_TYPES from 'twiddle/actions/types'; | |
const initialState = { | |
pageNumber: 1, | |
pageSize: 10, | |
items: Array.from({length: 127}, () => Math.floor(Math.random() * 1000)) | |
}; | |
const page = handleActions({ | |
[ACTION_TYPES.SET_PAGE]: (state, { payload }) => { | |
return Object.assign({}, state, { pageNumber: payload}); | |
}, | |
[ACTION_TYPES.SET_PAGE_SIZE]: (state, { payload }) => { | |
return Object.assign({}, state, { pageSize: payload}); | |
} | |
}, initialState); | |
export default combineReducers({ | |
page | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"version": "0.12.1", | |
"EmberENV": { | |
"FEATURES": {} | |
}, | |
"options": { | |
"use_pods": false, | |
"enable-testing": false | |
}, | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js", | |
"ember": "2.12.0", | |
"ember-template-compiler": "2.12.0", | |
"ember-testing": "2.12.0" | |
}, | |
"addons": { | |
"ember-redux": "3.0.0", | |
"ember-redux-actions": "0.3.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment