I'm trying to understanding backbone code. I read this http://addyosmani.com/blog/building-spas-jquerys-best-friends/
then I plan to change this
if (this._index === null){ $.ajax({ url: 'data/blog.json', dataType: 'json', data: {}, success: function(data) { ws._data = data; console.log(ws._data); ws._blogs = new BlogCollection(data.rows); console.log(ws._blogs); ws._index = new IndexView({model: ws._blogs}); Backbone.history.loadUrl(); } }); return this; } return this;
use collection fetch
if (this._index === null){ ws._data = new BlogCollection; ws._data.fetch({success: function(data){console.log(data.models[0].attributes);}}); //ws._blogs = new BlogCollection(ws._data.rows); //ws._index = new IndexView({model: ws._blogs}); Backbone.history.loadUrl(); }
with this collection
var BlogCollection = Backbone.Collection.extend({ model: Blog, url : 'data/blog.json', parse: function(response){ return response; }
when I read the response from collection, it's give same value as using jquery ajax. but when I use fetch in controller, why I have to access data.models[0].attributes); to get the same data return.
this is my json
{ "page":"1", "total": "5", "records": "25", "rows": [ { "title": "Classic Pop", "author": "Michael Jackson Collection", "image": "images/1.jpg", "tags": ["xyz", "abc", "def"], "slug" : "classic-pop", "url": "http://www.localhost/news", "intro": "hello test", "content": "hello test, alfjldsajfldjsflja dljfldasjfa jfljdasfl jfldsjf jljdafl jl" }, { "title": "Modern Pop/R&B", "author": "Bruno Mars and Others", "image": "images/54.jpg", "tags": ["test", "test2", "test3"], "slug" : "modern-pop-rb", "url": "http://www.localhost/news", "intro": "hello test 2", "content": "hello test, alfjldsajfldjsflja dljfldasjfa jfljdasfl jfldsjf jljdafl jl" } ] }
how to make fetch works right??