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

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

问题描述

我已经看到这个问题,但是我仍然在处理Mongo的 _id id 。我使用mongoose作为我的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不喜欢这个。这里有两个问题。 Ember不尊重称为 id 的计算属性,或至少不再。这是我的ember数据模型看起来像。

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')
});

在我下面的模板中,没有任何代码的呈现。

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 helpers工作,但是url具有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 ,然后将路由器更改为路径:':apartment_foo'导致具有对象引用的URL在url中,例如:#/ apartments /< App.Apartment:ember357:null>

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>.

像这样的一种让我厌烦的东西。任何帮助将不胜感激。

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 revision 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 Data和MongoDB的_id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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