Ember、Ember 数据和 MongoDB 的 _id [英] Ember, Ember Data and MongoDB's _id

查看:14
本文介绍了Ember、Ember 数据和 MongoDB 的 _id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了这个问题的讨论,但我仍然在将 Mongo 的 _id 处理为 id 时遇到问题.我使用猫鼬作为我的 ORM,虽然它有虚拟,但我似乎无法让它正常工作.以下是我的猫鼬模型.

I've seen this question talked about but I'm still having issues with manhandling Mongo's _id into id. I'm using mongoose as my ORM and while it has virtuals I can't seem to get it to work properly. Below is what I have in my mongoose model.

尝试从后端修复

mongoose = require 'mongoose'
Schema = mongoose.Schema
ObjectId = Schema.ObjectId

ApartmentSchema = new Schema
  body: String
  date: { type: Date, default: Date.now }
  deleted: {type: Boolean, default: false}
  saved: {type: Boolean, default: false}
  visited: {type: Boolean, default: false}
  email: String
  phoneNumber: String
  href: String

ApartmentSchema.virtual('id').get -> return @_id

module.exports = mongoose.model 'Apartment', ApartmentSchema

当我在 express 创建这个模型的新实例时,我可以像 apt.id 一样查找并取回 id,但是当我将响应发送给客户端时,我只需要_id 而不是 id.

When I create a new instance of this model in express I can do a look up like apt.id and get the id back but when I send the response down to the client, I just have _id and not id.

我尝试的第二个解决方案是为 id 创建一个计算属性,但无论出于何种原因,ember 都不喜欢这样.这里有2个问题.Ember 不尊重名为 id 的计算属性,或者至少不再尊重.这就是我的 ember-data 模型的样子.

The second solution I tried was to create a computed property for id but for whatever reason ember does not like this. There are 2 problems here. Ember does not respect a computed property called id or at least not anymore. This is what my ember-data model looks like.

尝试从前端修复

App.Apartment = DS.Model.extend({
  body: DS.attr('string'),
  date: DS.attr('date'),
  deleted: DS.attr('boolean'),
  saved: DS.attr('boolean'),
  visited: DS.attr('boolean'),
  email: DS.attr('string'),
  phone: DS.attr('string'),
  _id: DS.attr('string'),
  id : function () {
    return this.get('_id')
  }.property('_id')
});

在我下面的模板中,没有为 id 呈现任何内容.

In my template below, nothing renders for the id.

{{#each apartment in controller}}
    <li>{{apartment.body}} | {{apartment.date}} | {{apartment.href}} {{apartment.id}}</a>  {{#linkTo 'apartment' apartment }} View {{/linkTo}} </li>
{{/each}}

linkTo 助手可以工作,但 url 的 id 应该为 null.这会导致后退按钮被破坏并多次加载数据.下面是我的路由器的一些上下文.

The linkTo helpers work but the url has null where the id should be. This results in breaking of the backbutton and loading the data multiple times. Below is my router for some context.

App.Router.map(function(){
  this.resource('apartments', function (){
    this.resource('apartment', { path: ':apartment_id' } );
  });
});

将我计算的 id 属性的名称更改为类似 foo 的名称,然后将我的路由器更改为 path: ':apartment_foo' 导致在 url 中包含对象引用的 url,例如:#/apartments/.

Changing the name of my computed id property to something like foo and then changing my router to path: ':apartment_foo' results in urls that have the object reference in the url eg: #/apartments/<App.Apartment:ember357:null>.

像这样的东西让我对 ember 感到厌烦.任何帮助,将不胜感激.

It's stuff like this that kind of erks me about ember. Any help would be appreciated.

推荐答案

从 Ember 1.0.0-rc 1 和 Ember Data 修订版 11 开始,这似乎是解决此问题的最佳方法.

As of Ember 1.0.0-rc 1 and Ember Data revision 11, this seems to be the best way to resolve this.

App.Adapter = DS.RESTAdapter.extend({
  serializer: DS.RESTSerializer.extend({
    primaryKey: function(type){
      return '_id';
    }
  })
});

App.Store = DS.Store.extend({
  revision: 11,
  adapter: 'App.Adapter'
});

这篇关于Ember、Ember 数据和 MongoDB 的 _id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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