Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to fetch data from external json file into a backbone view
    primarykey
    data
    text
    <p>I am trying to render a backbone view by accessing a list which is populated from a json using jquery-ajax.</p> <p>The problem i am facing is that the page gets rendered even before the ajax call is completed. I am not sure whether i can include the backbone model/view inside the success data inorder to make it work after fetching the data. If i replace the ajax call and write static json into a javascript variable then this will work fine. But i am not able to get this to work with an external call. </p> <p>The method i am using maybe incorrect and I would like a proper way to do this using backbone.</p> <p>Code is as follows : </p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;script class="jsbin" src="http://code.jquery.com/jquery-1.7.1.min.js"&gt;&lt;/script&gt; &lt;script class="jsbin" src="http://documentcloud.github.com/underscore/underscore-min.js"&gt;&lt;/script&gt; &lt;script class="jsbin" src="http://documentcloud.github.com/backbone/backbone.js"&gt;&lt;/script&gt; &lt;script&gt; var stages; $.ajax({ type: 'GET', url: 'test.php', async:'false', data: { get_param: 'value' }, success: function (data) { window.stages = data; alert(stages); } }); alert(stages); // StageModel: no need to extend if you're not adding anything. StageModel = Backbone.Model; // StageCollection StageCollection = Backbone.Collection.extend({ model: StageModel }); // create view for list of stages StageCollectionView = Backbone.View.extend({ el: $("#stageNav"), initialize: function() { this.collection.bind('add', this.createStageListItem, this); }, events: { 'click .stageListItem' : 'selectStage' }, createStageListItem: function(model) { this.$("#stageList").append("&lt;tr&gt;&lt;td id=\"" + model.cid + "\" class='stageListItem'&gt;" + model.get('label') +'&lt;/td&gt;&lt;td&gt;-'+ model.get('message') + "&lt;/td&gt;&lt;/tr&gt;"); }, selectStage: function(event) { var cid = $(event.target).attr('id'); this.trigger('new-stage', this.collection.getByCid(cid)); }, render: function() { var self = this; this.collection.each(function(model) { self.createStageListItem(model); }); return this; } }); // StageView, StageView = Backbone.View.extend({ el: $("#stage"), initialize: function(options) { this.eventSource = options.newStageEventSource; this.eventSource.bind('new-stage', this.loadAndRenderStage, this); }, load: function(model) { this.model = model; return this; }, render: function() { $("#stageLabel").html(this.model.get('label')); $("#stageMsg").html(this.model.get('message')); }, loadAndRenderStage: function(stage) { this.load(stage); this.render(); } }); var stageCollection = new StageCollection(stages); var stageCollectionView = new StageCollectionView({ collection: stageCollection }); var stageView = new StageView({ newStageEventSource: stageCollectionView }); stageCollectionView.render(); &lt;/script&gt; &lt;meta charset=utf-8 /&gt; &lt;title&gt;JS Bin&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="stageNav"&gt; &lt;table id="stageList"&gt; &lt;/table&gt; &lt;/div&gt; &lt;div id="stage"&gt; &lt;h3 id="stageLabel"&gt;&lt;/h3&gt; &lt;p id="stageMsg"&gt;&lt;/p&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Many thanks</p> <p>Roy </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload