我正试图设定一个相当简单的Ember关系,但它并没有发生。前提是我正在创建一个“入门”模型的足球比赛预测。单击时,'预测'动作将被传递给正确的'俱乐部',并且所需要做的就是设置当前用户和输入记录上的正确预测。然后保存它。
这是'play'控制器。我评论了每个阶段的回报。
var PlayController = Ember.ArrayController.extend({
actions: {
predict: function(match, club) {
var userId = this.get('session.currentUser.id');
var user = this.store.getById('user', userId);
var predictionId = club.get('id');
var prediction = this.store.getById('club', predictionId);
console.log('User: ' + user); // <(subclass of DS.Model):ember487:1>
console.log('Prediction: ' + prediction); // <(subclass of DS.Model):ember507:4>
var entry = {
user: user,
prediction: prediction
}
var entry = this.store.createRecord('entry', entry);
// Have tried using entry.set('prediction', prediction) & entry.set('user', user) here to no effect.
console.log(entry.get('prediction')); // returns a promise with correct data
console.log(entry.get('user')); // returns a promise with correct data
entry.save().then(function() {
console.log(entry); // returns created entry object
console.log(entry.get('prediction')); // returning null
console.log(entry.get('user')); // returning null
});
}
}
});
export default PlayController;
这是我的JSON回复:
{"entry":{"id":1,"prediction_id":null,"user_id":null}}
记录中,这是我的关系。我已尝试设置反转,但这也无法解决问题。
//app/models/club.js
var Club = DS.Model.extend({
entries: DS.hasMany('entry', {async: true})
}); export default Club;
//app/models/entry.js
var Entry = DS.Model.extend({
prediction: DS.belongsTo('club', {async: true}),
user: DS.belongsTo('user', {async: true})
}); export default Entry;
//app/models/user.js
var User = DS.Model.extend({
entries: DS.hasMany('entry', {async: true})
}); export default User;
并证明我不会在后端搞砸了:
// Entries controller
class Api::EntriesController < Api::BaseController
def create
entry = Entry.new(entry_params)
if entry.save
render json: entry, status: :created
else
respond_with entry
end
end
private
def entry_params
params.require(:entry).permit(:id, :prediction_id, :user_id)
end
end
// Entry Serializer
class EntrySerializer < ActiveModel::Serializer
attributes :id, :prediction_id, :match_id, :user_id
embed :ids, include: true
end
任何想法都超过欢迎!
答案 0 :(得分:0)
商店是异步的,因此您需要在记录上使用then
,以确保在使用它们之前已经加载它们。
predict: function(match, club) {
var userId = this.get('session.currentUser.id');
var user = this.store.getById('user', userId);
var predictionId = club.get('id');
var prediction = this.store.getById('club', predictionId);
Ember.RSVP.hash({user:user, prediction:prediction}).then(function(result){
console.log('User: ' + result.user); // <(subclass of DS.Model):ember487:1>
console.log('Prediction: ' + result.prediction); // <(subclass of DS.Model):ember507:4>
var entry = {
user: result.user,
prediction: result.prediction
}
var entry = this.store.createRecord('entry', entry);
// Have tried using entry.set('prediction', prediction) & entry.set('user', user) here to no effect.
console.log(entry.get('prediction')); // returns a promise with correct data
console.log(entry.get('user')); // returns a promise with correct data
entry.save().then(function() {
console.log(entry); // returns created entry object
console.log(entry.get('prediction')); // returning null
console.log(entry.get('user')); // returning null
});
});
}
save: function(){
var store = this.get('store');
Ember.RSVP.hash({
foo: store.find('foo', 1),
bar: store.find('bar', 1)
}).then(function(results){
// results looks like {foo: fooRecord, bar: barRecord}
var foobar = store.createRecord('fooBar', results);
console.log(Em.get(results, 'foo.id'));
console.log(Em.get(results, 'bar.id'));
foobar.save().then(function(){
// don't forget, foo and bar are async on foobar
// you need to make sure they've resolved
// before you attempt to use them
Em.RSVP.hash({
foo: foobar.get('foo'),
bar: foobar.get('bar')
}).then(function(results2){
console.log(Em.get(results2, 'foo.id'));
console.log(Em.get(results2, 'bar.id'));
});
});
});
}