简体   繁体   English

将postgres json字段与余烬数据一起使用

[英]Using postgres json field with ember data

I've got a rails api that spits out a postgres field of type json called colors . 我有一个Rails api,它吐出了一个名为colors json类型的postgres字段。 I've got a series of input fields that can be changed and I'm hoping the keys on this json object can be modified when each field changes, but the binding doesn't seem to be working as expected. 我有一系列可以更改的输入字段,我希望每个字段更改时都可以修改此json对象上的键,但是绑定似乎并未按预期工作。

The data looks like: 数据如下:

 "colors": { "primary_color": "#c43535", "secondary_color": "#0000FF" }

My inputs look like: 我的输入如下所示:

{{input type="text" class="form-control" value=model.colors.primary_color}}
{{input type="text" class="form-control" value=model.colors.secondary_color}}

This displays the correct color in the field, but when I update the field, the JSON object does not appear to be updated in the Ember Inspector. 这会在字段中显示正确的颜色,但是当我更新字段时,JSON对象在Ember Inspector中似乎没有更新。

拾色器

I've tried three different transforms so far: 到目前为止,我已经尝试了三种不同的转换:

json.js json.js

export default DS.Transform.extend({

  deserialize: function(serialized) {
    return JSON.parse(serialized);
  },

  serialize: function(deserialized) {
    return JSON.stringify(deserialized);
  }

});

raw.js raw.js

import DS from 'ember-data';

export default DS.Transform.extend({

  deserialize: function(serialized) {
    return serialized;
  },

  serialize: function(deserialized) {
    return deserialized;
  }

});

object.js object.js

export default DS.Transform.extend({

  deserialize: function(serialized) {
    return Ember.isNone(serialized) ? {} : serialized;
  },

  serialize: function(deserialized) {
    return Ember.isNone(deserialized) ? {} : deserialized;
  }

});

All appear to have the same result, that updating the fields does not modify the object. 所有这些似乎都具有相同的结果,即更新字段不会修改对象。 How do I serialize/bind/update a json object with ember-data like this? 我如何像这样用ember-data序列化/绑定/更新json对象?

通过未在DS.attr()指定类型,它仅将原始对象传递通过,您可以在此处看到: http : //emberjs.jsbin.com/zitobi/2/edit

In this case, I think. 我认为在这种情况下。 Serialize, means to make the object into a regular JSON object and deserialize, means to make it into a Ember object. 序列化意味着将对象变成常规JSON对象,反序列化意味着将其变成Ember对象。

App.PrimaryColorTransform = DS.Transform.extend({
  serialize: function(value) {
    return value.get('primary_color');
  },
  deserialize: function(value) {
    return Ember.create({ primary_color: value[0]});
  }
});

App.Color = DS.Model.extend({
  primary_color: DS.attr('primaryColor'),

});

http://emberjs.com/guides/models/the-rest-adapter/#toc_creating-custom-transformations http://emberjs.com/guides/models/the-rest-adapter/#toc_creating-custom-transformations

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM