user3122094
user3122094

Reputation: 241

Backbone fetch single model attributes

im trying to fetch a single Models attributes. I use this model as kind of a config file for an app im currently building. But i cant get my head around how to get the attributes in a nice way.

The model looks like this:

WelcomeModel = Backbone.Model.extend({ 

    url: "assets/json/config.json",

    parse: function (response) {
        return response.data;
    }
});

The json looks liks this:

{   
    "data": [{
        "companyName": "lorem ipsum",
        "companyLogo": "loremipsum.png"
    }]
}

And in my view i fetch it like this.

this.model = new WelcomeModel();

this.model.fetch({  
    success: function(model,response) {
        console.log(model);
    },
    error: function() {
        console.log('error')
    }
});

Upvotes: 0

Views: 129

Answers (1)

Evgeniy
Evgeniy

Reputation: 2921

1) parse method returns array instead object. Replace

return response.data

with

return response.data[0];

2) add defaults hash to your WelcomeModel model.

defaults: { 
    companyName: '', 
    companyLogo: '' 
}

Upvotes: 1

Related Questions