【发布时间】:2025-12-19 11:50:09
【问题描述】:
这对我来说似乎一遍又一遍地出现,我想要关于如何处理它的建议。请原谅我在coffeescript中编写示例。
App.Post = DS.Model.extend
title: DS.attr('string')
author: DS.belongsTo('App.Author')
App.Author = DS.Model.extend
name: DS.attr('string')
posts: DS.hasMany('App.Post')
我想同时创建一个作者和相关的帖子。
author = App.Author.createRecord({name: 'waldo'});
author.get('posts').createRecord({title: 'how to do the Ember'})
或者
post = App.Post.createRecord({author: author})
问题是,我服务器上的 Post 模型验证了 author_id 的存在。所以当我调用store.commit() 时,author 提交成功,但post 被拒绝,因为post.get('author.id') //=> undefined
更新:作者模型还在服务器上进行了验证,表明它需要至少创建一个帖子,因此我无法在创建帖子之前调用store.commit()。
我处理交易失败。我会继续搞砸它,但这似乎是一个很好的问题,可以在 S/O 上回答以供将来参考。
【问题讨论】:
标签: ember.js ember-data