保存嵌套模型 [英] Saving nested models

查看:90
本文介绍了保存嵌套模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个这样的模型:

  App.Build = DS.Model.extend({
allegiance :DS.attr('string'),
职业:DS.attr('string'),
技能:DS.hasMany('技能')
});

App.Skill = DS.Model.extend({
name:DS.attr('string'),
value:DS.attr('number')
});

在我的应用程序中,我有控制设置每个技能的效忠,职业和价值(有最多55个)。



然后在我的应用程序控制器的动作哈希中,我有一个动作将构建模型保存到服务器。

  save:function(){
var store = this.get('store');
var skills = this.get('controllers.skills')。get('model');
console.log(技能);
var build = store.createRecord('build',{
professional:1,
allegiance:1,
skills:skill
});

build.set('技能',技能);
build.save();

console.log('Saved!');
}

但是,当构建模型发送到服务器时,技能属性是空的数组:

  {build:{效忠:1,专业:1 :[]}} 

我确定我做错了,但我可以'弄清楚什么也找不到任何有关它的好文档。一个额外的说明,我所关心的所有提交是技能id和价值。



任何帮助将不胜感激!



更新:



按照Daniel的建议,我已经编辑了保存功能,使用pushObjects将技能放到Build模型中,然后保存。它现在工作得更好生成的帖子数据现在是这样的:

  {build:{
效忠:1,
专业:1,
技能:[1,2,3,4,5,6,7,8 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,21 , 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45,46 ,47,48,49,50,51,52,53,54,55]}}

这是一个技能列表。没有其他属性在帖子中提交。我尝试过迭代技巧,创建一个新的对象,只需要推入id和value,这是我需要的唯一部分,但是这给我一个错误。有些东西,不能使用未定义的,必须是类型技能。



这似乎是Ember数据应该处理的东西。有没有什么东西让它发送请求中的其他技能属性?



谢谢!!

解决方案

如果有其他人感兴趣,我通过用这样的 Build 模型的自定义serliazer覆盖serlizer来解决问题:{
serializeHasMany:function(record,json,relationship) {
if(relationship.key ==='skills'){
var skills = record.get('skills');
var block = [];

skills.forEach(function(skill,index){
var current = {};
current.id = skill.get('id');
current.value = skill。 get('value')
block [index] = current;
});

json ['skill'] = block;

} else {
return this._sup呃(记录,json,关系);
}
}
});

更新:



有一个更容易现在使用 DS.EmbeddedRecordsMixin 这样做:

  App.BuildSerializer = DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin,{
attrs:{
skills: 'record'
}
});


I have two models like this:

App.Build = DS.Model.extend({
    allegiance: DS.attr('string'),
    profession: DS.attr('string'),
        skills: DS.hasMany('skill')
});

App.Skill = DS.Model.extend({
    name:DS.attr('string'),
    value:DS.attr('number')
});

In my app, I have controls to set the allegiance, profession, and values of each skill (there's up to 55).

Then in the actions hash of my application controller, I have an action to save the build model to the server.

save:function(){
     var store = this.get('store');
     var skills = this.get('controllers.skills').get('model');
     console.log(skills);
     var build = store.createRecord('build',{
          profession:1,
          allegiance:1,
          skills:skills
      });

      build.set('skills',skills);
      build.save();

      console.log('Saved!');
}

But when the build model is sent to the server the skills property is an empty array:

{"build":{"allegiance":"1","profession":"1","skills":[]}}

I'm sure I'm doing something wrong, but I can't figure out what and can't find any good documentation about it. An additional note, all I care about submitting is the skill id and value.

Any help will be greatly appreciated!

UPDATE:

Following Daniel's suggestion, I've edited the save function to use pushObjects to put the skills into the Build model, then save it. It's working better now. The generated post data is like this now:

{"build":{
    "allegiance":1,
     "profession":1,
          "skills":["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49","50","51","52","53","54","55"]}}

That being a list of the skill ids. None of the other attributes are submitted in the post. I've tried iterating over skills, creating a new object, and just pushing in the id and value, which are the only parts I need, but that gives me an error. Something like, can not use undefined, must be type skill.

This seems like something Ember data should handle natively. Is there something I'm missing to get it to send the other skill attributes in the request?

Thanks!!

解决方案

If anyone else is interested, I solved the issue by overriding the serlizer with a custom serliazer for the Build model like this:

App.BuildSerializer = DS.RESTSerializer.extend({
     serializeHasMany: function(record, json, relationship) {
         if(relationship.key === 'skills') {
            var skills = record.get('skills');
            var block = [];

            skills.forEach(function(skill, index) {
                var current = {};
                current.id = skill.get('id');
                current.value = skill.get('value')
                block[index] = current;
            });

            json['skills'] = block;

         } else {
             return this._super(record,json,relationship);
         }
     }
});

UPDATE:

There's a much easier way to do this now using the DS.EmbeddedRecordsMixin like this:

App.BuildSerializer = DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin,{
    attrs: {
        skills: 'records'
    }
});

这篇关于保存嵌套模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆