带有 Ember.js 的 Slug url [英] Slug urls with Ember.js

查看:20
本文介绍了带有 Ember.js 的 Slug url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Ember.js 中获得 post/my-title(带有自定义 slug)而不是 posts/2(带有 id)?

How would you get post/my-title (with a custom slug) instead of posts/2 (with id) in Ember.js?

我的方法是在我的模型中添加和使用一个 slug,但它不适用于直接访问.使用 {{link-to}} 帮助器可以工作,也可以使用 slug.我认为错误在于 PageRoute 的模型.尝试 return this.store.find('page', {'slug': params.page_slug}) 没有任何运气.

My approach was to add and use a slug in my model but it doesn't work on direct access. Using a {{link-to}} helper it works, also the slug. I assume the error lies in the PageRoute's model. Tried return this.store.find('page', {'slug': params.page_slug}) without any luck.

示例代码:http://emberjs.jsbin.com/AVAgUZAb/5/edit

推荐答案

您的 Ember Data 版本不匹配.在 PageRoute 中,您正在使用模型定义来尝试查找不再正确的记录.请参阅 https://github.com/emberjs/data/blob/master/TRANSITION.md

You're mismatching versions of Ember Data. In the PageRoute you are using the model definition to try and find the record which is no longer correct. See https://github.com/emberjs/data/blob/master/TRANSITION.md

此外,您真的应该用 slug 替换 id,因为您实际上已经使独特项目的想法变得毫无价值(又名 slug 不必是唯一的,但是 id 确实如此,那么当您有多个项目具有相同的 slug 时会发生什么?)如果您的 slug 将始终是唯一的,您不妨将它们用作 id 那里没有限制 id 必须是数字.

Additionally you really should just replace id with the slug since you've essentially rendered the idea of unique items worthless (aka slug doesn't have to be unique, but id does, so what happens when you have multiple items with the same slug?) If your slugs will always be unique, you might as well use them as the id there is no constraint that id must be numeric.

http://emberjs.jsbin.com/AVAgUZAb/9/edit

这篇关于带有 Ember.js 的 Slug url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆