Sahasrangshu Guha
Sahasrangshu Guha

Reputation: 683

get of Backbone Model is sowing undefined data

I have created a backbone model as follows

var note_model = Backbone.Model.extend({
    default    : {
        HistoryKey  : "",
        InsertDate  : "",
        MemberKey   : "",
        NoteDate    : "",
        ContactNote : "",
        UserId      : ""
    },
    initialize : function(note) { 
        this.HistoryKey  = note.historykey;
        this.InsertDate  = note.insertdateUTC;
        this.MemberKey   = note.memberkey;
        this.NoteDate    = note.notedateUTC;
        this.ContactNote = note.contactnote;
        this.UserId      = note.userid; 
        console.log(this.get('HistoryKey'));
    }
});

Now i have created a collection where a url is defined and i populated the model by using the fetch method of collection.Now after populating the model whenever i'm accessing the model data using

model_object.HistoryKey 

I'm getting the data but when i'm trying to use

model_object.get("HistoryKey")

I'm getting undefined value. The structure of the JSON data is something like this

    {
        "historykey": 4,
        "insertdateUTC": "2013-11-15T23:21:44.247",

    }

However if i use

model_object.get("historykey")

I'm getting proper data. My question is why i'm not getting data using member.get("HistoryKey").

Upvotes: 0

Views: 46

Answers (2)

Jonathan Naguin
Jonathan Naguin

Reputation: 14766

You should use set to change the attributes of your model:

this.set('HistoryKey', note.historykey);
this.set('InsertDate', note.insertdateUTC);
this.set('MemberKey', note.memberkey);
this.set('NoteDate', note.notedateUTC);
this.set('ContactNote', note.contactnote);
this.set('UserId', note.userid); 

And get to access to the properties.

Upvotes: 0

Phortuin
Phortuin

Reputation: 770

You should use this.set("HistoryKey", note.historykey) in the initialize method. What you are doing is setting a property on the object, but what you want to do is set an attribute on the Backbone model. If in your example you'd write console.log(this.HistoryKey) in the initialize method, you would get the value you are looking for. Read this: http://backbonejs.org/#Model-set

Upvotes: 1

Related Questions