Last active
December 29, 2015 01:09
-
-
Save OfTheDelmer/7591126 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| _ = require "lodash" | |
| {Model, Collection} = require "backbone" | |
| #Creating a simple todo collection | |
| class Todo extends Model | |
| defaults: | |
| title: "", | |
| completed: false | |
| class TodoCol extends Collection | |
| model: Todo | |
| myTodo = new Todo {title: "Show students a model/collection", id: 2} | |
| todos = new TodoCol [myTodo] | |
| console.log "Collections size: #{todos.length}" | |
| console.log "my collection looks like this ", todos.toJSON() | |
| a = new Todo title: "give my lecture talk" | |
| b = new Todo title: "listen to lecture" | |
| c = new Todo title: "Hey there y'all" | |
| todos = new TodoCol [a,b] | |
| console.log "my new todo list", todos.toJSON() | |
| todos.add c | |
| console.log "My new item added to todos ", todos.toJSON() | |
| todos.remove c | |
| console.log "My new item ripped away from his collection friends", todos.toJSON() | |
| items = new Collection | |
| items.add( [{id: 1, name: "Dog", age: 3}, | |
| {id: 2, name: "Cat", age: 35}]) | |
| console.log "My items are: ", items.toJSON() | |
| items.add( {id: 1, name: "Bear", age: 3}) | |
| console.log "Changed items are ", items.toJSON() | |
| # Retrieve Models from a collection | |
| anotherTodo = new Todo {title: "Show off retrieving models", id:2} | |
| todos = new TodoCol [anotherTodo] | |
| todoTwo = todos.get(2) | |
| console.log "Todo2: ", todoTwo.toJSON() | |
| console.log todoTwo == anotherTodo | |
| MoreTodos = new Collection() | |
| MoreTodos.on "add", (todo)-> | |
| console.log "I should #{todo.get('title')}. I've #{if todo.get('completed') then 'totally' else 'never'} done that before" | |
| MoreTodos.add({title: "do something awesome!!", completed: false}) | |
| console.log MoreTodos | |
| MoreTodos.add([{title: "do something awesome!!", completed: false},{title: "finish lecture", completed: false},{title: "do nothing", completed: true}]) | |
| console.log MoreTodos.toJSON() | |
| # Reseting and Refreshing Collections!! (ooOOhh) | |
| OtherTodos = new Collection() | |
| OtherTodos.add([ | |
| {id: 1, title: "Go To Dinner", completed: false}, | |
| {id: 2, title: "Go To Bed", completed: false}, | |
| {id: 3, title: "Wake Up", completed: true} | |
| ]) | |
| OtherTodos.on "add", (model)-> | |
| console.log "My todo #{model.get('title')}" | |
| OtherTodos.on "remove", (model)-> | |
| console.log "I removed #{model.get('title')}" | |
| OtherTodos.on "change:completed", (model)-> | |
| console.log "I changed completed #{model.get('title')}" | |
| #OtherTodos.set( [{id: 1, title: "Go To Dinner", completed: true}, {id: 2, title: "Blah"}, {id: 4, title: "Work", completed: true}]) | |
| #console.log OtherTodos.toJSON() | |
| # TO RESET everything | |
| OtherTodos.on "reset", -> | |
| console.log "I was completely reset!!!!" | |
| OtherTodos.reset([{id: 1, title: "Go To Dinner", completed: true}]) | |
| console.log OtherTodos.toJSON() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment