Thursday, May 29, 2014

Re: backbonejs and django

Hi,

On Thu, May 29, 2014 at 3:07 PM, Eddilbert Macharia <edd.cowan@gmail.com> wrote:
> Hi All,
>
> i am using django-tastypie to return django models as json object.
>
> when i run the following code
>
> var org = Backbone.Model.extend({
>
> urlRoot: 'api/orgnanization/',
> });
>
>
> var orgColl=Backbone.Collection.extend({
> model: org,
> urlRoot: 'api/orgnanization/',
> initialize:function(){
> this.fetch();
> console.log(this)
> }
> });
>
> var apiView=Backbone.View.extend({
> el:'.try',
> //template:,
> initialize:function(){
> this.listenTo(this.collection,'reset',this.render)
> },
> //events:{},
> render:function(){
> console.log(this.collection)
> //this.$el.html(this)
> },
> });
>
> var cos=new orgColl()
> new apiView({collection:cos})
>
> i get the following in the chrome console
> r {length: 0, models: Array[0], _byId: Object, constructor: function, model:
> function…}
>
> _byId: Object
> _events: Object
> _listenerId: "l7"
> length: 1
> meta: Object
> models: Array[1]
> __proto__: s
>
>
> but when i look a the network the model is actually fetched from the
> database
>
> meta: {limit:20, next:null, offset:0, previous:null, total_count:3}
>
> limit: 20
> next: null
> offset: 0
> previous: null
> total_count: 3
>
> objects: [{id:1, name:eddmashinc, resource_uri:}, {id:2, name:trive,
> resource_uri:},…]
>
> 0: {id:1, name:eddmashinc, resource_uri:}
>
> id: 1
> name: "eddmashinc"
> resource_uri: ""
>
> 1: {id:2, name:trive, resource_uri:}
>
> id: 2
> name: "trive"
> resource_uri: ""
>
> 2: {id:3, name:mashainc, resource_uri:}
>
> id: 3
> name: "mashainc"
> resource_uri: ""
>
> how do i get this data to display on the backbone view or what im i doing
> wrong ??
>

It seems like the JSON you're getting back from the server puts your
items under a key called 'objects'. A Backbone Collection, by default,
if you just provide it a urlArg, will expect the server to return an
array / list of items.

It does, however, provide a parse method that you can over-ride to
parse the JSON you get from the server and return an array.

So, your Collection definition would look something like:

var orgColl=Backbone.Collection.extend({
model: org,
urlRoot: 'api/orgnanization/',
initialize:function(){
this.fetch();
console.log(this)
},
parse: function(response) {
return response.objects;
}
});

See: http://backbonejs.org/#Collection-parse

All the best,
Sanjay

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAG3W7ZGvWUPtqDRyKNYijexTYLmVOCJCY%3DEgP%3DL3Qycaog72%2Bw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment