大佬教程收集整理的这篇文章主要介绍了ember.js – 如何在Ember数据中嵌入一对一的关系?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有两种模式,一对一的关系:
App.Lesson = Ds.Model.extend timeslot: DS.belongsTo 'App.Timeslot' App.Timeslot = Ds.Model.extend lesson: DS.belongsTo 'App.Lesson'
我有一个适配器配置为在保存时将时隙嵌入课程中:
App.Adapter = Ds.RESTAdapter.extend() App.Adapter.map App.Lesson,timeslot: { embedded: 'always' } App.Store = Ds.Store.extend revision: 11 adapter: App.Adapter.create()
然后我创建一个课程和一个时间段并尝试保存它们:
lesson = App.Lesson.createRecord group: group lesson.set('timeslot',App.Timeslot.createRecord()) lesson.store.commit()
但是在保存时没有嵌入任何东西,我看到POST请求,一个用于课程,一个用于时间段.
我如何告诉Ember始终将时间段嵌入课程中?
当您调用createRecord时,会将一条记录添加到存储桶中,并在提交时使用ember-data简单地在存储桶中的每条记录上触发一条ajax帖子.
因此,为了回到你的代码,使用ember-data,你创建了两个记录,并且在提交时它将触发一个ajax post调用,其中Lesson对象嵌入了Timeslot,并且在后续调用中也将触发另一个ajax post对于Timeslot来说,这是最后一个记录.
lesson = QrTimetable.Lesson.createRecord group: group lesson.set('timeslot',QrTimetable.Timeslot.createRecord()) lesson.store.commit()
除非有更好地理解余烬数据内容的人与我的观点相矛盾,否则我倾向于相信这是一个错误.
这是您提交事务时调用的最后一个代码.
createRecord: function(store,type,record) { var root = this.rootForType(typE); var data = {}; data[root] = this.serialize(record,{ includEID: true }); this.ajax(this.buildURL(root),"POST",{ data: data,context: this,success: function(json) { Ember.run(this,function(){ this.didCreateRecord(store,record,json); }); },error: function(xhr) { this.didError(store,xhr); } }); },
以上是大佬教程为你收集整理的ember.js – 如何在Ember数据中嵌入一对一的关系?全部内容,希望文章能够帮你解决ember.js – 如何在Ember数据中嵌入一对一的关系?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。