Andrey Kuzmin Andrey Kuzmin - 1 year ago 91
Javascript Question

How to set Backbone model's defaults based on collection attribute

I need all new MenuItem models to have menu attribute from parent collection.
Here is a basic example that doesn't work (because this.collection is undefined in MenuItem's defaults function)

var MenuItem, Menu, menu;

MenuItem = Backbone.Model.extend({
defaults: function() {
return {

// Fake Backbone sync
sync: function(method, model, options) {
if(typeof model.cid != 'undefined') {
var cid = model.cid;
model.unset('cid').set({id:cid}, {silent:true});


Menu = Backbone.Collection.extend({
model: MenuItem,
initialize: function(options) { =;

menu = new Menu({name: "footer"});

menu.create({title: "Page", url: "/page"}, {
success: function(model){
console.log(model.get("menu")) // expect to be "footer"

Answer Source

I've managed to fix it by overriding collection's create method, I'm still unsure if this is the right way to go.

create: function(attributes, options) {
    _.extend({menu:}, attributes),
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download