0
votes

Im pretty new at ember and I want to make Route work with a string instead of id for a prettier url. This Router code here is working when I enter the path manually example.com/#/Commune. But when using {{#linkTo "commune" name}}{{name}}{{/linkTo}} the url is changing correctly but nothing is showing (it does if i refresh the browser). any idea?

JS:

App.Router.map(function() {
    this.route('commune', {path: "/:commune_name"});
});

App.CommuneRoute = Em.Route.extend({
    model: function(params) {
        return App.CommunesController.findProperty('name', params.commune_name);
    }
});

If I do this: it's the other way around. (links working but enter/refresh aint)

App.CommuneRoute = Em.Route.extend({
    model: function(params) {
        return App.Commune.find(params.commune_name);
    }
});
1
have you already checked whether you end up in the right controller after clicking on the link and if params.commune_name is set correctly? also, is CommuneRoute really your only route? (just wondering because of the CommunesController.)Finn MacCool
Transitioned into 'commune' both on refresh/enter and then clicking the linksThomas Carlsen
if I do this: App.Router.map(function() { this.route('commune', {path: "/:commune_id"}); }); App.CommuneRoute = Em.Route.extend({ model: function(params) { return App.Commune.find(params.commune_id); } }); its the other way around, now the liks is working but on enter/refresh it crashesThomas Carlsen
hmm, and if you use return App.Commune.find({name: params.commune_name}); (or maybe return App.Commune.find({'name': params.commune_name});) in your CommuneRoute with the old router?Finn MacCool
They both only work on links, and not on enter/refresh :/Thomas Carlsen

1 Answers

0
votes

needed to add serialize to my route:

App.CommuneRoute = Em.Route.extend({
    serialize: function(model, params) {
        return {
            name: model._data.attributes.name
        };
    },
    model: function(params) {
        return App.CommunesController.findProperty('name', params.commune_name);
    }
});