]>
Commit | Line | Data |
---|---|---|
67641b24 | 1 | /* |
636247e2 EK |
2 | * The DiffStore is a in-memory store acting as proxy between a real store |
3 | * instance and a component. | |
67641b24 | 4 | * Its purpose is to redisplay the component *only* if the data has been changed |
636247e2 EK |
5 | * inside the real store, to avoid the annoying visual flickering of using |
6 | * the real store directly. | |
67641b24 EK |
7 | * |
8 | * Implementation: | |
636247e2 | 9 | * The DiffStore monitors via mon() the 'load' events sent by the real store. |
67641b24 EK |
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. | |
14 | */ | |
15 | ||
521bcdac | 16 | /* Config properties: |
636247e2 | 17 | * rstore: the realstore which will autorefresh its content from the API |
521bcdac | 18 | * Only works if rstore has a model and use 'idProperty' |
636247e2 | 19 | * sortAfterUpdate: sort the diffstore before rendering the view |
521bcdac DM |
20 | */ |
21 | Ext.define('PVE.data.DiffStore', { | |
22 | extend: 'Ext.data.Store', | |
23 | ||
24 | sortAfterUpdate: false, | |
25 | ||
26 | constructor: function(config) { | |
27 | var me = this; | |
28 | ||
29 | config = config || {}; | |
30 | ||
31 | if (!config.rstore) { | |
32 | throw "no rstore specified"; | |
33 | } | |
34 | ||
35 | if (!config.rstore.model) { | |
36 | throw "no rstore model specified"; | |
37 | } | |
38 | ||
39 | var rstore = config.rstore; | |
40 | ||
41 | Ext.apply(config, { | |
42 | model: rstore.model, | |
43 | proxy: { type: 'memory' } | |
44 | }); | |
45 | ||
46 | me.callParent([config]); | |
47 | ||
48 | var first_load = true; | |
49 | ||
50 | var cond_add_item = function(data, id) { | |
51 | var olditem = me.getById(id); | |
52 | if (olditem) { | |
53 | olditem.beginEdit(); | |
5cd975e2 EK |
54 | Ext.Array.each(me.model.prototype.fields, function(field) { |
55 | if (olditem.data[field.name] !== data[field.name]) { | |
56 | olditem.set(field.name, data[field.name]); | |
521bcdac DM |
57 | } |
58 | }); | |
59 | olditem.endEdit(true); | |
60 | olditem.commit(); | |
61 | } else { | |
5cd975e2 | 62 | var newrec = Ext.create(me.model, data); |
521bcdac DM |
63 | var pos = (me.appendAtStart && !first_load) ? 0 : me.data.length; |
64 | me.insert(pos, newrec); | |
65 | } | |
66 | }; | |
67 | ||
68 | me.mon(rstore, 'load', function(s, records, success) { | |
69 | ||
70 | if (!success) { | |
71 | return; | |
72 | } | |
73 | ||
74 | me.suspendEvents(); | |
75 | ||
76 | // remove vanished items | |
77 | (me.snapshot || me.data).each(function(olditem) { | |
78 | var item = rstore.getById(olditem.getId()); | |
79 | if (!item) { | |
80 | me.remove(olditem); | |
81 | } | |
82 | }); | |
5cd975e2 | 83 | |
521bcdac DM |
84 | rstore.each(function(item) { |
85 | cond_add_item(item.data, item.getId()); | |
86 | }); | |
87 | ||
88 | me.filter(); | |
89 | ||
90 | if (me.sortAfterUpdate) { | |
91 | me.sort(); | |
92 | } | |
93 | ||
94 | first_load = false; | |
95 | ||
96 | me.resumeEvents(); | |
953d9924 | 97 | me.fireEvent('refresh', me); |
115d1727 | 98 | me.fireEvent('datachanged', me); |
521bcdac DM |
99 | }); |
100 | } | |
101 | }); |