我试图在我的收藏中获取数据,但我没有得到任何结果。
这是我的Backbone视图
class Movieseat.Views.MovieseatsIndex extends Backbone.View
template: JST['movieseats/index']
initialize: ->
@collection.on('reset', @render, this)
render: ->
$(@el).html(@template(movies: @collection))
this
My Backbone collection
class Movieseat.Collections.Movieseats extends Backbone.Collection
url: '/api/movies'
My Backbone路线
class Movieseat.Routers.Movieseats extends Backbone.Router
routes:
'': 'index'
initialize: ->
@collection = new Movieseat.Collections.Movieseats()
@collection.fetch()
index: ->
view = new Movieseat.Views.MovieseatsIndex(collection: @collection)
$('#container').html(view.render().el)
我的Backbone模板
<ul id="movie-overview">
<li><%= @movies.length %></li>
</ul>
此长度输出0
当我进入我的控制台并执行
时movies = new Movieseat.Collections.Movieseats
结果
Movieseats {length: 0, models: Array[0], _byId: Object, constructor: function, url: "/api/movies"…}
检查长度
movies.length
结果
0
但是当我做的时候
movies.fetch()
导致此
Object {readyState: 1, getResponseHeader: function, getAllResponseHeaders: function, setRequestHeader: function, overrideMimeType: function…}abort: function ( statusText ) {always: function () {complete: function () {done: function () {error: function () {fail: function () {getAllResponseHeaders: function () {getResponseHeader: function ( key ) {overrideMimeType: function ( type ) {pipe: function ( /* fnDone, fnFail, fnProgress */ ) {progress: function () {promise: function ( obj ) {readyState: 4responseJSON: Array[41]responseText: "[{"id":11,"title":null,"created_at":"2014-10-16T17:32:24.650Z","updated_at":"2014-10-16T17:32:24.650Z","user_id":2},{"id":12,"title":null,"created_at":"2014-10-16T17:33:01.114Z","updated_at":"2014-10-16T17:33:01.114Z","user_id":2},{"id":13,"title":null,"created_at":"2014-10-16T17:39:31.986Z","updated_at":"2014-10-16T17:39:31.986Z","user_id":2},{"id":14,"title":"star wars 3 ","created_at":"2014-10-16T17:41:32.631Z","updated_at":"2014-10-16T17:41:32.631Z","user_id":2},{"id":15,"title":"star wars 4","created_at":"2014-10-16T17:41:54.040Z","updated_at":"2014-10-16T17:41:54.040Z","user_id":2},{"id":16,"title":"Star wars 5","created_at":"2014-10-16T17:50:28.079Z","updated_at":"2014-10-16T17:50:28.079Z","user_id":2},{"id":17,"title":"asd","created_at":"2014-10-16T18:06:02.597Z","updated_at":"2014-10-16T18:06:02.597Z","user_id":2},{"id":18,"title":"Star Wars 1","created_at":"2014-10-17T13:33:41.411Z","updated_at":"2014-10-17T13:33:41.411Z","user_id":2},{"id":19,"title":"Star Wars 2","created_at":"2014-10-17T13:33:43.129Z","updated_at":"2014-10-17T13:33:43.129Z","user_id":2},{"id":20,"title":"Star Wars 3","created_at":"2014-10-17T13:33:43.614Z","updated_at":"2014-10-17T13:33:43.614Z","user_id":2},{"id":21,"title":"Star Wars 4","created_at":"2014-10-17T13:33:44.136Z","updated_at":"2014-10-17T13:33:44.136Z","user_id":2},{"id":22,"title":"Star Wars 5","created_at":"2014-10-17T13:33:44.553Z","updated_at":"2014-10-17T13:33:44.553Z","user_id":2},{"id":23,"title":"Star Wars 6","created_at":"2014-10-17T13:33:45.045Z","updated_at":"2014-10-17T13:33:45.045Z","user_id":2},{"id":24,"title":"Star Wars 7","created_at":"2014-10-17T13:33:45.405Z","updated_at":"2014-10-17T13:33:45.405Z","user_id":2},{"id":25,"title":"Star Wars 1","created_at":"2014-10-17T14:08:29.747Z","updated_at":"2014-10-17T14:08:29.747Z","user_id":2},{"id":26,"title":"Star Wars 1","created_at":"2014-10-17T14:08:30.392Z","updated_at":"2014-10-17T14:08:30.392Z","user_id":2},{"id":27,"title":"Star Wars 1","created_at":"2014-10-17T14:08:31.204Z","updated_at":"2014-10-17T14:08:31.204Z","user_id":2},{"id":28,"title":"Star Wars 5","created_at":"2014-10-17T18:24:19.032Z","updated_at":"2014-10-17T18:24:19.032Z","user_id":2},{"id":29,"title":"Star Wars 7","created_at":"2014-10-17T18:24:26.330Z","updated_at":"2014-10-17T18:24:26.330Z","user_id":2},{"id":30,"title":"Star Wars 1","created_at":"2014-10-17T18:28:57.187Z","updated_at":"2014-10-17T18:28:57.187Z","user_id":2},{"id":32,"title":"Star Wars 3","created_at":"2014-10-17T18:34:25.670Z","updated_at":"2014-10-17T18:34:25.670Z","user_id":2},{"id":33,"title":"Star Wars 7","created_at":"2014-10-17T18:48:12.072Z","updated_at":"2014-10-17T18:48:12.072Z","user_id":2},{"id":34,"title":"Star Wars 5","created_at":"2014-10-17T18:58:35.853Z","updated_at":"2014-10-17T18:58:35.853Z","user_id":2},{"id":35,"title":"Star Wars 1","created_at":"2014-10-17T19:04:00.502Z","updated_at":"2014-10-17T19:04:00.502Z","user_id":2},{"id":36,"title":"Star Wars 1","created_at":"2014-10-17T19:04:24.880Z","updated_at":"2014-10-17T19:04:24.880Z","user_id":2},{"id":37,"title":"Star Wars 6","created_at":"2014-10-17T19:04:32.956Z","updated_at":"2014-10-17T19:04:32.956Z","user_id":2},{"id":38,"title":"Star Wars 1","created_at":"2014-10-17T19:27:40.209Z","updated_at":"2014-10-17T19:27:40.209Z","user_id":2},{"id":39,"title":"Star Wars 7","created_at":"2014-10-17T21:30:30.830Z","updated_at":"2014-10-17T21:30:30.830Z","user_id":2},{"id":40,"title":"Star Wars 6","created_at":"2014-10-17T21:30:35.783Z","updated_at":"2014-10-17T21:30:35.783Z","user_id":2},{"id":41,"title":"Star Wars 2","created_at":"2014-10-17T21:31:04.950Z","updated_at":"2014-10-17T21:31:04.950Z","user_id":2},{"id":42,"title":"Star Wars 4","created_at":"2014-10-17T21:34:40.229Z","updated_at":"2014-10-17T21:34:40.229Z","user_id":2},{"id":43,"title":"Star Wars 4","created_at":"2014-10-18T08:26:55.440Z","updated_at":"2014-10-18T08:26:55.440Z","user_id":2},{"id":44,"title":"Star Wars 6","created_at":"2014-10-18T08:33:53.247Z","updated_at":"2014-10-18T08:33:53.247Z","user_id":2},{"id":45,"title":"Star Wars 1","created_at":"2014-10-18T08:33:55.066Z","updated_at":"2014-10-18T08:33:55.066Z","user_id":2},{"id":46,"title":"Star Wars 1","created_at":"2014-10-18T08:35:17.138Z","updated_at":"2014-10-18T08:35:17.138Z","user_id":2},{"id":47,"title":"Star Wars 3","created_at":"2014-10-18T08:35:28.749Z","updated_at":"2014-10-18T08:35:28.749Z","user_id":2},{"id":48,"title":"Star Wars 7","created_at":"2014-10-18T08:54:23.623Z","updated_at":"2014-10-18T08:54:23.623Z","user_id":2},{"id":49,"title":"Star Wars 1","created_at":"2014-10-18T08:57:31.560Z","updated_at":"2014-10-18T08:57:31.560Z","user_id":2},{"id":50,"title":"Star Wars 6","created_at":"2014-10-18T09:04:34.729Z","updated_at":"2014-10-18T09:04:34.729Z","user_id":2},{"id":51,"title":"Star Wars 6","created_at":"2014-10-18T09:14:26.799Z","updated_at":"2014-10-18T09:14:26.799Z","user_id":2},{"id":52,"title":"Star Wars 4","created_at":"2014-10-18T09:18:38.227Z","updated_at":"2014-10-18T09:18:38.227Z","user_id":2}]"setRequestHeader: function ( name, value ) {arguments: nullcaller: nulllength: 2name: ""prototype: Object__proto__: function Empty() {}<function scope>state: function () {status: 200statusCode: function ( map ) {statusText: "OK"success: function () {then: function ( /* fnDone, fnFail, fnProgress */ ) {__proto__: Object
我做了
movies.length
现在它输出电影数量
41
因此,如果fetch在我的控制台中有效,为什么它在我的应用中无效呢?请注意,我已经有了
initialize: ->
@collection.on('reset', @render, this)
哪个应该重新收藏。
答案 0 :(得分:0)
通过更改我的Backbone路由器文件中的fetch命令,我能够解决问题
initialize: ->
@collection = new Movieseat.Collections.Movieseats()
@collection.fetch()
要
initialize: ->
@collection = new Movieseat.Collections.Movieseats()
@collection.fetch({reset: true})