1
votes

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
I guess I need to write sideload mappings, can anyone help me write this.sudhanshu
Someone an answer would be great.!!! Cannot pass arguments as options to the JSONtransforms, almost stuck only way out is to write a mapper.sudhanshu

1 Answers

0
votes

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

}