简体   繁体   English

使用余烬序列化程序的SEO路由

[英]Seo routes with ember serializer

Im following the example emberjs guides 我遵循示例emberjs指南

...
this.route('author', { path: '/author/:post_userName' });
...

App.PostsAuthorRoute = Ember.Route.extend({
  model: function(params) {
    return App.Post.find({userName : params.userName});
  },

  serialize:function(model) {
    return { post_userName: model.get('userName')};
  }
});

Then here is the link to 然后是这里的链接

Author {{#linkTo 'posts.author' post }} {{post.userName }} {{/linkTo}}

The fun is when I click on the link I get a routing error 有趣的是,当我单击链接时,出现路由错误

Error while loading route: TypeError {}
Uncaught TypeError: Object [Object Object] has no method 'slice'

But when I reload the page, the full data appears. 但是,当我重新加载页面时,将显示完整数据。

How can I solve the routing error, really I don't understand why I get the error and is solved on reload the page 我该如何解决路由错误,实际上我不明白为什么会收到错误并在重新加载页面时得到解决

Here is the jsbin of a similar case. 这是类似情况的jsbin。

http://jsbin.com/aZIXaYo/31/edit http://jsbin.com/aZIXaYo/31/edit

The problem is with the object you're passing to your link-to . 问题出在传递给link-to对象的对象link-to You're doing this : 您正在这样做:

Author {{#linkTo 'posts.author' post }} {{post.userName }} {{/linkTo}}

which passes, a Post to the author route. 通过后, Postauthor路线。 Passing a model to link-to causes the model hook on the route to be skipped and the passed model is used instead. 将模型传递给link-to导致跳过路线上的model钩子,而改用传递的模型。 When you hit reload, the model hook is executed and the model for PostsAuthor route is set to be a collection of Post objects, and then things work as expected. 当您单击reload时,将执行model挂钩,并将PostsAuthor路由的模型设置为Post对象的集合,然后事情将按预期进行。

To do things The Ember Way (TM), you'd want to have an Author model that was related to your Post model. 做事在Ember Way(TM)中,您希望拥有一个与Post模型相关的Author模型。 Then you'd have an AuthorRoute and AuthorController that needs a PostsController . 然后,你就会有一个AuthorRouteAuthorControllerneeds一个PostsController In your link you'd pass post.author , and then use the setupController hook to prime the collection for the PostsController . 在链接中,您将传递post.author ,然后使用setupController钩子为PostsController的集合进行PostsController Something like this: 像这样:

App.Post = DS.Model.extend({
  author : DS.belongsTo('post'),
  title  : DS.attr('string')
});

App.Author = DS.Model.extend({
  name : DS.attr('string'),
  userName : DS.attr('string')
});

App.AuthorRoute = DS.Route.extend({
  model : function(){ // not called when the author is passed in to #link-to
    return this.store.find('author',{userName : params.post_userName})
  },
  setupController : function(controller,model){
    this._super(controller,model);
    this.controllerFor('posts').set('content', this.store.find('post',{userName : model.userName}))
  }
});

App.AuthorController = Ember.ObjectController.extend({
  needs : ['posts']
});

App.PostsController = Ember.ArrayController.extend({
 sortProperties : ['name']
});

Then the template : 然后是模板:

Author {{#linkTo 'posts.author' post.author }} {{post.author.name }} {{/linkTo}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM