Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting static data from a json file into backbone model

I have the following code and was wondering why my data isn't getting pulled into my model? I'm using a static json file and I'm guessing this might be my problem but can't seem to find any documentation about it.

var DataModel = Backbone.Model.extend({
    initialize: function () {
        console.log('initiliazed model')
    },

    url: "data/data.json"

});

var StructureView = Backbone.View.extend ({
    initialize: function () {
        console.log('initiliazed view')
        _.bindAll(this);
        this.model.fetch();
        this.render();
        this.model.on('change',this.render);
    },
    el : '#ev-wrapper',
    render: function () {
        $('#ev-wrapper').empty().append(Handlebars.compile($('#ev-template').html())(this.model.toJSON()));
        $('.ev-asset-loader').fadeOut('slow');
    }

});

var structureView = new StructureView({model: new DataModel()});
like image 751
darylhedley Avatar asked Nov 13 '12 09:11

darylhedley


1 Answers

You need to call fetch. This will issue an AJAX request using url

var model = new DataModel();
model.fetch();

Open Firebug or your favorite browser's network console to see AJAX requests and check if it's OK

like image 83
mexique1 Avatar answered Sep 25 '22 16:09

mexique1