fetch model from json - backbone.js -
good morning, have simple json file , need fetch data (i read thousand of other question not find solution app). have view, model json file. can fetch data can't display them. here code:
calendardetails.json
{ "calendartitle": "webcal", "calendardescription": "hello, i'm new calendar" } headermodel.js
var headermodel = backbone.model.extend({ isedit: false, url: '/assets/calendardetail.json' }); headermodel = new headermodel(); headermodel.fetch(); headerview.js
var headerview = backbone.view.extend({ template: handlebarstemplates["header"], model: headermodel, initialize: function(){ this.render(); this.model.on('change', this.render, this); }, render: function(){ this.$el.html(this.template(this.model.tojson())); return this; } }); app.js
$(function() { var calendarapp = backbone.view.extend({ el: $('#wrap'), initialize: function(){ this.headermodel = new headermodel(); this.headerview = new headerview({ model: this.headermodel, el: $('header') }), this.monthview = new monthview({ el: $("#calendarview") }) } }); var calendar = new calendarapp; }); i'm working rubyonrails , handelbars
any appreciated!!
you have fetch model :
$(function() { var calendarapp = backbone.view.extend({ el: $('#wrap'), initialize: function(){ this.headermodel = new headermodel(); this.headerview = new headerview({ model: this.headermodel, el: $('header') }); // semicolon here this.headermodel.fetch(); // here this.monthview = new monthview({ el: $("#calendarview") }); // semicolon here } }); var calendar = new calendarapp; }); your problem in headermodel.js instantiating model , fetching it, model populated json file , that's it.
in headerview.js affecting model view class, nothing more, view initialize , render functions not yet called.
in app.js when instantiate view
this.headerview = new headerview({ model: this.headermodel, el: $('header') }) your overriding set model new 1 this.headermodel when initialize , render executed new model this.headermodel empty.
Comments
Post a Comment