Skip to content

Commit a0c38e1

Browse files
committed
Grid: Rewrite parts of todo-app and master-detail to get rid of non-observable event bindings
1 parent 557cbb3 commit a0c38e1

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

grid-editing/master-detail.html

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,39 @@
2323
var movies = store.load();
2424

2525
$(function() {
26-
$( [ movies ] ).bind("insert remove refresh change", function(event, ui) {
27-
store.save( movies );
28-
$(store).trigger("update");
26+
function renderList() {
27+
list.empty().append( $( "#movie-list-item" ).tmpl( movies ) );
28+
}
29+
function bindChange( items ) {
30+
$.each( items, function( index, item ) {
31+
$.observable( item ).bind( "change", function() {
32+
renderList();
33+
});
34+
});
35+
}
36+
$.observable( movies ).bind("insert remove replaceAll", function( event, ui ) {
37+
if ( event.type === "insert" ) {
38+
bindChange( ui.items );
39+
}
40+
store.save();
41+
renderList();
2942
});
43+
bindChange( movies );
3044

3145
$(":submit").button();
3246

33-
var movie;
3447
var editForm = $( "#editForm" ).hide().submit( function( event ) {
3548
event.preventDefault();
49+
var movie = $( this ).data( "movie" );
3650
$.observable( movie, movies ).property( serializeForm( this ) );
3751
editForm.hide();
3852
});
3953
var list = $("#movies-list").delegate("li:has(li)", "click", function() {
40-
movie = $( this ).tmplItem().data;
54+
var movie = $( this ).tmplItem().data;
55+
editForm.data( "movie", movie );
4156
deserializeForm( editForm, movie );
4257
editForm.show();
4358
});
44-
$(store).bind("update", function() {
45-
list.empty().append( $("#movie-list-item").tmpl( movies ) );
46-
});
4759

4860
$("#addMovie").click(function() {
4961
$.observable(movies).insert({
@@ -57,7 +69,7 @@
5769

5870
$("#editForm").hide();
5971

60-
$(store).trigger("update");
72+
renderList();
6173
});
6274
</script>
6375
<style>

grid-editing/todo-app.html

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,13 @@
2727
// TODO rewrite to do incremental rendering on each todo item
2828
$.observable( todos ).bind("insert remove replaceAll", function() {
2929
store.save();
30+
renderList();
31+
});
32+
function renderList() {
3033
updateAllChecked();
3134
$( "#remaining" ).text( todos.length + (todos.length === 1 ? " item" : " items") + " remaining" );
3235
list.empty().append( $( "#todo-list-item" ).tmpl( todos ) );
33-
})
36+
}
3437
function update( index, checked ) {
3538
todos[ index ].done = checked;
3639
updateAllChecked();
@@ -80,8 +83,7 @@
8083
}
8184
$.observable(todos).replaceAll(todos);
8285
});
83-
// TODO fix this
84-
$.observable( todos ).replaceAll( todos );
86+
renderList();
8587
});
8688
</script>
8789
<style>

0 commit comments

Comments
 (0)