alex cintea
alex cintea

Reputation: 13

Error on the backbone model

I've just started to learn Backbone and I have a issue with the model. I have created the object but when I am trying to get the attributes from the created object what I get from the console is undefined. Could You please tell me why?

Person = Backbone.Model.extend({

defaults: {

    name: 'Fetus',

    age: 0,

    child: ''

},

initialize: function() {

    alert("Welcome to this world");

}

  });

and this is from the console:

var person = new Person({ name: "tom", age: 15, child: "jerry" });
undefined 

var name = person.get("name");
undefined

why I don't get the name "tom" here???

Thanks

Upvotes: 0

Views: 42

Answers (2)

homtg
homtg

Reputation: 1999

I guess this is just a misunderstanding of the console outputs, you enter this in your console:

var name = person.get("name");

And you intend to see the value of your name variable as the console output, but the console output after declaring a variable is not the variables value, so you could do:

person.get("name");

or

var name = person.get("name");
name //outputs the value of name variable in your console

in your console and you will see that your code works fine. ;)

Upvotes: 1

James Lai
James Lai

Reputation: 2071

When I loaded your code in a fiddle, it actually works. Are you sure this is not a race condition problem, and the code which creates Person is being run before you attempt to use it?

Upvotes: 0

Related Questions