javascript - Backbone collection working with Rest not working -
when console.log(this.collection.fetch()) can see in responcejson , responsetext it's returned correct data api.
however, readystate remain 1 , when run
this.collection.fetch({ success : function() { console.log(that.collection.tojson()) } });
it returns blank array though no sever has responded data.
here's code...
model
define(['backbone'], function(backbone){ var google_place = backbone.model.extend({ defaults : { 'title' : 'no titile', 'sub_title' : 'no subtitle', 'content' : 'no content available', 'lat' : 0, 'lng' : 0, 'text' : 'no text' } }); return google_place; });
collection
define(['../model/google_place'], function(google_place){ var google_places = backbone.collection.extend({ model: google_places, initialize: function(models, options) { }, url: function() { return "http://globallcoach-app.dev/api/"; }, parse: function(data) { return data.items; }, }); return google_places; });
view
define(includes(), function(backbone, mustache){ var index = backbone.view.extend({ initialize : function() { this.template = mustache.render(nav); }, render : function() { var = this; console.log(that.collection.fetch()); this.collection.fetch({ success : function() { console.log(that.collection.tojson()) }, }); return; }, }); return index; });
json response fetch (responsetext)
[{ "title": "no titile", "sub_title": "no subtitle", "content": "no content available", "lat": "0", "lng": "0", "text": "no text" }, { "title": "example title", "sub_title": "example subtitle", "content": "loads on content here", "lat": "0", "lng": "0", "text": "random text bullshit" }]
your collections parse
method contains following:
parse: function(data) { return data.items; },
but response doesn't have items
property:
[{ "title": "no titile", "sub_title": "no subtitle", "content": "no content available", "lat": "0", "lng": "0", "text": "no text" }, { "title": "example title", "sub_title": "example subtitle", "content": "loads on content here", "lat": "0", "lng": "0", "text": "random text bullshit" }]
hence parse
returns undefined, collection.tojson()
returns empty array
either should remove parse
method or response should be
{ "items": [{ "title": "no titile", "sub_title": "no subtitle", "content": "no content available", "lat": "0", "lng": "0", "text": "no text" }, { "title": "example title", "sub_title": "example subtitle", "content": "loads on content here", "lat": "0", "lng": "0", "text": "random text bullshit" }] }
also collection doesn't seem importing backbone
. make sure has access backbone
, isn't throwing error
Comments
Post a Comment