简体   繁体   English

带有Firebase后端的Ember.js:更改Firebase的自动生成的模型ID

[英]Ember.js with Firebase backend: change Firebase's auto-generated model IDs

I'm building a personal website with Ember.js and Firebase (EmberFire) as the backend. 我正在建立一个以Ember.js和Firebase(EmberFire)为后端的个人网站。 I've got Firebase connected and I can createRecords and such, and then view the records in the Firebase console. 我已连接Firebase,可以创建RecordRecords等,然后在Firebase控制台中查看记录。

I'm building my own post system for my website for personal posts and whatnot. 我正在为自己的网站构建自己的帖子系统,以发布个人帖子等。 I would like to have a dynamic route for my posts at /posts/:post_id , where :post_id is the dasherized title of the post. 我想在/posts/:post_id为我的帖子提供一条动态路线,其中:post_id是帖子的反斜线标题。 I've been following the Ember tutorial and already have all the attributes I need (like a dasherized title) in my model. 我一直在关注Ember教程,并且已经在模型中拥有了我需要的所有属性(如虚线标题)。 However, whenever I create a record and send the data to the Firebase database, Firebase automatically generates a post id like so: 但是,无论何时创建记录并将数据发送到Firebase数据库,Firebase都会自动生成一个帖子ID,如下所示:

posts
 -KP3SRMcfGCXW1hz-c1W
 -KP3a-6nr9C_gqPMTnFO
 -KP53U2pxOZw1gCewBXM
 -KPAde4QmamjrrNpVhhn
 -KPAeXaxty4p5uWbBvE8
 -KPAetgYyKFZMVCl9h9l
 -KPAh0JZt6GvLqhpEnIG
 -KPAh8vrKNaPLYZGbkvq

So if I have a post called "hello first post", I can't go to /posts/hello-first-post , but if that post has ID -KPAh8vrKNaPLYZGbkvq , I could go to /posts/-KPAh8vrKNaPLYZGbkvq and it would work. 因此,如果我有一个名为“ hello first post”的帖子,则不能转到/posts/hello-first-post ,但是如果该帖子的ID为-KPAh8vrKNaPLYZGbkvq ,则可以转到/posts/-KPAh8vrKNaPLYZGbkvq ,它将可以正常工作。 I've already generated the dynamic route which is why the latter option works. 我已经生成了动态路由,这就是后一个选项起作用的原因。

I am wondering how I can change this so that I can have the dynamic route for posts NOT be the auto-generated id by Firebase. 我想知道如何更改此设置,以便使帖子的动态路由不是Firebase自动生成的ID。

For example, I want this to work: 例如,我希望它能正常工作:

{{link-to post.title "post" post.dasherizedTitle}}

I already have a post route which routes to /posts/:post_id, so the link-to helper is generating the correct link. 我已经有一条发布路线,该路线可以路由到/ posts /:post_id,因此链接到帮助器正在生成正确的链接。 But there is nothing there because Firebase has it's auto-generated post ID. 但是那里什么也没有,因为Firebase具有它自动生成的帖子ID。

您可以在firebase上使用put而不是post,put将没有自动生成的数据,push将具有自动生成的id,因为它是一个数组

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

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