我有点坚持尝试获取 Backbone 集合,我是 Backbone 的新手,我确信我错过了一些简单的东西,但不知道是什么。
有什么想法吗?
$ ->
User = Backbone.Model.extend(
urlRoot: "/users"
)
user = new User()
View = Backbone.View.extend(
tagName: "li"
el: "ul"
id: "#users"
template: _.template("<li>...just a code...<span class='buttons'><button class='info'>Info</button><button class='delete'>Delete</button></span></li>")
render: ->
@$el.html(@template())
)
view = new View(
model: user
)
view.render().el
Users = Backbone.Collection.extend(
model: User
url: "/users"
parse: (resp, xhr) ->
return resp.toJSON()
)
usersCollection = new Users()
usersCollection.fetch(
success: (response) ->
console.log 'success'
console.log usersCollection
console.log response
error: (data, response) ->
console.log(response)
)
我的 "/users/ 返回这个数组:
[{"created_at":"2013-05-25T18:00:43Z","email":"user1@mail.ru","id":1,"updated_at":"2013-05-25T18:00:43Z"},{"created_at":"2013-05-25T18:00:43Z","email":"user2@mail.ru","id":2,"updated_at":"2013-05-25T18:00:43Z"},{"created_at":"2013-05-25T18:00:43Z","email":"user3@mail.ru","id":3,"updated_at":"2013-05-25T18:00:43Z"},{"created_at":"2013-05-25T18:00:43Z","email":"user4@mail.ru","id":4,"updated_at":"2013-05-25T18:00:43Z"},{"created_at":"2013-05-25T18:00:43Z","email":"user5@mail.ru","id":5,"updated_at":"2013-05-25T18:00:43Z"},{"created_at":"2013-05-25T18:00:43Z","email":"user6@mail.ru","id":6,"updated_at":"2013-05-25T18:00:43Z"},{"created_at":"2013-05-25T18:00:43Z","email":"user7@mail.ru","id":7,"updated_at":"2013-05-25T18:00:43Z"}]
控制器(Ruby on Rails)看起来像这样:
def index
users = User.all
render json: users, status: 200
end
在 console.log 我有:
success
child {length: 1, models: Array[1], _byId: Object, constructor: function, model: function…}