Last active
March 10, 2017 16:02
-
-
Save alexspeller/8618adbf68159eebdf5ee414afe04f4c to your computer and use it in GitHub Desktop.
New Twiddle
This file contains 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'; | |
// this is a bit of a weird construction, but it prevents duplicating the default | |
// values in the controller and the route. It'd probably be possible to make a helper | |
// here to make it a bit less weird. Or if default values are always null, this isn't necessary | |
const ApplicationController = Ember.Controller.extend({ | |
queryParams: ['statusId', 'caseId'] | |
}); | |
export const defaultQueryParamValues = { | |
statusId: null, | |
caseId: null | |
} | |
export default ApplicationController.extend(defaultQueryParamValues); |
This file contains 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 { defaultQueryParamValues } from "../controllers/application"; | |
// fake storage for the params, really this would be in the session / via ajax | |
let fakeStoredParams = { | |
caseId: 'Stored case id' | |
}; | |
export default Ember.Route.extend({ | |
queryParams: { | |
statusId: { refreshModel: true }, | |
caseId: { refreshModel: true } | |
}, | |
queryHash(params) { | |
// would really be this.get('session.user.settingsFilters.case'); | |
let storedParams = fakeStoredParams; | |
let storedParamsDirty = false; | |
let hash = Object.keys(defaultQueryParamValues).reduce( (memo, key) => { | |
if (params[key] != defaultQueryParamValues[key]) { | |
console.log("The key is in the query params, this takes precedence", key); | |
memo[key] = params[key]; | |
// set dirty flag if what's in the url doesn't match the stored value | |
if (storedParams[key] !== params[key]) { storedParamsDirty = true; } | |
} else if (storedParams[key]) { | |
console.log("The key is stored in the session, so using that", key); | |
memo[key] = storedParams[key]; | |
} else { | |
console.log("The key is not in the params or the session, use the default", key); | |
memo[key] = defaultQueryParamValues[key]; | |
} | |
return memo; | |
}, {}); | |
if (storedParamsDirty) { | |
this.updateStoredParams(hash); | |
} | |
return hash; | |
}, | |
updateStoredParams(newStoredParams) { | |
// trigger ajax request here or store in session or however it's stored | |
fakeStoredParams = newStoredParams; | |
// just for testing | |
this.controller.set('storedParams', fakeStoredParams); | |
}, | |
model(params) { | |
// really would be this.store.query('case', this.queryHash(params)); | |
return this.queryHash(params); | |
}, | |
// just for testing | |
setupController(controller, context) { | |
controller.set('model', context); | |
controller.set('storedParams', fakeStoredParams); | |
} | |
}); |
This file contains 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
body { | |
margin: 12px 16px; | |
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; | |
font-size: 12pt; | |
} | |
dt { | |
font-weight: bold; | |
float: left; | |
width: 100px; | |
clear: left; | |
} | |
dt:after { | |
content: ":"; | |
} | |
h2 { | |
clear: both; | |
margin-top: 100px; | |
} |
This file contains 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.11.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.11.0", | |
"ember-data": "2.11.0", | |
"ember-template-compiler": "2.11.0", | |
"ember-testing": "2.11.0" | |
}, | |
"addons": {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment