始终嵌入的Ember数据多级层次结构

时间:2013-02-15 13:41:24

标签: mongodb ember.js ember-data

我能够将嵌入式总是用于一个级别,但我无法将其用于两级深度模型。需要紧急帮助

App.Post = DS.Model.extend(
  title: DS.attr("string")
  comment: DS.belongsTo("App.Comment")
)

App.Comment = DS.Model.extend(
  text: DS.attr("string")
  ferment: DS.belongsTo("App.Ferment") 
)

App.Ferment = DS.Model.extend(
  fermenter: DS.attr("string")
)

App.Adapter.map App.Post,
  'comment':
    embedded: "always"


App.Adapter.map App.Comment,    
  ferment :
    embedded: "always"

# -----------------------------
App.store = App.Store.create(
  adapter: App.Adapter.create()
)
# -----------------------------

App.store.adapter.load App.store, App.Post,
  id: 12
  comment: {text: "blabla", ferment:{fermenter:'abcd'}}

console.log App.Post.find(12).get("comment.text")
console.log App.Post.find(12).get("comment.ferment.fermenter")

我将comment.text的日志记录为blabla 但是不能让第二部分工作。 我正在使用Ember数据存储的修订版11。 任何有类似问题/解决方案的人。

有趣的是我们尝试过hasMany所以Post -hasMany->评论,评论-hasOne->发酵。这很好用,这是代码。

App.Post = DS.Model.extend(
  title: DS.attr("string")
  comments: DS.hasMany("App.Comment")
)

App.Comment = DS.Model.extend(
  text: DS.attr("string")
  ferment: DS.belongsTo("App.Ferment")
)

App.Ferment = DS.Model.extend(
  fermi: DS.attr("string")
)

App.Adapter.map App.Post,
  comments:
    embedded: "always"

App.Adapter.map App.Comment,
  ferment:
    embedded: "always"

App.store = App.Store.create(
  adapter: App.Adapter.create()
)

# App.store.adapter.serializer.configure(App.Comment,
#   sideloadAs: 'comments' 
# )
App.store.adapter.load App.store, App.Post,
  id: 12
  comments: [{text: "blabla", ferment:{fermi: "found fermi"}}]

console.log App.Post.find(12).get("comments.firstObject.text")
console.log App.Post.find(12).get("comments.firstObject.ferment.fermi")

1 个答案:

答案 0 :(得分:0)

在ember-rails中还有一些错误修复: 我们因为功能而受苦

extractEmbeddedBelongsTo: function(loader, relationship, data, parent, prematerialized)     {
var reference = this.extractRecordRepresentation(loader, relationship.type, data, true);
prematerialized[relationship.key] = reference;

// If the embedded record should also be saved back when serializing the parent,
// make sure we set its parent since it will not have an ID.
var embeddedType = this.embeddedType(parent.type, relationship.key);
if (embeddedType === 'always') {
  reference.parent = parent;
}

}

BelongsTo关系正在跳过嵌入对象的提取。错误的是:

extractEmbeddedBelongsTo: function(loader, relationship, data, parent, prematerialized) {
var reference = loader.sideload(relationship.type, data);
prematerialized[relationship.key] = reference;

// If the embedded record should also be saved back when serializing the parent,
// make sure we set its parent since it will not have an ID.
var embeddedType = this.embeddedType(parent.type, relationship.key);
if (embeddedType === 'always') {
  reference.parent = parent;
}

}