@@ -28,8 +28,7 @@ <h1>Serialization demo</h1>
2828 < script type ="text/javascript ">
2929 $ ( function ( ) {
3030 var $grid = $ ( '.grid-stack' ) ;
31- var grid = $grid . gridstack ( ) ;
32-
31+
3332 $grid . on ( 'added' , function ( e , items ) { log ( ' added ' , items ) } ) ;
3433 $grid . on ( 'removed' , function ( e , items ) { log ( ' removed ' , items ) } ) ;
3534 $grid . on ( 'change' , function ( e , items ) { log ( ' change ' , items ) } ) ;
@@ -40,50 +39,59 @@ <h1>Serialization demo</h1>
4039 console . log ( type + items . length + ' items.' + str ) ;
4140 }
4241
43- var serializedData = [
44- { x : 0 , y : 0 , width : 2 , height : 2 } ,
45- { x : 3 , y : 1 , width : 1 , height : 2 } ,
46- { x : 4 , y : 1 , width : 1 , height : 1 } ,
47- { x : 2 , y : 3 , width : 3 , height : 1 } ,
48- { x : 1 , y : 4 , width : 1 , height : 1 } ,
49- { x : 1 , y : 3 , width : 1 , height : 1 } ,
50- { x : 2 , y : 4 , width : 1 , height : 1 } ,
51- { x : 2 , y : 5 , width : 1 , height : 1 }
52- ] ;
42+ $grid . gridstack ( ) ;
5343
54- loadGrid = function ( ) {
55- grid . removeAll ( ) ;
56- var items = GridStackUI . Utils . sort ( serializedData ) ;
57- grid . batchUpdate ( ) ;
58- items . forEach ( function ( node ) {
59- grid . addWidget ( $ ( '<div><div class="grid-stack-item-content"></div></div>' ) , node ) ;
60- } ) ;
61- grid . commit ( ) ;
62- } ;
44+ new function ( ) {
45+ this . serializedData = [
46+ { x : 0 , y : 0 , width : 2 , height : 2 } ,
47+ { x : 3 , y : 1 , width : 1 , height : 2 } ,
48+ { x : 4 , y : 1 , width : 1 , height : 1 } ,
49+ { x : 2 , y : 3 , width : 3 , height : 1 } ,
50+ { x : 1 , y : 4 , width : 1 , height : 1 } ,
51+ { x : 1 , y : 3 , width : 1 , height : 1 } ,
52+ { x : 2 , y : 4 , width : 1 , height : 1 } ,
53+ { x : 2 , y : 5 , width : 1 , height : 1 }
54+ ] ;
6355
64- saveGrid = function ( ) {
65- serializedData = $ ( '.grid-stack > .grid-stack-item:visible' ) . map ( function ( i , el ) {
66- el = $ ( el ) ;
67- var node = el . data ( '_gridstack_node' ) ;
68- return {
69- x : node . x ,
70- y : node . y ,
71- width : node . width ,
72- height : node . height
73- } ;
74- } ) . toArray ( ) ;
75- $ ( '#saved-data' ) . val ( JSON . stringify ( serializedData , null , ' ' ) ) ;
76- } ;
56+ this . grid = $ ( '.grid-stack' ) . data ( 'gridstack' ) ;
7757
78- clearGrid = function ( ) {
79- grid . removeAll ( ) ;
80- }
58+ this . loadGrid = function ( ) {
59+ this . grid . removeAll ( ) ;
60+ var items = GridStackUI . Utils . sort ( this . serializedData ) ;
61+ this . grid . batchUpdate ( ) ;
62+ items . forEach ( function ( node ) {
63+ this . grid . addWidget ( $ ( '<div><div class="grid-stack-item-content"></div></div>' ) , node ) ;
64+ } , this ) ;
65+ this . grid . commit ( ) ;
66+ return false ;
67+ } . bind ( this ) ;
8168
82- $ ( '#save-grid' ) . click ( saveGrid ) ;
83- $ ( '#load-grid' ) . click ( loadGrid ) ;
84- $ ( '#clear-grid' ) . click ( clearGrid ) ;
69+ this . saveGrid = function ( ) {
70+ this . serializedData = $ ( '.grid-stack > .grid-stack-item:visible' ) . map ( function ( i , el ) {
71+ el = $ ( el ) ;
72+ var node = el . data ( '_gridstack_node' ) ;
73+ return {
74+ x : node . x ,
75+ y : node . y ,
76+ width : node . width ,
77+ height : node . height
78+ } ;
79+ } ) . toArray ( ) ;
80+ $ ( '#saved-data' ) . val ( JSON . stringify ( this . serializedData , null , ' ' ) ) ;
81+ return false ;
82+ } . bind ( this ) ;
8583
86- loadGrid ( ) ;
84+ this . clearGrid = function ( ) {
85+ this . grid . removeAll ( ) ;
86+ return false ;
87+ } . bind ( this ) ;
88+
89+ $ ( '#save-grid' ) . click ( this . saveGrid ) ;
90+ $ ( '#load-grid' ) . click ( this . loadGrid ) ;
91+ $ ( '#clear-grid' ) . click ( this . clearGrid ) ;
92+
93+ this . loadGrid ( ) ;
94+ } ;
8795 } ) ;
8896 </ script >
8997</ body >
0 commit comments