Note that there are some explanatory texts on larger screens.

plurals
  1. POIntegrating JSON feed with Backbone JS
    primarykey
    data
    text
    <p>I'm currently working on a project where I type a keyword inside an input box and when I click send it hits a PHP server with a link like (localhost/json-status.php?query=<strong>input text</strong>) and it returns whatever is after "query=" in json format. Now I've accomplished this with jQuery and I'm trying to do this again in backbone js.</p> <pre><code>$("#updateStatus").click(function(){ var query = $("#statusBar").val(); var url = "json-status.php" + "?query=" + query; $.getJSON(url,function(json){ $.each(json.posts,function(i,post){ $("#content").append( '&lt;div&gt;'+ '&lt;p&gt;'+post.status+'&lt;/p&gt;'+ '&lt;/div&gt;' ); }); }); }); </code></pre> <p>I've pretty much ported over what I did in jQuery over to backbone js and it's not working out as expected so far, please let me know if my approach is correct and how I can solve my problem.</p> <p>backbone code:</p> <pre><code>(function ($) { Status = Backbone.Model.extend({ status: null }); StatusList = Backbone.Collection.extend({ initialize: function (models, options) { this.bind("add", options.view.addStatusList); } }); AppView = Backbone.View.extend({ el: $("body"), initialize: function () { this.status = new StatusList( null, { view: this }); }, events: { "click #updateStatus": "getStatus", }, getStatus: function () { var url = "json-status.php" + "?query=" + $("#statusBar").val(); var statusModel; $.getJSON(url,function(json){ $.each(json.posts,function(i,post){ statusModel = new Status({ status: post.status }); this.status.add( statusModel ); }); }); }, addStatusList: function (model) { $("#status").prepend("&lt;div&gt;" + model.get('status') + "&lt;/div&gt;"); } }); var appview = new AppView; })(jQuery); </code></pre> <p>PHP server code which returns in json format (this works fine):</p> <pre><code>&lt;?php $getQuery = $HTTP_GET_VARS["query"]; $json=' {"posts":[ { "status": "' . $getQuery . '" } ]} '; echo $json; ?&gt; </code></pre> <p>And if you wish to copy/paste what I have so far it's:</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;JSON Test&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;input value="What's on your mind?" id="statusBar" /&gt;&lt;button id="updateStatus"&gt;Update Status&lt;/button&gt; &lt;div id="content"&gt; &lt;/div&gt; &lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"&gt;&lt;/script&gt; &lt;script src="http://ajax.cdnjs.com/ajax/libs/underscore.js/1.1.4/underscore-min.js"&gt;&lt;/script&gt; &lt;script src="http://ajax.cdnjs.com/ajax/libs/backbone.js/0.3.3/backbone-min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; $("#statusBar").click(function() { $(this).val(""); }); (function ($) { Status = Backbone.Model.extend({ status: null }); StatusList = Backbone.Collection.extend({ initialize: function (models, options) { this.bind("add", options.view.addStatusList); } }); AppView = Backbone.View.extend({ el: $("body"), initialize: function () { this.status = new StatusList( null, { view: this }); }, events: { "click #updateStatus": "getStatus", }, getStatus: function () { var url = "json-status.php" + "?query=" + $("#statusBar").val(); var statusModel; $.getJSON(url,function(json){ $.each(json.posts,function(i,post){ statusModel = new Status({ status: post.status }); this.status.add( statusModel ); }); }); }, addStatusList: function (model) { $("#status").prepend("&lt;div&gt;" + model.get('status') + "&lt;/div&gt;"); } }); var appview = new AppView; })(jQuery); &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Thank you for your time.</p> <hr> <p>Julien's code.</p> <pre><code>StatusList = Backbone.Collection.extend({ model: Status, value: null, url: function(){ return "json-status.php?query=" + this.value;} }); AppView = Backbone.View.extend({ el: $("body"), initialize: function () { _.bindAll(this, "render");// to solve the this issue this.status = new StatusList( null, { view: this }); this.status.bind("refresh", this.render); }, events: { "click #updateStatus" :"getStatus", }, getStatus: function () { this.status.value = $("#statusBar").val(); this.status.fetch(this.status.value); }, render: function () { var statusEl = $("#status"); this.status.each( function(model) { statusEl.prepend("&lt;div&gt;" + model.get('status') + "&lt;/div&gt;"); }); } }); var appview = new AppView; </code></pre> <p>Full HTML (part 2):</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;JSON Test&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;input value="What's on your mind?" id="statusBar" /&gt; &lt;button id="updateStatus"&gt;Update Status&lt;/button&gt; &lt;div id="status"&gt; &lt;/div&gt; &lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"&gt;&lt;/script&gt; &lt;script src="http://ajax.cdnjs.com/ajax/libs/underscore.js/1.1.4/underscore-min.js"&gt;&lt;/script&gt; &lt;script src="http://ajax.cdnjs.com/ajax/libs/backbone.js/0.3.3/backbone-min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; $("#statusBar").click(function() { $(this).val(""); }); Status = Backbone.Model.extend(); StatusList = Backbone.Collection.extend({ model: Status, value: null, url: function(){ return "json-status.php?query=" + this.value;} }); AppView = Backbone.View.extend({ el: $("body"), initialize: function () { _.bindAll(this, "render");// to solve the this issue this.status = new StatusList( null, { view: this }); this.status.bind("refresh", this.render); }, events: { "click #updateStatus" :"getStatus", }, getStatus: function () { this.status.value = $("#statusBar").val(); this.status.fetch(this.status.value); }, render: function () { var statusEl = $("#status"); this.status.each( function(model) { statusEl.prepend("&lt;div&gt;" + model.get('status') + "&lt;/div&gt;"); }); } }); var appview = new AppView; &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>And the PHP is still the same from the one originally documented.</p>
    singulars
    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.
 

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