Skip to content

Instantly share code, notes, and snippets.

@rlivsey
Created May 11, 2014 17:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rlivsey/d6a8e57ab60eb00d1546 to your computer and use it in GitHub Desktop.
Save rlivsey/d6a8e57ab60eb00d1546 to your computer and use it in GitHub Desktop.
Deserialized query params are not available in model hooks
// a controller which has a date property which defaults to today, serialized it to query params as a string
FoosController = Ember.Route.extend({
queryParams: ["date:dateStr"],
date: function(){
return moment()
}.property(),
dateStr: function(k, value) {
if (arguments.length == 2) {
this.set("date", moment(value, "YYYY-MM-DD"))
return value
} else {
return this.get("date").format("YYYY-MM-DD")
}
}.property("date")
})
// now we have the route, but we only have the pre-deserialized date here so I've got to duplicate
// the deserialization & defaults
FoosRoute = Ember.Route.extend({
// here be duplication, I don't have access to the controller here yet so have to repeat myself
model: function(params) {
var dateStr = params.date, date
if (dateStr) {
date = moment(dateStr, "YYYY-MM-DD")
} else {
date = moment()
}
return this.store.find("foos", {
start: date.startOf("week"),
end: date.endOf("week")
})
},
setupController: function(controller, model) {
// I now have the controller with the date set, but it's too late to be useful for finding
return this._super(controller, model)
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment