We are loading the JSON from the file using backbone collection.. Now if i take the same content of the file and put that into an array and call a url function, nothing seems to work.. Can you please help..
var cool = {};
cool.Item = Backbone.Model.extend({});
cool.Items = Backbone.Collection.extend({
model: cool.Item,
url: "data.json"
This code works
var cool = {};
cool.Item = Backbone.Model.extend({});
cool.Items = Backbone.Collection.extend({
model: cool.Item,
url: this.return(cool_cont());
function cool_cont()
{
* JSON object*
}
This code doesnt work..
Your logic in this has some fallacies. The example that works here is that you define the url-attribute as
data.json. What happens when you fetch, is that Backbone makes anajax-request for that url (stumbling upon your file) and gets returned the content there (your json data).Now your second try is where everything goes wrong. First of all:
I don’t know what this does (or more likely does not do) and I don’t want to know. The basic premise of the url-attribute/function is that it is meant to
-Quote from the Backbone.js docs (true story)
So what you do here is that you pass the data you want to populate your collection with as the reference to its location on the server (frankly I think that you’re not even doing that), which is plain wrong.
So url == the path_to_your_data_be_it_url_or_file
url can either be a string (aka a static path) or a function (for example creates the path using some attributes of the model as parts of it).
If you want to insert some faux data to your Collection and you don’t want to do it with an external file, do it like this:
Hope this helps!