1

I have a local json file. I need to put these data in a Backbone.Collection, then receive this collection in a Backbone.View and render the data in a template. So I tried many ways but...

Model:

define([
    'backbone',
], function(Backbone) {
    var InfoModel = Backbone.Model.extend({
        defaults: {
            id               : "",
            name             : "",
            info             : "",
            img              : "",
            user             : "",
            userlvl          : "",
            mobile           : "",
            phone            : "",
            fax              : "",
            web              : "",
            email            : "",
            linkedin         : "",
            instagram        : "",
            twitter          : "",
            storeService     : "",
            departomanService: "",
            shopBalence      : "",
            shopWithdrawal   : "",
            shopDeposit      : ""
        } 
    }); 
    return InfoModel;
});

Collection:

define([
    'backbone',
    'core/application',
    'components/persian_social_tinet/models/information',
], function(Backbone, Application,InfoModel) {
    var InfoCollection = Backbone.Collection.extend({
        model : new InfoModel(), 

        url   : function(){
            return 'json!data/' + Application.current_section + '/information.json';
        },

        parse: function(Response){
            return Response;
        }
    });
    return InfoCollection;
});

View:

define([
    'backbone',
    'components/persian_social_tinet/collections/information',
    'text!components/persian_social_tinet/tpls/leftpanel-acc.html',
], function(Backbone, InfoColl, accTpl ) {
    var accView = Backbone.View.extend({
        className: 'account-leftPanel',
        
        initialize: function() {
            var infoColl = new InfoColl();
            this.listenTo(InfoColl, "sync", this.render) 
        },
       
        render: function() {
            this.template = _.template(accTpl);  
            var item = { infoColl: this.infoColl.toJSON }
            this.$el.html(this.template(item));
            return this;
        }
          
    });
    return accView;
});

I want to fetch the collection. However, the json data are not from a server but from the local filesystem. Please help me.

8
  • Where is the problem, is the request actually successful?
    – T J
    Commented Jul 8, 2020 at 15:52
  • I can't get json data from collection
    – ALi Hz
    Commented Jul 9, 2020 at 6:23
  • 1
    Model should be like this: model: InfoModel, apart from that, is the request successfull in network tab? is the parse method of collection being invoked?
    – T J
    Commented Jul 9, 2020 at 10:08
  • I want to get values from the Json file and display them in a template. But when defining a collection, it does not show any value for me to fetch. But Json information is displayed on the network
    – ALi Hz
    Commented Jul 12, 2020 at 12:44
  • Model and collection files are separate, not in one file
    – ALi Hz
    Commented Jul 12, 2020 at 12:46

0