[英]Ember.js how to add belongsTo relationship?
我的EmberData中有一個評估模型,該模型與Thread模型具有belongsTo關系。 我想創建一個新線程並將其分配給Evalutation,然后將其保存到后端。 我的代碼如下所示:
var thread = self.get("store").createRecord("thread", {
title: self.get("thread_name"),
private: true
});
thread.save().then(function() {
var post = self.get("store").createRecord("post", {
author: self.get("session.profile.id"),
body: self.get("thread_content"),
thread: thread,
private: true
});
return post.save();
}).then(function() {
// assign the realtionship - HERE is the problem
self.set("model.comment", thread); // This call fails
return self.get("model").save();
}
我得到這個不清楚的堆棧跟蹤...
post_thread/<@http://localhost:4200/assets/ksi.js:370:21
tryCatch@http://localhost:4200/assets/vendor.js:64238:14
invokeCallback@http://localhost:4200/assets/vendor.js:64253:15
publish@http://localhost:4200/assets/vendor.js:64221:9
@http://localhost:4200/assets/vendor.js:41059:7
Queue.prototype.invoke@http://localhost:4200/assets/vendor.js:10314:9
Queue.prototype.flush@http://localhost:4200/assets/vendor.js:10378:11
DeferredActionQueues.prototype.flush@http://localhost:4200/assets/vendor.js:10178:11
Backburner.prototype.end@http://localhost:4200/assets/vendor.js:9571:9
Backburner.prototype.run@http://localhost:4200/assets/vendor.js:9639:13
run@http://localhost:4200/assets/vendor.js:28889:12
ember$data$lib$adapters$rest$adapter$$RestAdapter<.ajax/</hash.success@http://localhost:4200/assets/vendor.js:72687:15
jQuery.Callbacks/fire@http://localhost:4200/assets/vendor.js:3301:10
jQuery.Callbacks/self.fireWith@http://localhost:4200/assets/vendor.js:3413:7
done@http://localhost:4200/assets/vendor.js:8466:5
.send/callback/<@http://localhost:4200/assets/vendor.js:8807:1
我究竟做錯了什么?
您需要使用self
,而不是this
:
var self = this; // I'm assuming you do this somewhere
thread.save().then(function() {
...
}).then(function() {
// assign the realtionship - HERE is the problem
self.set("model.comment", thread); // This call fails
return self.get("model").save();
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.