Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add order to schema

  • Loading branch information...
commit 0ab8b2f84e42004c351f16ddf00d1b830edb882f 1 parent 7533fe0
Joshua Stauter authored
View
12 app/assets/javascripts/collections/todos.js.coffee
@@ -1,4 +1,14 @@
class App.Collections.Todos extends Backbone.Collection
model: App.Models.Todo
- url: '/todos'
+ url: '/todos'
+
+ done: -> @filter (todo)-> todo.get 'done'
+
+ remaining: -> @without.apply @, @done()
+
+ nextOrder: ->
+ return 1 if !@.length
+ return @last().get 'order' + 1
+
+ comparator: (todo)-> todo.get 'order'
View
4 app/assets/javascripts/models/todo.js.coffee
@@ -6,9 +6,7 @@ class App.Models.Todo extends Backbone.Model
content: 'empty todo...'
done: false
- initialize: ->
- if !@get 'content'
- @set "content": @defaults.content
+ initialize: -> @set "content": @defaults.content if !@get 'content'
toggle: -> @save done: !@get 'done'
View
54 app/assets/javascripts/todos.js
@@ -1,56 +1,24 @@
$(function(){
- var TodoList = Backbone.Collection.extend({
-
- // Reference to this collection's model.
- model: window.App.Models.Todo,
-
- // Save all of the todo items under the `"todos"` namespace.
- localStorage: new Store("todos-backbone"),
-
- // Filter down the list of all todo items that are finished.
- done: function() {
- return this.filter(function(todo){ return todo.get('done'); });
- },
-
- // Filter down the list to only todo items that are still not finished.
- remaining: function() {
- return this.without.apply(this, this.done());
- },
-
- // We keep the Todos in sequential order, despite being saved by unordered
- // GUID in the database. This generates the next order number for new items.
- nextOrder: function() {
- if (!this.length) return 1;
- return this.last().get('order') + 1;
- },
-
- // Todos are sorted by their original insertion order.
- comparator: function(todo) {
- return todo.get('order');
- }
-
- });
-
// Create our global collection of **Todos**.
- var Todos = new TodoList;
+ var Todos = new window.App.Collections.Todos;
// The DOM element for a todo item...
var TodoView = Backbone.View.extend({
//... is a list tag.
- tagName: "li",
+ tagName: "li",
// Cache the template function for a single item.
template: JST["templates/item"],
// The DOM events specific to an item.
events: {
- "click .check" : "toggleDone",
- "dblclick label.todo-content" : "edit",
- "click span.todo-destroy" : "clear",
- "keypress .todo-input" : "updateOnEnter",
- "blur .todo-input" : "close"
+ "click .check" : "toggleDone",
+ "dblclick label.todo-content": "edit",
+ "click span.todo-destroy" : "clear",
+ "keypress .todo-input" : "updateOnEnter",
+ "blur .todo-input" : "close"
},
// The TodoView listens for changes to its model, re-rendering. Since there's
@@ -110,9 +78,9 @@ $(function(){
// Delegated events for creating new items, and clearing completed ones.
events: {
- "keypress #new-todo": "createOnEnter",
- "keyup #new-todo": "showTooltip",
- "click .todo-clear a": "clearCompleted",
+ "keypress #new-todo" : "createOnEnter",
+ "keyup #new-todo" : "showTooltip",
+ "click .todo-clear a" : "clearCompleted",
"click .mark-all-done": "toggleAllComplete"
},
@@ -129,7 +97,7 @@ $(function(){
Todos.bind('reset', this.addAll);
Todos.bind('all', this.render);
- Todos.fetch();
+ //Todos.fetch();
},
// Re-rendering the App just means refreshing the statistics -- the rest
View
2  app/models/todo.rb
@@ -1,3 +1,3 @@
class Todo < ActiveRecord::Base
- attr_accessible :content, :done
+ attr_accessible :content, :done, :order
end
View
1  db/migrate/20120414043745_create_todos.rb
@@ -3,6 +3,7 @@ def change
create_table :todos do |t|
t.string :content
t.boolean :done
+ t.integer :order
end
end
end
View
1  db/schema.rb
@@ -16,6 +16,7 @@
create_table "todos", :force => true do |t|
t.string "content"
t.boolean "done"
+ t.integer "order"
end
end
View
14 public/index.html
@@ -8,12 +8,14 @@
<script src="assets/jquery.js"></script>
<script src="assets/underscore.js"></script>
<script src="assets/backbone.js"></script>
- <script src="assets/backbone-localstorage.js"></script>
+ <script src="assets/backbone_rails_sync.js"></script>
+ <script src="assets/backbone_datalink.js"></script>
+ <!-- <script src="assets/backbone-localstorage.js"></script> -->
<script>
- window.App = {};
- App.Models = {};
+ window.App = { Models: {}, Collections: {} };
</script>
<script src="assets/models/todo.js"></script>
+ <script src="assets/collections/todos.js"></script>
<script src="assets/todos.js"></script>
</head>
<body>
@@ -44,9 +46,9 @@
</div>
<div id="credits">
- Created by<br />
- <a href="http://jgn.me/">J&eacute;r&ocirc;me Gravel-Niquet</a>.<br />
- Cleanup, edits: <a href="http://addyosmani.com">Addy Osmani</a>.
+ Sincere thanks to:<br />
+ <a href="http://jgn.me/">J&eacute;r&ocirc;me Gravel-Niquet</a>,
+ <a href="http://addyosmani.com">Addy Osmani</a>.
</div>
</body>
Please sign in to comment.
Something went wrong with that request. Please try again.