Last active
October 11, 2016 15:54
-
-
Save sglanzer-deprecated/7a59143c0639b8a54404d6f62b1f69b3 to your computer and use it in GitHub Desktop.
Object browser
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({ | |
queryParams: ['filter', 'sort', 'page'], | |
filter: null, | |
sort: null, | |
page: null, | |
actions: { | |
onFilter(model) { | |
this.send('resetPage') | |
this.set('filter', model) | |
}, | |
onSort(desc) { | |
this.send('resetPage') | |
this.set('sort', desc ? true : null) | |
}, | |
onPage(next) { | |
this.send(next ? 'nextPage' : 'previousPage') | |
} | |
} | |
}); |
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 function divisibleByFive(params) { | |
return params[0] % 5 === 0; | |
} | |
export default Ember.Helper.helper(divisibleByFive); |
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 config from './config/environment'; | |
const Router = Ember.Router.extend({ | |
location: 'none', | |
rootURL: config.rootURL | |
}); | |
Router.map(function() { | |
this.route('browser') | |
this.route('reset') | |
}); | |
export default Router; |
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'; | |
const data = [ | |
'ab', 'bc', 'cd', 'de', 'ef', | |
'fg', 'gh', 'hi', 'ij', 'jk', | |
'kl', 'lm', 'mn', 'no', 'op', | |
'pq', 'qr', 'rs', 'st', 'tu', | |
'uv', 'vw', 'wx', 'xy', 'yz' | |
] | |
const pageSize = 5 | |
export default Ember.Route.extend({ | |
queryParams: { | |
filter: { | |
replace: true, | |
refreshModel: true | |
}, | |
sort: { | |
replace: true, | |
refreshModel: true | |
}, | |
page: { | |
replace: true, | |
refreshModel: true | |
} | |
}, | |
firstPage: null, | |
lastPage: null, | |
query(params) { | |
return new Promise((resolve, reject) => { | |
const result = Ember.A(data) | |
.filter((item) => { | |
if (Ember.isEmpty(params.filter)) { | |
return true | |
} | |
return item.includes(params.filter) | |
}) | |
.sort(function (a, b) { | |
if (a > b) { return params.sort ? -1 : 1 } | |
if (a < b) { return params.sort ? 1 : -1 } | |
return 0 | |
}) | |
.slice(params.page * pageSize, params.page * pageSize + pageSize) | |
resolve(result) | |
}) | |
}, | |
model(params) { | |
const filter = params && params.filter | |
const sort = params && params.sort | |
const page = (params && params.page) || 0 | |
let requests = [] | |
requests.push(this.query({ filter, sort, page })) | |
// On the initial load request the pages before/after as well | |
if (Ember.isEmpty(this.get('firstPage')) && Ember.isEmpty(this.get('lastPage'))) { | |
if (params.page > 0) { | |
requests.unshift(this.query({ filter, sort, page: Number(page) - 1 })) | |
} | |
requests.push(this.query({ filter, sort, page: Number(page) + 1 })) | |
} | |
return Promise.all(requests).then((values) => { | |
const result = [].concat(...values) | |
if (Ember.isEmpty(this.get('firstPage'))) { | |
this.setProperties({ | |
firstPage: page != 0 ? Number(page) - 1 : 0, | |
lastPage: Number(page) + 1 | |
}) | |
return result | |
} else if (page > this.get('lastPage')) { | |
this.setProperties({ | |
lastPage: page | |
}) | |
return this.controllerFor('browser').get('model').concat(result) // append | |
} else if (page < this.get('firstPage')) { | |
this.setProperties({ | |
firstPage: page | |
}) | |
return result.concat(this.controllerFor('browser').get('model')) // prepend | |
} | |
}) | |
}, | |
actions: { | |
nextPage() { | |
this.controllerFor('browser').set('page', Number(this.get('lastPage')) + 1) | |
}, | |
previousPage() { | |
const firstPage = Number(this.get('firstPage')) | |
if (firstPage > 0) { | |
this.controllerFor('browser').set('page', firstPage - 1) | |
} | |
}, | |
resetPage() { | |
this.setProperties({ | |
firstPage: null, | |
lastPage: null | |
}) | |
} | |
}, | |
resetController: function(controller, isExiting, transition) { | |
if (isExiting) { | |
// TODO Only for the demo? Might want to keep this state | |
controller.set('model', null) | |
controller.set('filter', null) | |
controller.set('sort', null) | |
controller.set('page', null) | |
this.set('firstPage', null) | |
this.set('lastPage', null) | |
} | |
} | |
}); |
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.Route.extend({ | |
}); |
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.10.5", | |
"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.8.0", | |
"ember-data": "2.8.0", | |
"ember-template-compiler": "2.8.0", | |
"ember-testing": "2.8.0" | |
}, | |
"addons": {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment