-
Notifications
You must be signed in to change notification settings - Fork 47
/
index.js
101 lines (96 loc) · 2 KB
/
index.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
93
94
95
96
97
98
99
100
101
import Ember from 'ember';
import Pretender from 'pretender';
const LEGACY_PAYLOAD = {
'fruit': [
{
id: 1,
name: 'apple'
}, {
id: 2,
name: 'pear'
}, {
id: 3,
name: 'orange'
}, {
id: 4,
name: 'grape'
}
]
};
const PAYLOAD = {
data: [{
type: 'fruit',
id: 1,
attributes: {
name: 'apple'
}
}, {
type: 'fruit',
id: 2,
attributes: {
name: 'pear'
}
}, {
type: 'fruit',
id: 3,
attributes: {
name: 'orange'
}
}, {
type: 'fruit',
id: 4,
attributes: {
name: 'grape'
}
}]
};
export default Ember.Route.extend({
model() {
let arr = [];
this.store.pushPayload('fruit', !this.store.peekAll ? LEGACY_PAYLOAD : PAYLOAD);
if (!this.store.peekAll) {
arr = [1, 2, 3, 4].map(id => this.store.getById('fruit', id));
} else {
arr = this.store.peekAll('fruit');
}
return Ember.A(arr);
// return this.get('store').findAll('fruit');
},
beforeModel() {
this._super(...arguments);
if (!Ember.testing) {
this._setupPretender();
}
},
deactivate() {
this._super(...arguments);
},
_setupPretender() {
let server = new Pretender();
// server.get('/fruits', request => {
// return [200, {}, JSON.stringify({
// fruits:
// })];
// });
server.put('/fruits/:id/doRipen', request => {
let controller = this.get('controller');
controller.get('requests').addObject({
url: request.url,
data: JSON.parse(request.requestBody)
});
return [200, {}, '{"status": "ok"}'];
});
server.put('/fruits/ripenEverything', request => {
let controller = this.get('controller');
controller.get('requests').addObject({
url: request.url,
data: JSON.parse(request.requestBody)
});
return [200, {}, '{"status": "ok"}'];
});
this.set('server', server);
},
_teardownPretender() {
this.get('server').shutdown();
}
});