nietsnegttiw
nietsnegttiw

Reputation: 371

Adding values to individual attributes of Backbone Model

Suppose I have a JSON file which contains a number of attributes like this:

json.type[1].next_type[1].attribute

And I want to change a specific value of a backbone model which is to contain these attributes.

var M = Backbone.Model.extend({
defaults:{
    attr1: null,
    attr2: null,
    attr3: null }

How would I make attr1 = json.type[1].new_type[2].attr1 ?

Thanks

Upvotes: 2

Views: 120

Answers (2)

Tholle
Tholle

Reputation: 112777

var m = new M({ attr1: json.type[1].new_type[2].attr1 });

Upvotes: 3

asloan
asloan

Reputation: 123

If you have the json file when the model is constructed, you could do

var m = new M({'attr1': json.type[1].new_type[2].attr1})

otherwise

var m = new M()
m.set({'attr1': json.type[1].new_type[2].attr1});

should work

Upvotes: 3

Related Questions