@@ -35,20 +35,30 @@ <h1>Serialization demo</h1>
35
35
} ) ;
36
36
37
37
var serializedData = [
38
- { x : 0 , y : 0 , width : 2 , height : 2 } ,
39
- { x : 3 , y : 1 , width : 1 , height : 2 } ,
40
- { x : 4 , y : 1 , width : 1 , height : 1 } ,
41
- { x : 2 , y : 3 , width : 3 , height : 1 } ,
42
- { x : 1 , y : 3 , width : 1 , height : 1 }
38
+ { x : 0 , y : 0 , width : 2 , height : 2 , id : '0' } ,
39
+ { x : 3 , y : 1 , width : 1 , height : 2 , id : '1' } ,
40
+ { x : 4 , y : 1 , width : 1 , height : 1 , id : '2' } ,
41
+ { x : 2 , y : 3 , width : 3 , height : 1 , id : '3' } ,
42
+ { x : 1 , y : 3 , width : 1 , height : 1 , id : '4' }
43
43
] ;
44
44
45
45
loadGrid = function ( ) {
46
- grid . removeAll ( ) ;
47
46
var items = GridStack . Utils . sort ( serializedData ) ;
48
47
grid . batchUpdate ( ) ;
49
- items . forEach ( function ( node ) {
50
- grid . addWidget ( '<div><div class="grid-stack-item-content"></div></div>' , node ) ;
51
- } ) ;
48
+
49
+ if ( grid . engine . nodes . length === 0 ) {
50
+ // load from empty
51
+ items . forEach ( function ( item ) {
52
+ grid . addWidget ( '<div><div class="grid-stack-item-content">' + item . id + '</div></div>' , item ) ;
53
+ } ) ;
54
+ } else {
55
+ // else update existing nodes (instead of calling grid.removeAll())
56
+ grid . engine . nodes . forEach ( function ( node ) {
57
+ var item = items . find ( function ( e ) { return e . id === node . id } ) ;
58
+ grid . update ( node . el , item . x , item . y , item . width , item . height ) ;
59
+ } ) ;
60
+ }
61
+
52
62
grid . commit ( ) ;
53
63
} ;
54
64
@@ -59,7 +69,8 @@ <h1>Serialization demo</h1>
59
69
x : node . x ,
60
70
y : node . y ,
61
71
width : node . width ,
62
- height : node . height
72
+ height : node . height ,
73
+ id : node . id
63
74
} ) ;
64
75
} ) ;
65
76
document . querySelector ( '#saved-data' ) . value = JSON . stringify ( serializedData , null , ' ' ) ;
0 commit comments