Skip to content

Instantly share code, notes, and snippets.

@majornorth
Created March 23, 2015 19:29
Show Gist options
  • Save majornorth/20a8112de2a9d5ca16f7 to your computer and use it in GitHub Desktop.
Save majornorth/20a8112de2a9d5ca16f7 to your computer and use it in GitHub Desktop.
Creating associations between lists and todos in Backbone.js
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