Skip to content

Commit ee4031e

Browse files
committed
修复版本不兼容属性、方法
1 parent deed50d commit ee4031e

2 files changed

Lines changed: 41 additions & 6 deletions

File tree

addon/adapters/wildember.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import Ember from 'ember';
22
import DS from 'ember-data';
33
import Waitable from '../mixins/waitable';
44
import toPromise from '../utils/to-promise';
5-
65
import forEach from 'lodash/collection/forEach';
76
import filter from 'lodash/collection/filter';
87
import map from 'lodash/collection/map';
@@ -64,6 +63,7 @@ export default DS.Adapter.extend(Waitable, {
6463
*/
6564
init(application) {
6665
this._super.apply(this, arguments);
66+
// wilddogConfig会在子类中被赋值
6767
let wilddogConfig = this.get('wilddogConfig');
6868
if (!wilddogConfig) {
6969
throw new Error('请在适配器`application`中设置属性`wilddogConfig`!');
@@ -72,9 +72,6 @@ export default DS.Adapter.extend(Waitable, {
7272
// 获取野狗连接
7373
wilddog.initializeApp(wilddogConfig);
7474
let ref = wilddog.sync().ref();
75-
// if (!wd) {
76-
// throw new Error('连接`widdog`失败!');
77-
// }
7875
if (!ref) {
7976
throw new Error('Please set the `wildember` property in the environment config.');
8077
}
@@ -581,10 +578,31 @@ export default DS.Adapter.extend(Waitable, {
581578
* @return {Boolean} Is the relationship embedded?
582579
*/
583580
isRelationshipEmbedded(store, modelName, relationship) {
584-
var serializer = store.serializerFor(modelName);
585-
return serializer.hasDeserializeRecordsOption(relationship.key);
581+
// var serializer = store.serializerFor(modelName);
582+
// return serializer.hasDeserializeRecordsOption(relationship.key);
583+
return this.hasDeserializeRecordsOption(relationship.key);
584+
},
585+
586+
// checks config for attrs option to deserialize records
587+
// a defined option object for a resource is treated the same as
588+
// `deserialize: 'records'`
589+
// 注意:从ember-data2.8版本开始没有这个方法!
590+
hasDeserializeRecordsOption(attr) {
591+
var alwaysEmbed = this.hasEmbeddedAlwaysOption(attr);
592+
var option = this.attrsOption(attr);
593+
return alwaysEmbed || (option && option.deserialize === 'records');
586594
},
587595

596+
// checks config for attrs option to embedded (always) - serialize and deserialize
597+
hasEmbeddedAlwaysOption(attr) {
598+
var option = this.attrsOption(attr);
599+
return option && option.embedded === 'always';
600+
},
601+
602+
attrsOption(attr) {
603+
var attrs = this.get('attrs');
604+
return attrs && (attrs[camelize(attr)] || attrs[attr]);
605+
},
588606

589607
/**
590608
* Determine from if the record is embedded via implicit relationships.

addon/serializers/wildember.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,23 @@ export default DS.JSONSerializer.extend(DS.EmbeddedRecordsMixin, {
142142
});
143143
},
144144

145+
// checks config for attrs option to deserialize records
146+
// a defined option object for a resource is treated the same as
147+
// `deserialize: 'records'`
148+
hasDeserializeRecordsOption(attrs, attr) {
149+
var alwaysEmbed = hasEmbeddedAlwaysOption(attrs, attr);
150+
var option = attrsOption(attrs, attr);
151+
var hasSerializingOption = option && (option.deserialize || option.serialize);
152+
return alwaysEmbed || hasSerializingOption /* option.deserialize === 'records' */;
153+
},
154+
// checks config for attrs option to embedded (always) - serialize and deserialize
155+
hasEmbeddedAlwaysOption(attrs, attr) {
156+
var option = attrsOption(attrs, attr);
157+
return option && option.embedded === 'always';
158+
},
159+
attrsOption(attrs, attr) {
160+
return attrs && (attrs[Ember.String.camelize(attr)] || attrs[attr]);
161+
},
145162

146163
/**
147164
* Coerce arrays back into relationship arrays. When numeric ids are used

0 commit comments

Comments
 (0)