Question

I'm new to Backbone.js as well as to JavaScript in general. while playing around a bit, I'll get to error message shown in questions title having these model definitions:

var Incredient = Backbone.Model.extend({
  initialize : console.log("A new Incredient object called " + name + "has been created."),
  defaults : {
    name : '',
    quantity_unit: '',
    need_to_buy : true
  }
});

var Dish = Backbone.Model.extend({
  initialize : console.log("A new Dish object with title " + title + "has been created."),
  defaults : {
    title : '',
    incredients : [],
    instructions : ''
  }
});

Strange thing is, this error appears in line 11, which holds

initialize : console.log("A new Dish object with title " + title + "has been created."),

the variable, which is not defined is 'title'. However, line 2 does not cause any error message in Chrome's JS console. Obviously, there's no such reference error for 'name' despite the otherwise similar declarations. What's the matter with line 11? Any help is much appreciated.

Thanks.

Was it helpful?

Solution

try this:

var Dish = Backbone.Model.extend({
  defaults : {
    title : '',
    incredients : [],
    instructions : ''
  },
  initialize : function(){
      console.log("A new Dish object with title " + this.get('title') + "has been created.")
  }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top