Ember data multi level hierarchy with embedded alw

2019-01-12 05:20发布

问题:

I am able to use embedded always for one level but I am unable to use it for two level deep model. Need an urgent help

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

I get log for comment.text as blabla But cannot get second part to work. I am using revision 11 of Ember data store. Any one with similar problem/solution.

Interestingly we tried hasMany so Post -hasMany-> Comments, Comment -hasOne-> Ferment. This works fine, here is the code.

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:

There is still some bug fixes left in ember-rails: We suffered because of the function

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 relation is skipping the extraction of embedded objects. The wrong one is here:

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

}