Created
March 23, 2015 19:29
-
-
Save majornorth/20a8112de2a9d5ca16f7 to your computer and use it in GitHub Desktop.
Creating associations between lists and todos in Backbone.js
This file contains 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
var TodoLists = Backbone.Collection.extend({ | |
model: TodoList | |
}); | |
var TodoList = Backbone.Model.extend({ | |
defaults: { | |
name: '' | |
}, | |
initialize: function(options) { | |
this.collection = options.collection; | |
} | |
}); | |
var TodoCollection = Backbone.Collection.extend({ | |
model: Todo | |
}); | |
var Todo = Backbone.Model.extend({ | |
defaults: { | |
text: '', | |
finished: false, | |
due_date: null | |
} | |
}); | |
var collection = new TodoCollection([{text: 'item one'}, {text: 'item two'}]); | |
var list = new TodoList({ | |
name: 'My List' | |
collection: collection | |
}); | |
var collection2 = new TodoCollection([{text: 'item three'}, {text: 'item four'}]); | |
var list2 = new TodoList({ | |
name: 'My Other List', | |
collection: collection2 | |
}); | |
var lists = new TodoLists([list, list2]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment