Skip to content

Commit f04f7fa

Browse files
committed
Grid Editing: Extract localStorage persistence into simple abstraction for demos.
1 parent 4c4c016 commit f04f7fa

File tree

3 files changed

+92
-49
lines changed

3 files changed

+92
-49
lines changed

grid-editing/editor.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ $.widget( "ui.editor", {
1313
_create: function() {
1414
this.inner = this.element.wrapInner("<div class='editor-wrapper'></div>").children();
1515
this._bind({
16-
click: "start"
16+
click: function( event ) {
17+
if (this.input.is(":visible")) {
18+
return;
19+
}
20+
this.start( event );
21+
}
1722
});
1823

1924
this.input = this.inputWrapper = $( "<input>" );
@@ -24,13 +29,18 @@ $.widget( "ui.editor", {
2429
}
2530
this.inputWrapper.hide().appendTo( this.element )
2631

27-
this._bind( this.input, {
28-
// TODO ignore clicks (=blur) on spinner controls
29-
blur: function( event ) {
32+
this._bind( this.inputWrapper, {
33+
focusin: function() {
34+
clearTimeout( this.timer );
35+
},
36+
focusout: function( event ) {
3037
if (!this.input.is(":visible")) {
3138
return;
3239
}
33-
this.submit( event );
40+
var that = this;
41+
this.timer = setTimeout( function() {
42+
that.submit( event );
43+
}, 100 );
3444
},
3545
keyup: function( event ) {
3646
if ( event.keyCode === $.ui.keyCode.ENTER || event.keyCode === $.ui.keyCode.NUMPAD_ENTER ) {

grid-editing/grid.html

Lines changed: 17 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,13 @@
1717
<script src="../grid-spf/grid.js"></script>
1818
<script src="../grid-spf/pager.js"></script>
1919
<script src="editor.js"></script>
20+
<script src="localstore.js"></script>
2021
<script>
21-
var localStorageSupport = (function() {
22-
try {
23-
return !!localStorage.getItem;
24-
} catch(e){
25-
return false;
26-
}
27-
})();
28-
29-
var localDevelopers;
30-
if ( localStorageSupport ) {
31-
var item = localStorage.getItem( "grid-editing-developers" );
32-
if ( item ) {
33-
localDevelopers = JSON.parse( item );
34-
}
35-
}
36-
if ( !localDevelopers ) {
37-
$.ajax({
38-
dataType: "json",
39-
url: "../grid-spf/developers.json",
40-
async: false,
41-
success: function( result ) {
42-
localDevelopers = result;
43-
}
44-
});
45-
}
22+
var store = $.demos.localstore( {
23+
key: "grid-editing-developers",
24+
initial: "../grid-spf/developers.json"
25+
});
26+
var localDevelopers = store.load();
4627

4728
$(function() {
4829
var developers = $.ui.localDatasource({
@@ -51,15 +32,8 @@
5132
limit: 8
5233
}
5334
});
54-
developers.save = function( object ) {
55-
if ( object ) {
56-
console.log( "save update to " + object.firstName + " " + object.lastName );
57-
} else {
58-
console.log( "save all" );
59-
}
60-
if ( localStorageSupport ) {
61-
localStorage.setItem( "grid-editing-developers", JSON.stringify( localDevelopers ) )
62-
}
35+
developers.save = function() {
36+
store.save( localDevelopers );
6337
return this;
6438
}
6539

@@ -70,13 +44,13 @@
7044
var th = $( this ).parents( "table:first" ).find( "th" ).eq( this.cellIndex ),
7145
field = th.data( "field" );
7246
$( this ).editor({
73-
type: th.data( "type" ),
74-
submit: function( event, ui ) {
75-
var object = $( this ).tmplItem().data;
76-
object[field] = ui.value;
77-
developers.save( object );
78-
}
79-
})
47+
type: th.data( "type" ),
48+
submit: function( event, ui ) {
49+
var object = $( this ).tmplItem().data;
50+
set(object, field, ui.value);
51+
developers.save();
52+
}
53+
});
8054
});
8155
}
8256
}).data("grid");
@@ -112,7 +86,7 @@
11286
$.each( $( this ).serializeArray(), function( index, object ) {
11387
set( developer, object.name, object.value );
11488
});
115-
developers.save( developer ).refresh();
89+
developers.save().refresh();
11690
editForm.hide();
11791
});
11892
grid.element.delegate( "button.edit", "click", function() {
@@ -144,8 +118,7 @@
144118
country: "Unknown",
145119
bitcoins: 0
146120
});
147-
developers.save();
148-
developers.refresh();
121+
developers.save().refresh();
149122
});
150123

151124
developers.refresh();

grid-editing/localstore.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Little abstraction for localStorage access
3+
*
4+
* Depends on:
5+
* widget
6+
*/
7+
(function( $ ) {
8+
9+
var localStorageSupport = (function() {
10+
try {
11+
return !!localStorage.getItem;
12+
} catch(e){
13+
return false;
14+
}
15+
})();
16+
17+
$.widget( "demos.localstore", {
18+
options: {
19+
key: null,
20+
initial: null
21+
},
22+
_create: function() {
23+
if (this.options.key === null ) {
24+
throw "need to configure a key";
25+
}
26+
if ( this._fresh() && $.type( this.options.initial ) === "string") {
27+
var that = this;
28+
$.ajax({
29+
dataType: "json",
30+
url: this.options.initial,
31+
async: false,
32+
success: function( result ) {
33+
that.save( result );
34+
}
35+
});
36+
}
37+
},
38+
_fresh: function() {
39+
var stored = this.load();
40+
return !stored || stored === this.options.initial;
41+
},
42+
load: function() {
43+
var stored = localStorageSupport
44+
? JSON.parse( localStorage.getItem( this.options.key ) )
45+
: this.data;
46+
if (!stored) {
47+
return this.options.intial;
48+
}
49+
return stored;
50+
},
51+
save: function( data ) {
52+
console.log("save", data)
53+
localStorageSupport
54+
? localStorage.setItem( this.options.key, JSON.stringify( data ) )
55+
: this.data = data;
56+
}
57+
});
58+
59+
60+
})( jQuery );

0 commit comments

Comments
 (0)