MemoryEventStore.js
3.67 KB
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
102
103
/*
* This is a simple in-memory store implementation that is ONLY intended for use with
* calendar samples running locally in the browser with no external data source. Under
* normal circumstances, stores that use a MemoryProxy are read-only and intended only
* for displaying data read from memory. In the case of the calendar, it's still quite
* useful to be able to deal with in-memory data for sample purposes (as many people
* may not have PHP set up to run locally), but by default, updates will not work since the
* calendar fully expects all CRUD operations to be supported by the store (and in fact
* will break, for example, if phantom records are not removed properly). This simple
* class gives us a convenient way of loading and updating calendar event data in memory,
* but should NOT be used outside of the local samples.
*/
Ext.define('Ext.calendar.data.MemoryEventStore', {
extend: 'Ext.data.Store',
model: 'Ext.calendar.data.EventModel',
requires: [
'Ext.data.proxy.Memory',
'Ext.data.reader.Json',
'Ext.data.writer.Json',
'Ext.calendar.data.EventModel',
'Ext.calendar.data.EventMappings'
],
proxy: {
type: 'memory',
reader: {
type: 'json',
root: 'evts'
},
writer: {
type: 'json'
}
},
// private
constructor: function(config){
this.callParent(arguments);
this.sorters = this.sorters || [{
property: Ext.calendar.data.EventMappings.StartDate.name,
direction: 'ASC'
}];
this.idProperty = this.idProperty || Ext.calendar.data.EventMappings.EventId.mapping || 'id';
this.fields = Ext.calendar.data.EventModel.prototype.fields.getRange();
this.onCreateRecords = Ext.Function.createInterceptor(this.onCreateRecords, this.interceptCreateRecords);
this.initRecs();
},
// private - override to make sure that any records added in-memory
// still get a unique PK assigned at the data level
interceptCreateRecords: function(records, operation, success) {
if (success) {
var i = 0,
rec,
len = records.length;
for (; i < len; i++) {
records[i].data[Ext.calendar.data.EventMappings.EventId.name] = records[i].id;
}
}
},
// If the store started with preloaded inline data, we have to make sure the records are set up
// properly as valid "saved" records otherwise they may get "added" on initial edit.
initRecs: function(){
this.each(function(rec){
rec.store = this;
rec.phantom = false;
}, this);
},
// private - override the default logic for memory storage
onProxyLoad: function(operation) {
var me = this,
records;
if (me.data && me.data.length > 0) {
// this store has already been initially loaded, so do not reload
// and lose updates to the store, just use store's latest data
me.totalCount = me.data.length;
records = me.data.items;
}
else {
// this is the initial load, so defer to the proxy's result
var resultSet = operation.getResultSet(),
successful = operation.wasSuccessful();
records = operation.getRecords();
if (resultSet) {
me.totalCount = resultSet.total;
}
if (successful) {
me.loadRecords(records, operation);
}
}
me.loading = false;
me.fireEvent('load', me, records, successful);
}
});