Fetching data from a server with a foundation and creating a model and collection

I have a url http://anexampleproject/api/players which returns a list of players in json format.

How can I create a model, its collection and warning name in the console.

json return URL example:

  [ { "id": 1, "name": "Lily", "age": 14, "city": New York, }, { "id": 2, "name": "BIlly", "age": 14, "city": New York, } ] 
+4
source share
1 answer
 var data = [{ "id": 1, "name": "Lily", "age": 14, "city": "New York" }, { "id": 2, "name": "BIlly", "age": 14, "city": "New York" }]; var MyModel = Backbone.Model.extend({ defaults: { "id": "", "name": "", "age": 0, "city": "" } }); var MyCollection = Backbone.Collection.extend({ model: MyModel }); var myCollection = new MyCollection(data); 

EDIT:

Using url

 var MyCollection = Backbone.Collection.extend({ url: "http://anexampleproject/api/players", model: MyModel }); var myCollection = new MyCollection(); myCollection.fetch({ success: function(){ }, error: function(){ } }); 
+8
source

Source: https://habr.com/ru/post/1469174/


All Articles