1

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.

1 Answer 1

1

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.")
  }
});
Sign up to request clarification or add additional context in comments.

3 Comments

This change now fires: Uncaught TypeError: Object [object global] has no method 'get'
This works fine for me... have you included jquery, backbone? Have you got other resources? @Bunjip
yeah, works fine for me now too. Forgot to put console.log() into a function(). Sorry for delay, though. Thnx mate!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.