Note that there are some explanatory texts on larger screens.

plurals
  1. POBackbone can create objects, but not fetch them
    primarykey
    data
    text
    <p>I'm trying out Backbone.js for the first time. I'm not new to Django and so I'm trying to build a Backbone-powered frontend to a Django app. After some research, I decided that Tastypie is probably the easiest to go. </p> <p>After much wrangling, I have this problem: I can create objects just fine, but I can't fetch any of them. </p> <p>So I can do this:</p> <pre><code>var foo = new LabSite() var newInfo={name:"Mine",site_code:"FRED"} foo.save(newInfo) </code></pre> <p>And I can see a new LabSite in the admin named "Mine" with an ID of 1.</p> <p>When I try this:</p> <pre><code>var bar = new LabSite({id:1}) bar.fetch({ success: function (bar) { alert(bar.toJSON()); } }) </code></pre> <p>I don't see the alert.</p> <p>On the object that is turn back it has this as the response:</p> <pre><code>responseText: "callback({"id": "1", "name": "Mine", "resource_uri": "/api/v1/labsite/1/","site_code": "FRED"})" </code></pre> <p>So it was returned, the value isn't there. Maybe something doesn't understand the response?</p> <p>I tried using this backbone-tastypie project, but it didn't change anything: <a href="https://github.com/amccloud/backbone-tastypie" rel="nofollow">https://github.com/amccloud/backbone-tastypie</a></p> <p>My ModelResource is quite simple:</p> <pre><code>class LabSiteResource(ModelResource): def determine_format(self, request): return 'text/javascript' class Meta: queryset = LabSite.objects.all() authorization= Authorization() </code></pre> <p>And if I go to <code>http://localhost:8000/api/v1/labsite/1/</code> I see:</p> <pre><code> callback({"id": "1", "name": "Mine", "resource_uri": "/api/v1/labsite/1/", "site_code": "FRED"}) </code></pre> <p>Any idea why I can't fetch anything?</p> <p><em>EDIT</em> Based on a comment below, I tried to catch the error.</p> <pre><code> var errorInfo = {}; var bar = new LabSite({id:1}) bar.fetch({ success: function (bar) { alert(bar.toJSON()); }, error: function(a1,a2,a3) { errorInfo=[a1,a2,a3] } }); </code></pre> <p><code>errorInfo</code> didn't provide a lot of helpful information -- this is what it was:</p> <pre><code> &gt; errorInfo[0] d _escapedAttributes: Object _pending: Object _previousAttributes: Object _silent: Object attributes: Object changed: Object cid: "c1" id: 1 __proto__: x &gt; errorInfo[1] [...] readyState: 4 responseText: "callback({"id": "1", "name": "Mine", "resource_uri": "/api/v1/labsite/1/", "site_code": "FRED"})" setRequestHeader: function (a,b){if(!v){var c=a.toLowerCase();a=u[c]=u[c]||a,t[a]=b}return this} state: function (){return c} status: 200 statusCode: function (a){if(a){var b;if(v&lt;2)for(b in a)r[b]=[r[b],a[b]];else b=a[x.status],x.always(b)}return this} statusText: "OK" [....] &gt; errorInfo[2] Object error: function (d,e){e=d===b?e:d;a?a(b,e,c):b.trigger("error",b,e,c)} success: function (d,e,f){if(!b.set(b.parse(d,f),a))return!1;c&amp;&amp;c(b,d)} __proto__: Object </code></pre> <p>The <code>errorInfo[1].status</code> seems to indicate the the communication was perfectly find and <code>errorInfo[1].responseText</code> is the correct data. I may be more confused now than before.</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.
    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