]>
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 constructor: function(config
) {
30 config
= config
|| {};
33 throw "no rstore specified";
36 if (!config
.rstore
.model
) {
37 throw "no rstore model specified";
40 var rstore
= config
.rstore
;
44 proxy
: { type
: 'memory' }
47 me
.callParent([config
]);
49 var first_load
= true;
51 var cond_add_item = function(data
, id
) {
52 var olditem
= me
.getById(id
);
55 Ext
.Array
.each(me
.model
.prototype.fields
, function(field
) {
56 if (olditem
.data
[field
.name
] !== data
[field
.name
]) {
57 olditem
.set(field
.name
, data
[field
.name
]);
60 olditem
.endEdit(true);
63 var newrec
= Ext
.create(me
.model
, data
);
64 var pos
= (me
.appendAtStart
&& !first_load
) ? 0 : me
.data
.length
;
65 me
.insert(pos
, newrec
);
69 var loadFn = function(s
, records
, success
) {
77 // getSource returns null if data is not filtered
78 // if it is filtered it returns all records
79 var allItems
= me
.getData().getSource() || me
.getData();
81 // remove vanished items
82 allItems
.each(function(olditem
) {
83 var item
= rstore
.getById(olditem
.getId());
89 rstore
.each(function(item
) {
90 cond_add_item(item
.data
, item
.getId());
95 if (me
.sortAfterUpdate
) {
102 me
.fireEvent('refresh', me
);
103 me
.fireEvent('datachanged', me
);
106 if (rstore
.isLoaded()) {
107 // if store is already loaded,
108 // insert items instantly
109 loadFn(rstore
, [], true);
112 me
.mon(rstore
, 'load', loadFn
);