最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - fetching data from server with backbone and creating model and collection - Stack Overflow

programmeradmin1浏览0评论

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

How can i create model and collection of it and alert name in console.

example of url returned json:

 [
       {
          "id": 1,
          "name": "Lily",
          "age": 14,
          "city": New York,
       },
       {
         "id": 2,
         "name": "BIlly",
          "age": 14,
          "city": New York,
      }
    ]

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

How can i create model and collection of it and alert name in console.

example of url returned json:

 [
       {
          "id": 1,
          "name": "Lily",
          "age": 14,
          "city": New York,
       },
       {
         "id": 2,
         "name": "BIlly",
          "age": 14,
          "city": New York,
      }
    ]
Share Improve this question edited Mar 14, 2013 at 9:02 Backlin 14.9k4 gold badges52 silver badges81 bronze badges asked Mar 14, 2013 at 8:57 LasangLasang 1,3796 gold badges24 silver badges44 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8
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(){

    }
});
发布评论

评论列表(0)

  1. 暂无评论