3gwebtrain
3gwebtrain

Reputation: 15293

Backbone - get method not returning the out put

i am new to Backbone.js, i have a data set, assigned to view, but i can't get the data, while i console that any one tell me what is wrong with my code?

code:

var model = Backbone.Model.extend({
    data:[
        {text:'Google',href:"http://www.google.com"},
        {text:'Yahoo',href:"http://www.yahoo.com"},
        {text:'Bing',href:"http://www.bing.com"}
    ]
});

var View = Backbone.View.extend({
    initialize:function(){
       this.render();
    },
    render:function(){
         var data = this.model.get('data');
        console.log(data);    
    }
});

var myView = new View({model:model});

the error i am getting is here:

TypeError: this.model.get is not a function
[Break On This Error]   

var data = this.model.get('data');

Upvotes: 0

Views: 1191

Answers (1)

McGarnagle
McGarnagle

Reputation: 102743

You need to actually instantiate the model; using extend just creates an extension of the Backbone.Model prototype. The following creates a new instance of Backbone.Model, and initializes it with the data:

var model = new Backbone.Model({
    data:[
        {text:'Google',href:"http://www.google.com"},
        {text:'Yahoo',href:"http://www.yahoo.com"},
        {text:'Bing',href:"http://www.bing.com"}
    ]
});
var myView = new View({ model: model });

If you want to put the data in the type of the model itself, then you could use the defaults property:

var Model = Backbone.Model.extend({
    defaults: {
        data:[
            {text:'Google',href:"http://www.google.com"},
            {text:'Yahoo',href:"http://www.yahoo.com"},
            {text:'Bing',href:"http://www.bing.com"}
        ]
    }
});
var model = new Model();
var myView = new View({ model: model });

Upvotes: 3

Related Questions