Commit 757e78ae authored by Sam Saccone's avatar Sam Saccone Committed by Sindre Sorhus

Close GH-991: Fix Marionette Example to comply to spec 100%.

parent 79f5c1f9
...@@ -6,6 +6,6 @@ ...@@ -6,6 +6,6 @@
"todomvc-common": "~0.1.7", "todomvc-common": "~0.1.7",
"underscore": "~1.4.4", "underscore": "~1.4.4",
"backbone.localStorage": "~1.1.6", "backbone.localStorage": "~1.1.6",
"backbone.marionette": "~1.0.4" "backbone.marionette": "~2.1.0"
} }
} }
...@@ -27,13 +27,13 @@ ...@@ -27,13 +27,13 @@
</span> </span>
<ul id="filters"> <ul id="filters">
<li class="all"> <li class="all">
<a href="#">All</a> <a href="#/">All</a>
</li> </li>
<li class="active"> <li class="active">
<a href="#active">Active</a> <a href="#/active">Active</a>
</li> </li>
<li class="completed"> <li class="completed">
<a href="#completed">Completed</a> <a href="#/completed">Completed</a>
</li> </li>
</ul> </ul>
<button id="clear-completed" <% if (!completedCount) { %>class="hidden"<% } %>> <button id="clear-completed" <% if (!completedCount) { %>class="hidden"<% } %>>
......
...@@ -60,7 +60,7 @@ TodoMVC.module('Layout', function (Layout, App, Backbone) { ...@@ -60,7 +60,7 @@ TodoMVC.module('Layout', function (Layout, App, Backbone) {
}, },
initialize: function () { initialize: function () {
this.listenTo(App.vent, 'todoList:filter', this.updateFilterSelection, this); this.listenTo(App.request('filterState'), 'change:filter', this.updateFilterSelection, this);
}, },
serializeData: function () { serializeData: function () {
...@@ -80,10 +80,9 @@ TodoMVC.module('Layout', function (Layout, App, Backbone) { ...@@ -80,10 +80,9 @@ TodoMVC.module('Layout', function (Layout, App, Backbone) {
}, },
updateFilterSelection: function () { updateFilterSelection: function () {
this.ui.filters this.ui.filters.removeClass('selected');
.removeClass('selected') this.ui[App.request('filterState').get('filter')]
.filter('[href="' + (location.hash || '#') + '"]') .addClass('selected');
.addClass('selected');
}, },
onClearClick: function () { onClearClick: function () {
......
...@@ -16,7 +16,7 @@ TodoMVC.module('TodoList.Views', function (Views, App, Backbone, Marionette, $) ...@@ -16,7 +16,7 @@ TodoMVC.module('TodoList.Views', function (Views, App, Backbone, Marionette, $)
}, },
events: { events: {
'click .destroy': 'destroy', 'click .destroy': 'deleteModel',
'dblclick label': 'onEditClick', 'dblclick label': 'onEditClick',
'keydown .edit': 'onEditKeypress', 'keydown .edit': 'onEditKeypress',
'focusout .edit': 'onEditFocusout', 'focusout .edit': 'onEditFocusout',
...@@ -37,7 +37,7 @@ TodoMVC.module('TodoList.Views', function (Views, App, Backbone, Marionette, $) ...@@ -37,7 +37,7 @@ TodoMVC.module('TodoList.Views', function (Views, App, Backbone, Marionette, $)
} }
}, },
destroy: function () { deleteModel: function () {
this.model.destroy(); this.model.destroy();
}, },
...@@ -83,8 +83,8 @@ TodoMVC.module('TodoList.Views', function (Views, App, Backbone, Marionette, $) ...@@ -83,8 +83,8 @@ TodoMVC.module('TodoList.Views', function (Views, App, Backbone, Marionette, $)
// filtering of activs vs completed items for display. // filtering of activs vs completed items for display.
Views.ListView = Backbone.Marionette.CompositeView.extend({ Views.ListView = Backbone.Marionette.CompositeView.extend({
template: '#template-todoListCompositeView', template: '#template-todoListCompositeView',
itemView: Views.ItemView, childView: Views.ItemView,
itemViewContainer: '#todo-list', childViewContainer: '#todo-list',
ui: { ui: {
toggle: '#toggle-all' toggle: '#toggle-all'
...@@ -98,6 +98,18 @@ TodoMVC.module('TodoList.Views', function (Views, App, Backbone, Marionette, $) ...@@ -98,6 +98,18 @@ TodoMVC.module('TodoList.Views', function (Views, App, Backbone, Marionette, $)
'all': 'update' 'all': 'update'
}, },
initialize: function () {
this.listenTo(App.request('filterState'), 'change:filter', this.render, this);
},
addChild: function (child) {
var filteredOn = App.request('filterState').get('filter');
if (child.matchesFilter(filteredOn)) {
Backbone.Marionette.CompositeView.prototype.addChild.apply(this, arguments);
}
},
onRender: function () { onRender: function () {
this.update(); this.update();
}, },
...@@ -121,14 +133,4 @@ TodoMVC.module('TodoList.Views', function (Views, App, Backbone, Marionette, $) ...@@ -121,14 +133,4 @@ TodoMVC.module('TodoList.Views', function (Views, App, Backbone, Marionette, $)
}); });
} }
}); });
// Application Event Handlers
// --------------------------
//
// Handler for filtering the list of items by showing and
// hiding through the use of various CSS classes
App.vent.on('todoList:filter', function (filter) {
filter = filter || 'all';
$('#todoapp').attr('class', 'filter-' + filter);
});
}); });
...@@ -53,7 +53,8 @@ TodoMVC.module('TodoList', function (TodoList, App, Backbone, Marionette, $, _) ...@@ -53,7 +53,8 @@ TodoMVC.module('TodoList', function (TodoList, App, Backbone, Marionette, $, _)
// Set the filter to show complete or all items // Set the filter to show complete or all items
filterItems: function (filter) { filterItems: function (filter) {
App.vent.trigger('todoList:filter', (filter && filter.trim()) || ''); var newFilter = filter && filter.trim() || 'all';
App.request('filterState').set('filter', newFilter);
} }
}); });
......
...@@ -23,6 +23,18 @@ TodoMVC.module('Todos', function (Todos, App, Backbone) { ...@@ -23,6 +23,18 @@ TodoMVC.module('Todos', function (Todos, App, Backbone) {
isCompleted: function () { isCompleted: function () {
return this.get('completed'); return this.get('completed');
},
matchesFilter: function (filter) {
if (filter == 'all') {
return true;
}
if (filter == 'active') {
return !this.isCompleted();
}
return this.isCompleted();
} }
}); });
......
...@@ -5,12 +5,22 @@ ...@@ -5,12 +5,22 @@
// and functional testing. // and functional testing.
window.TodoMVC = new Backbone.Marionette.Application(); window.TodoMVC = new Backbone.Marionette.Application();
(function () {
var filterState = new Backbone.Model({
filter: 'all'
});
TodoMVC.reqres.setHandler('filterState', function () {
return filterState;
});
})();
TodoMVC.addRegions({ TodoMVC.addRegions({
header: '#header', header: '#header',
main: '#main', main: '#main',
footer: '#footer' footer: '#footer'
}); });
TodoMVC.on('initialize:after', function () { TodoMVC.on('start', function () {
Backbone.history.start(); Backbone.history.start();
}); });
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment