]>
git.proxmox.com Git - proxmox-widget-toolkit.git/blob - data/DiffStore.js
db608c56ac8c00d47a98c3ef4582b714c2cc57d4
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 true, destroy rstore on destruction. Defaults to true if a rstore
28 // config is passed instead of an existing rstore instance
29 autoDestroyRstore
: false,
31 onDestroy: function() {
33 if (me
.autoDestroyRstore
) {
34 if (Ext
.isFunction(me
.rstore
.destroy
)) {
42 constructor: function(config
) {
45 config
= config
|| {};
48 throw "no rstore specified";
51 if (!config
.rstore
.model
) {
52 throw "no rstore model specified";
56 if (config
.rstore
.isInstance
) {
57 rstore
= config
.rstore
;
58 } else if (config
.rstore
.type
) {
59 Ext
.applyIf(config
.rstore
, {
60 autoDestroyRstore
: true,
62 rstore
= Ext
.create(`store.${config.rstore.type}`, config
.rstore
);
64 throw 'rstore is not an instance, and cannot autocreate without "type"';
69 proxy
: { type
: 'memory' },
72 me
.callParent([config
]);
76 var first_load
= true;
78 var cond_add_item = function(data
, id
) {
79 var olditem
= me
.getById(id
);
82 Ext
.Array
.each(me
.model
.prototype.fields
, function(field
) {
83 if (olditem
.data
[field
.name
] !== data
[field
.name
]) {
84 olditem
.set(field
.name
, data
[field
.name
]);
87 olditem
.endEdit(true);
90 var newrec
= Ext
.create(me
.model
, data
);
91 var pos
= me
.appendAtStart
&& !first_load
? 0 : me
.data
.length
;
92 me
.insert(pos
, newrec
);
96 var loadFn = function(s
, records
, success
) {
103 // getSource returns null if data is not filtered
104 // if it is filtered it returns all records
105 var allItems
= me
.getData().getSource() || me
.getData();
107 // remove vanished items
108 allItems
.each(function(olditem
) {
109 var item
= me
.rstore
.getById(olditem
.getId());
115 me
.rstore
.each(function(item
) {
116 cond_add_item(item
.data
, item
.getId());
121 if (me
.sortAfterUpdate
) {
128 me
.fireEvent('refresh', me
);
129 me
.fireEvent('datachanged', me
);
132 if (me
.rstore
.isLoaded()) {
133 // if store is already loaded,
134 // insert items instantly
135 loadFn(me
.rstore
, [], true);
138 me
.mon(me
.rstore
, 'load', loadFn
);