@@ -28,7 +28,8 @@ <h1>Serialization demo</h1>
2828 < script type ="text/javascript ">
2929 $ ( function ( ) {
3030 var $grid = $ ( '.grid-stack' ) ;
31-
31+ var grid = $grid . gridstack ( ) ;
32+
3233 $grid . on ( 'added' , function ( e , items ) { log ( ' added ' , items ) } ) ;
3334 $grid . on ( 'removed' , function ( e , items ) { log ( ' removed ' , items ) } ) ;
3435 $grid . on ( 'change' , function ( e , items ) { log ( ' change ' , items ) } ) ;
@@ -39,59 +40,50 @@ <h1>Serialization demo</h1>
3940 console . log ( type + items . length + ' items.' + str ) ;
4041 }
4142
42- $grid . gridstack ( ) ;
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+ ] ;
4353
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- ] ;
55-
56- this . grid = $ ( '.grid-stack' ) . data ( 'gridstack' ) ;
57-
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 ) ;
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+ } ;
6863
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 ) ;
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+ } ;
8377
84- this . clearGrid = function ( ) {
85- this . grid . removeAll ( ) ;
86- return false ;
87- } . bind ( this ) ;
78+ clearGrid = function ( ) {
79+ grid . removeAll ( ) ;
80+ }
8881
89- $ ( '#save-grid' ) . click ( this . saveGrid ) ;
90- $ ( '#load-grid' ) . click ( this . loadGrid ) ;
91- $ ( '#clear-grid' ) . click ( this . clearGrid ) ;
82+ $ ( '#save-grid' ) . click ( saveGrid ) ;
83+ $ ( '#load-grid' ) . click ( loadGrid ) ;
84+ $ ( '#clear-grid' ) . click ( clearGrid ) ;
9285
93- this . loadGrid ( ) ;
94- } ;
86+ loadGrid ( ) ;
9587 } ) ;
9688 </ script >
9789</ body >
0 commit comments