Commit 2b4a9d49 authored by Kevin Malakoff's avatar Kevin Malakoff Committed by Sindre Sorhus

Close GH-165: Updated Knockback to 0.15.1 and Knockout to 2.1.0.

parent b461a23c
...@@ -30,10 +30,7 @@ ...@@ -30,10 +30,7 @@
</ul> </ul>
</section> </section>
<footer id="footer" data-bind="block: todos.tasks_exist"> <footer id="footer" data-bind="block: todos.tasks_exist">
<span id="todo-count"> <span id="todo-count" data-bind="html: footer.remaining_text"></span>
<strong data-bind="text: footer.remaining_count"></strong>
<span data-bind="text: footer.remaining_text"></span>
</span>
<ul id="filters"> <ul id="filters">
<li> <li>
<a href="#/" data-bind="css: {selected: settings.list_filter_mode()==''}">All</a> <a href="#/" data-bind="css: {selected: settings.list_filter_mode()==''}">All</a>
...@@ -63,7 +60,7 @@ ...@@ -63,7 +60,7 @@
<!-- Knockback Dependencies --> <!-- Knockback Dependencies -->
<script src="js/lib/underscore-min.js"></script> <script src="js/lib/underscore-min.js"></script>
<script src="js/lib/backbone-min.js"></script> <script src="js/lib/backbone-min.js"></script>
<script src="js/lib/knockout-2.0.0.js"></script> <script src="js/lib/knockout-2.1.0.js"></script>
<script src="js/lib/knockback.min.js"></script> <script src="js/lib/knockback.min.js"></script>
<!-- More Demo Dependencies --> <!-- More Demo Dependencies -->
<script src="js/lib/backbone.localStorage-min.js"></script> <script src="js/lib/backbone.localStorage-min.js"></script>
...@@ -76,7 +73,6 @@ ...@@ -76,7 +73,6 @@
<script src="js/viewmodels/footer.js"></script> <script src="js/viewmodels/footer.js"></script>
<script src="js/routers/app.js"></script> <script src="js/routers/app.js"></script>
<!-- The Demo --> <!-- The Demo -->
<script src="../../assets/base.js"></script>
<script src="js/app.js"></script> <script src="js/app.js"></script>
</body> </body>
</html> </html>
\ No newline at end of file
// Generated by CoffeeScript 1.3.1
(function() { (function() {
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
$(function() { $(function() {
var todos; var todos;
ko.bindingHandlers.dblclick = { ko.bindingHandlers.dblclick = {
...@@ -20,17 +21,18 @@ ...@@ -20,17 +21,18 @@
}); });
}, },
update: function(element, value_accessor) { update: function(element, value_accessor) {
var _this = this;
ko.utils.unwrapObservable(value_accessor()); ko.utils.unwrapObservable(value_accessor());
return _.defer(__bind(function() { return _.defer(function() {
return ko.bindingHandlers.hasfocus.update(element, value_accessor); return ko.bindingHandlers.hasfocus.update(element, value_accessor);
}, this)); });
} }
}; };
window.app = { window.app = {
viewmodels: {} viewmodels: {}
}; };
app.viewmodels.settings = new SettingsViewModel(); app.viewmodels.settings = new SettingsViewModel();
todos = new TodosCollection(); todos = new TodoCollection();
app.viewmodels.header = new HeaderViewModel(todos); app.viewmodels.header = new HeaderViewModel(todos);
app.viewmodels.todos = new TodosViewModel(todos); app.viewmodels.todos = new TodosViewModel(todos);
app.viewmodels.footer = new FooterViewModel(todos); app.viewmodels.footer = new FooterViewModel(todos);
...@@ -39,4 +41,5 @@ ...@@ -39,4 +41,5 @@
Backbone.history.start(); Backbone.history.start();
return todos.fetch(); return todos.fetch();
}); });
}).call(this); }).call(this);
This diff is collapsed.
// Generated by CoffeeScript 1.3.1
(function() { (function() {
var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) { var __hasProp = {}.hasOwnProperty,
for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; };
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype; window.Todo = (function(_super) {
child.prototype = new ctor;
child.__super__ = parent.prototype; __extends(Todo, _super);
return child;
}; Todo.name = 'Todo';
window.Todo = (function() {
__extends(Todo, Backbone.Model);
function Todo() { function Todo() {
Todo.__super__.constructor.apply(this, arguments); return Todo.__super__.constructor.apply(this, arguments);
} }
Todo.prototype.completed = function(completed) { Todo.prototype.completed = function(completed) {
if (arguments.length === 0) { if (arguments.length === 0) {
return !!this.get('completed'); return !!this.get('completed');
...@@ -20,6 +21,9 @@ ...@@ -20,6 +21,9 @@
completed: completed ? new Date() : null completed: completed ? new Date() : null
}); });
}; };
return Todo; return Todo;
})();
})(Backbone.Model);
}).call(this); }).call(this);
// Generated by CoffeeScript 1.3.1
(function() { (function() {
var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) { var __hasProp = {}.hasOwnProperty,
for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; };
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype; window.TodoCollection = (function(_super) {
child.prototype = new ctor;
child.__super__ = parent.prototype; __extends(TodoCollection, _super);
return child;
}; TodoCollection.name = 'TodoCollection';
window.TodosCollection = (function() {
__extends(TodosCollection, Backbone.Collection); function TodoCollection() {
function TodosCollection() { return TodoCollection.__super__.constructor.apply(this, arguments);
TodosCollection.__super__.constructor.apply(this, arguments);
} }
TodosCollection.prototype.localStorage = new Store('todos-knockback');
TodosCollection.prototype.model = Todo; TodoCollection.prototype.localStorage = new Store('todos-knockback');
TodosCollection.prototype.completedCount = function() {
TodoCollection.prototype.model = Todo;
TodoCollection.prototype.completedCount = function() {
return this.models.reduce((function(prev, cur) { return this.models.reduce((function(prev, cur) {
return prev + (cur.completed() ? 1 : 0); return prev + (cur.completed() ? 1 : 0);
}), 0); }), 0);
}; };
TodosCollection.prototype.remainingCount = function() {
TodoCollection.prototype.remainingCount = function() {
return this.models.length - this.completedCount(); return this.models.length - this.completedCount();
}; };
TodosCollection.prototype.completeAll = function(completed) {
TodoCollection.prototype.completeAll = function(completed) {
return this.each(function(todo) { return this.each(function(todo) {
return todo.completed(completed); return todo.completed(completed);
}); });
}; };
TodosCollection.prototype.destroyCompleted = function() {
TodoCollection.prototype.destroyCompleted = function() {
var completed_tasks, model, _i, _len, _results; var completed_tasks, model, _i, _len, _results;
completed_tasks = this.filter(function(todo) { completed_tasks = this.filter(function(todo) {
return todo.completed(); return todo.completed();
...@@ -39,6 +45,9 @@ ...@@ -39,6 +45,9 @@
} }
return _results; return _results;
}; };
return TodosCollection;
})(); return TodoCollection;
})(Backbone.Collection);
}).call(this); }).call(this);
// Generated by CoffeeScript 1.3.1
(function() { (function() {
var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) { var __hasProp = {}.hasOwnProperty,
for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; };
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype; window.AppRouter = (function(_super) {
child.prototype = new ctor;
child.__super__ = parent.prototype; __extends(AppRouter, _super);
return child;
}; AppRouter.name = 'AppRouter';
window.AppRouter = (function() {
__extends(AppRouter, Backbone.Router);
function AppRouter() { function AppRouter() {
AppRouter.__super__.constructor.apply(this, arguments); return AppRouter.__super__.constructor.apply(this, arguments);
} }
AppRouter.prototype.routes = { AppRouter.prototype.routes = {
"": "all", "": "all",
"active": "active", "active": "active",
"completed": "completed" "completed": "completed"
}; };
AppRouter.prototype.all = function() { AppRouter.prototype.all = function() {
return app.viewmodels.settings.list_filter_mode(''); return app.viewmodels.settings.list_filter_mode('');
}; };
AppRouter.prototype.active = function() { AppRouter.prototype.active = function() {
return app.viewmodels.settings.list_filter_mode('active'); return app.viewmodels.settings.list_filter_mode('active');
}; };
AppRouter.prototype.completed = function() { AppRouter.prototype.completed = function() {
return app.viewmodels.settings.list_filter_mode('completed'); return app.viewmodels.settings.list_filter_mode('completed');
}; };
return AppRouter; return AppRouter;
})();
})(Backbone.Router);
}).call(this); }).call(this);
// Generated by CoffeeScript 1.3.1
(function() { (function() {
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
window.FooterViewModel = function(todos) { window.FooterViewModel = function(todos) {
this.collection_observable = kb.collectionObservable(todos); var _this = this;
this.remaining_count = ko.computed(__bind(function() { this.todos = kb.collectionObservable(todos);
return this.collection_observable.collection().remainingCount(); this.todos.collection().bind('change', function() {
}, this)); return _this.todos.valueHasMutated();
this.remaining_text = ko.computed(__bind(function() { });
return "" + (this.collection_observable.collection().remainingCount() === 1 ? 'item' : 'items') + " left"; this.remaining_text = ko.computed(function() {
}, this)); return "<strong>" + (_this.todos.collection().remainingCount()) + "</strong> " + (_this.todos.collection().remainingCount() === 1 ? 'item' : 'items') + " left";
this.clear_text = ko.computed(__bind(function() { });
this.clear_text = ko.computed(function() {
var count; var count;
count = this.collection_observable.collection().completedCount(); count = _this.todos.collection().completedCount();
if (count) { if (count) {
return "Clear completed (" + count + ")"; return "Clear completed (" + count + ")";
} else { } else {
return ''; return '';
} }
}, this)); });
this.onDestroyCompleted = __bind(function() { this.onDestroyCompleted = function() {
return todos.destroyCompleted(); return todos.destroyCompleted();
}, this); };
return this; return this;
}; };
}).call(this); }).call(this);
// Generated by CoffeeScript 1.3.1
(function() { (function() {
var ENTER_KEY; var ENTER_KEY;
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
ENTER_KEY = 13; ENTER_KEY = 13;
window.HeaderViewModel = function(todos) { window.HeaderViewModel = function(todos) {
var _this = this;
this.title = ko.observable(''); this.title = ko.observable('');
this.onAddTodo = __bind(function(view_model, event) { this.onAddTodo = function(view_model, event) {
if (!$.trim(this.title()) || (event.keyCode !== ENTER_KEY)) { if (!$.trim(_this.title()) || (event.keyCode !== ENTER_KEY)) {
return true; return true;
} }
todos.create({ todos.create({
title: $.trim(this.title()) title: $.trim(_this.title())
}); });
return this.title(''); return _this.title('');
}, this); };
return this; return this;
}; };
}).call(this); }).call(this);
// Generated by CoffeeScript 1.3.1
(function() { (function() {
window.SettingsViewModel = function() { window.SettingsViewModel = function() {
this.list_filter_mode = ko.observable(''); this.list_filter_mode = ko.observable('');
return this; return this;
}; };
}).call(this); }).call(this);
// Generated by CoffeeScript 1.3.1
(function() { (function() {
var TodoViewModel; var TodoViewModel;
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
TodoViewModel = function(model) { TodoViewModel = function(model) {
var _this = this;
this.editing = ko.observable(false); this.editing = ko.observable(false);
this.completed = kb.observable(model, { this.completed = kb.observable(model, {
key: 'completed', key: 'completed',
read: (function() {
return model.completed();
}),
write: (function(completed) { write: (function(completed) {
return model.save({ return model.completed(completed);
completed: completed
});
}) })
}, this); }, this);
this.visible = ko.computed(__bind(function() { this.visible = ko.computed(function() {
switch (app.viewmodels.settings.list_filter_mode()) { switch (app.viewmodels.settings.list_filter_mode()) {
case 'active': case 'active':
return !this.completed(); return !_this.completed();
case 'completed': case 'completed':
return this.completed(); return _this.completed();
default: default:
return true; return true;
} }
}, this)); });
this.title = kb.observable(model, { this.title = kb.observable(model, {
key: 'title', key: 'title',
write: (__bind(function(title) { write: (function(title) {
if ($.trim(title)) { if ($.trim(title)) {
model.save({ model.save({
title: $.trim(title) title: $.trim(title)
...@@ -33,42 +36,47 @@ ...@@ -33,42 +36,47 @@
return model.destroy(); return model.destroy();
}); });
} }
return this.editing(false); return _this.editing(false);
}, this)) })
}, this); }, this);
this.onDestroyTodo = __bind(function() { this.onDestroyTodo = function() {
return model.destroy(); return model.destroy();
}, this); };
this.onCheckEditBegin = __bind(function() { this.onCheckEditBegin = function() {
if (!this.editing() && !this.completed()) { if (!_this.editing() && !_this.completed()) {
this.editing(true); _this.editing(true);
return $('.todo-input').focus(); return $('.todo-input').focus();
} }
}, this); };
this.onCheckEditEnd = __bind(function(view_model, event) { this.onCheckEditEnd = function(view_model, event) {
if ((event.keyCode === 13) || (event.type === 'blur')) { if ((event.keyCode === 13) || (event.type === 'blur')) {
$('.todo-input').blur(); $('.todo-input').blur();
return this.editing(false); return _this.editing(false);
} }
}, this); };
return this; return this;
}; };
window.TodosViewModel = function(todos) { window.TodosViewModel = function(todos) {
this.todos = ko.observableArray([]); var _this = this;
this.collection_observable = kb.collectionObservable(todos, this.todos, { this.todos = kb.collectionObservable(todos, {
view_model: TodoViewModel view_model: TodoViewModel
}); });
this.tasks_exist = ko.computed(__bind(function() { this.todos.collection().bind('change', function() {
return this.collection_observable().length; return _this.todos.valueHasMutated();
}, this)); });
this.tasks_exist = ko.computed(function() {
return _this.todos().length;
});
this.all_completed = ko.computed({ this.all_completed = ko.computed({
read: __bind(function() { read: function() {
return !this.collection_observable.collection().remainingCount(); return !_this.todos.collection().remainingCount();
}, this), },
write: __bind(function(completed) { write: function(completed) {
return this.collection_observable.collection().completeAll(completed); return _this.todos.collection().completeAll(completed);
}, this) }
}); });
return this; return this;
}; };
}).call(this); }).call(this);
...@@ -15,7 +15,7 @@ $ -> ...@@ -15,7 +15,7 @@ $ ->
# Create and bind the app viewmodels # Create and bind the app viewmodels
window.app = {viewmodels: {}} window.app = {viewmodels: {}}
app.viewmodels.settings = new SettingsViewModel() app.viewmodels.settings = new SettingsViewModel()
todos = new TodosCollection() todos = new TodoCollection()
app.viewmodels.header = new HeaderViewModel(todos) app.viewmodels.header = new HeaderViewModel(todos)
app.viewmodels.todos = new TodosViewModel(todos) app.viewmodels.todos = new TodosViewModel(todos)
app.viewmodels.footer = new FooterViewModel(todos) app.viewmodels.footer = new FooterViewModel(todos)
......
class window.TodosCollection extends Backbone.Collection class window.TodoCollection extends Backbone.Collection
localStorage: new Store('todos-knockback') # Save all of the todos under the `"todos-knockback"` namespace. localStorage: new Store('todos-knockback') # Save all of the todos under the `"todos-knockback"` namespace.
model: Todo model: Todo
......
window.FooterViewModel = (todos) -> window.FooterViewModel = (todos) ->
@collection_observable = kb.collectionObservable(todos) @todos = kb.collectionObservable(todos)
@todos.collection().bind('change', => @todos.valueHasMutated()) # get notified of changes to any models
@remaining_count = ko.computed(=> return @collection_observable.collection().remainingCount()) @remaining_text = ko.computed(=> return "<strong>#{@todos.collection().remainingCount()}</strong> #{if @todos.collection().remainingCount() == 1 then 'item' else 'items'} left")
@remaining_text = ko.computed(=> return "#{if @collection_observable.collection().remainingCount() == 1 then 'item' else 'items'} left")
@clear_text = ko.computed(=> @clear_text = ko.computed(=>
count = @collection_observable.collection().completedCount() count = @todos.collection().completedCount()
return if count then "Clear completed (#{count})" else '' return if count then "Clear completed (#{count})" else ''
) )
......
TodoViewModel = (model) -> TodoViewModel = (model) ->
# Task UI state # Task UI state
@editing = ko.observable(false) @editing = ko.observable(false)
@completed = kb.observable(model, {key: 'completed', write: ((completed) -> model.save(completed: completed)) }, @) @completed = kb.observable(model, {key: 'completed', read: (-> return model.completed()), write: ((completed) -> model.completed(completed)) }, @)
@visible = ko.computed(=> @visible = ko.computed(=>
switch app.viewmodels.settings.list_filter_mode() switch app.viewmodels.settings.list_filter_mode()
when 'active' then return not @completed() when 'active' then return not @completed()
...@@ -24,13 +24,13 @@ TodoViewModel = (model) -> ...@@ -24,13 +24,13 @@ TodoViewModel = (model) ->
@ @
window.TodosViewModel = (todos) -> window.TodosViewModel = (todos) ->
@todos = ko.observableArray([]) @todos = kb.collectionObservable(todos, {view_model: TodoViewModel})
@collection_observable = kb.collectionObservable(todos, @todos, view_model: TodoViewModel) @todos.collection().bind('change', => @todos.valueHasMutated()) # get notified of changes to any models
@tasks_exist = ko.computed(=> @collection_observable().length) @tasks_exist = ko.computed(=> @todos().length)
@all_completed = ko.computed( @all_completed = ko.computed(
read: => return not @collection_observable.collection().remainingCount() read: => return not @todos.collection().remainingCount()
write: (completed) => @collection_observable.collection().completeAll(completed) write: (completed) => @todos.collection().completeAll(completed)
) )
@ @
\ No newline at end of file
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