-
Notifications
You must be signed in to change notification settings - Fork 47
/
utilities.js
92 lines (78 loc) · 2.27 KB
/
utilities.js
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import Ember from 'ember';
export const modelTransform = function(model, polymorphic) {
if (polymorphic) {
return { id: model.id, type: model.modelName || model.constructor.modelName };
}
return model.id;
};
export const relationShipTransform = {
belongsTo: {
serialize(model, key, options) {
let relationship = model.belongsTo(key).belongsToRelationship;
let value = relationship.hasOwnProperty('inverseRecordData') ? relationship.inverseRecordData: relationship.canonicalState;
return value && modelTransform(value, options.polymorphic);
},
deserialize() {
}
},
hasMany: {
serialize(model, key, options) {
let relationship = model.hasMany(key).hasManyRelationship;
let value = relationship.currentState;
return value && value.map(item => modelTransform(item, options.polymorphic));
},
deserialize() {
}
}
};
export const relationshipKnownState = {
belongsTo: {
isKnown(model, key) {
let belongsTo = model.belongsTo(key);
let relationship = belongsTo.belongsToRelationship;
return !relationship.relationshipIsStale;
}
},
hasMany: {
isKnown(model, key) {
let hasMany = model.hasMany(key);
let relationship = hasMany.hasManyRelationship;
return !relationship.relationshipIsStale;
}
}
};
export const isEmpty = function(value) {
if (Ember.typeOf(value) === 'object') {
return Object.keys(value).length === 0;
}
return Ember.isEmpty(value);
};
export const didSerializedModelChange = function(one, other, polymorphic) {
if (polymorphic) {
return one.id !== other.id || one.type !== other.type;
}
return one !== other;
};
export const didModelsChange = function(one, other, polymorphic) {
if (isEmpty(one) && isEmpty(other)) {
return false;
}
if ((one && one.length) !== (other && other.length)) {
return true;
}
for (let i = 0, len = one.length; i < len; i++) {
if (didSerializedModelChange(one[i], other[i], polymorphic)) {
return true;
}
}
return false;
};
export const didModelChange = function(one, other, polymorphic) {
if (isEmpty(one) && isEmpty(other)) {
return false;
}
if (!one && other || one && !other) {
return true;
}
return didSerializedModelChange(one, other, polymorphic);
};