Ember-data:反序列化嵌入式模型
Ember-data: deserialize embedded model
我真的不明白...
我有以下代码:
App.Instance = DS.Model.extend({
hash: DS.attr('string'),
users: DS.hasMany('user', { embedded: 'always' })
});
App.User = DS.Model.extend({
name: DS.attr('string'),
color: DS.attr('string'),
lat: DS.attr('number'),
lng: DS.attr('number'),
instance: DS.belongsTo('instance')
});
App.InstanceSerializer = DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
attrs: {
users: { embedded: 'always' }
}
});
像这样的例子:
var instance = {
hash: "68309966ec7fbaac",
id: "54b4518fcbe12d5160771ebe",
users: [{
color: "#9E463C",
id: "78b662bc56169a96",
lat: 36.5299487,
lng: -6.2921774,
name: "User 1"
},{
color: "#9E463C",
id: "78b662bc56169a96",
lat: 36.5299487,
lng: -6.2921774,
name: "User 2"
}]
}
但是当我想要 store.push('instance', instance);
时,我收到:
Uncaught Error: Assertion Failed: Ember Data expected a number or string to represent the record(s) in the users
relationship instead it found an object. If this is a polymorphic relationship please specify a type
key. If this is an embedded relationship please include the DS.EmbeddedRecordsMixin
and specify the users
property in your serializer's attrs
哪里错了?
从所有那些总是使用不同策略的资源中读取:
- http://emberjs.com/api/data/classes/DS.EmbeddedRecordsMixin.html
- How to make embedded hasMany relationships work with ember data
- Ember-data Serialize/Deserialize embedded records on 3rd level
非常感谢
来自这篇文章:http://mozmonkey.com/2013/12/loading-json-with-embedded-records-into-ember-data-1-0-0-beta/ ember 想要像这样侧载您的数据:
var data = {
instance: {
hash: "68309966ec7fbaac",
id: "54b4518fcbe12d5160771ebe",
users: ["78b662bc56169a96", "78b662bc56169a97"]
},
users: [{
color: "#9E463C",
id: "78b662bc56169a96",
lat: 36.5299487,
lng: -6.2921774,
name: "User 1"
},{
color: "#9E463C",
id: "78b662bc56169a96",
lat: 36.5299487,
lng: -6.2921774,
name: "User 2"
}]
}
这里很容易做到:
for (var i=0; i < data.users.length; i++) {
store.push('user', data.users[i]);
}
store.push('instance', data.instance);
在已序列化的文件夹中创建序列化。序列化的名称与您的模型相同。
import DS from 'ember-data';
export default DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
hasEmbeddedAlwaysOption: function(attr) {
var option = this.attrsOption(attr);
if (typeof(option) === 'undefined') {
return true;
} else if (option.embedded === false) {
return false;
}
return this._super.apply(this, arguments);
},
noSerializeOptionSpecified: function(attr) {
var option = this.attrsOption(attr);
if (typeof(option) === 'undefined') {
return false;
} else if (option.embedded === false && !!option.serialize) {
return true;
}
return this._super.apply(this, arguments);
}
});
当对象内部有对象时会发生这种情况。如果您不想为每个包含对象内部对象的模型创建一个像这样的 class,请将此代码复制到 application.js inside serializers 文件夹中。
我真的不明白...
我有以下代码:
App.Instance = DS.Model.extend({
hash: DS.attr('string'),
users: DS.hasMany('user', { embedded: 'always' })
});
App.User = DS.Model.extend({
name: DS.attr('string'),
color: DS.attr('string'),
lat: DS.attr('number'),
lng: DS.attr('number'),
instance: DS.belongsTo('instance')
});
App.InstanceSerializer = DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
attrs: {
users: { embedded: 'always' }
}
});
像这样的例子:
var instance = {
hash: "68309966ec7fbaac",
id: "54b4518fcbe12d5160771ebe",
users: [{
color: "#9E463C",
id: "78b662bc56169a96",
lat: 36.5299487,
lng: -6.2921774,
name: "User 1"
},{
color: "#9E463C",
id: "78b662bc56169a96",
lat: 36.5299487,
lng: -6.2921774,
name: "User 2"
}]
}
但是当我想要 store.push('instance', instance);
时,我收到:
Uncaught Error: Assertion Failed: Ember Data expected a number or string to represent the record(s) in the
users
relationship instead it found an object. If this is a polymorphic relationship please specify atype
key. If this is an embedded relationship please include theDS.EmbeddedRecordsMixin
and specify theusers
property in your serializer's attrs
哪里错了?
从所有那些总是使用不同策略的资源中读取:
- http://emberjs.com/api/data/classes/DS.EmbeddedRecordsMixin.html
- How to make embedded hasMany relationships work with ember data
- Ember-data Serialize/Deserialize embedded records on 3rd level
非常感谢
来自这篇文章:http://mozmonkey.com/2013/12/loading-json-with-embedded-records-into-ember-data-1-0-0-beta/ ember 想要像这样侧载您的数据:
var data = {
instance: {
hash: "68309966ec7fbaac",
id: "54b4518fcbe12d5160771ebe",
users: ["78b662bc56169a96", "78b662bc56169a97"]
},
users: [{
color: "#9E463C",
id: "78b662bc56169a96",
lat: 36.5299487,
lng: -6.2921774,
name: "User 1"
},{
color: "#9E463C",
id: "78b662bc56169a96",
lat: 36.5299487,
lng: -6.2921774,
name: "User 2"
}]
}
这里很容易做到:
for (var i=0; i < data.users.length; i++) {
store.push('user', data.users[i]);
}
store.push('instance', data.instance);
在已序列化的文件夹中创建序列化。序列化的名称与您的模型相同。
import DS from 'ember-data';
export default DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
hasEmbeddedAlwaysOption: function(attr) {
var option = this.attrsOption(attr);
if (typeof(option) === 'undefined') {
return true;
} else if (option.embedded === false) {
return false;
}
return this._super.apply(this, arguments);
},
noSerializeOptionSpecified: function(attr) {
var option = this.attrsOption(attr);
if (typeof(option) === 'undefined') {
return false;
} else if (option.embedded === false && !!option.serialize) {
return true;
}
return this._super.apply(this, arguments);
}
});
当对象内部有对象时会发生这种情况。如果您不想为每个包含对象内部对象的模型创建一个像这样的 class,请将此代码复制到 application.js inside serializers 文件夹中。