简体   繁体   English

Ember.js如何添加belongsTo关系?

[英]Ember.js how to add belongsTo relationship?

I have an Evaluation model in my EmberData, which has a belongsTo relationship with Thread model. 我的EmberData中有一个评估模型,该模型与Thread模型具有belongsTo关系。 I'd like to create a new thread and assign it to the Evalutation and then save it to backend. 我想创建一个新线程并将其分配给Evalutation,然后将其保存到后端。 My code looks like this: 我的代码如下所示:

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

I am getting this unclear stacktrace... 我得到这个不清楚的堆栈跟踪...

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

What am I doing wrong? 我究竟做错了什么?

You need to use self instead of this : 您需要使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM