]>
git.proxmox.com Git - proxmox-widget-toolkit.git/blob - data/DiffStore.js
2 * The DiffStore is a in-memory store acting as proxy between a real store
3 * instance and a component.
4 * Its purpose is to redisplay the component *only* if the data has been changed
5 * inside the real store, to avoid the annoying visual flickering of using
6 * the real store directly.
9 * The DiffStore monitors via mon() the 'load' events sent by the real store.
10 * On each 'load' event, the DiffStore compares its own content with the target
11 * store (call to cond_add_item()) and then fires a 'refresh' event.
12 * The 'refresh' event will automatically trigger a view refresh on the component
13 * who binds to this store.
17 * rstore: the realstore which will autorefresh its content from the API
18 * Only works if rstore has a model and use 'idProperty'
19 * sortAfterUpdate: sort the diffstore before rendering the view
21 Ext
.define('Proxmox.data.DiffStore', {
22 extend
: 'Ext.data.Store',
25 sortAfterUpdate
: false,
27 // if set to true, destroy rstore on destruction
28 autoDestroyRstore
: false,
30 onDestroy: function() {
32 if (me
.autoDestroyRstore
) {
33 if (Ext
.isFunction(me
.rstore
.destroy
)) {
41 constructor: function(config
) {
44 config
= config
|| {};
47 throw "no rstore specified";
50 if (!config
.rstore
.model
) {
51 throw "no rstore model specified";
54 var rstore
= config
.rstore
;
58 proxy
: { type
: 'memory' }
61 me
.callParent([config
]);
65 var first_load
= true;
67 var cond_add_item = function(data
, id
) {
68 var olditem
= me
.getById(id
);
71 Ext
.Array
.each(me
.model
.prototype.fields
, function(field
) {
72 if (olditem
.data
[field
.name
] !== data
[field
.name
]) {
73 olditem
.set(field
.name
, data
[field
.name
]);
76 olditem
.endEdit(true);
79 var newrec
= Ext
.create(me
.model
, data
);
80 var pos
= (me
.appendAtStart
&& !first_load
) ? 0 : me
.data
.length
;
81 me
.insert(pos
, newrec
);
85 var loadFn = function(s
, records
, success
) {
93 // getSource returns null if data is not filtered
94 // if it is filtered it returns all records
95 var allItems
= me
.getData().getSource() || me
.getData();
97 // remove vanished items
98 allItems
.each(function(olditem
) {
99 var item
= me
.rstore
.getById(olditem
.getId());
105 me
.rstore
.each(function(item
) {
106 cond_add_item(item
.data
, item
.getId());
111 if (me
.sortAfterUpdate
) {
118 me
.fireEvent('refresh', me
);
119 me
.fireEvent('datachanged', me
);
122 if (me
.rstore
.isLoaded()) {
123 // if store is already loaded,
124 // insert items instantly
125 loadFn(me
.rstore
, [], true);
128 me
.mon(me
.rstore
, 'load', loadFn
);