Created
May 11, 2014 17:44
-
-
Save rlivsey/d6a8e57ab60eb00d1546 to your computer and use it in GitHub Desktop.
Deserialized query params are not available in model hooks
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
// 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