Ember.js如何添加belongsTo关系?

时间:2015-12-04 23:30:23

标签: ember.js ember-data

我的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

我做错了什么?

1 个答案:

答案 0 :(得分:0)

您需要使用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();
});
相关问题