1

我可以始终将嵌入式用于一级,但无法将其用于两级深度模型。需要紧急帮助

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-> Comments,Comment -hasOne-> Ferment。这工作正常,这是代码。

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")
4

1 回答 1

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

}

于 2013-02-18T15:57:21.877 回答