gpt4 book ai didi

javascript - 无法从 Rails 上的收集主干获取数据

转载 作者:行者123 更新时间:2023-11-29 10:32:46 27 4
gpt4 key购买 nike

我正在使用 OMDB api 制作一个应用程序。我在“movie_controller”中定义了一个方法“top_movies”,它正在渲染 json 数据。

我这样定义集合:

class Fanboy.Collections.Movies extends Backbone.Collection
url: '/movie/top_movies'

Click to see JSON response

我在控制台中获取集合并以这种方式获取对象。

Click to see the Image view of console

我想在页面上显示列表。但是我无法显示列表。

movies_router.js.coffee

class Fanboy.Routers.Movies extends Backbone.Router
routes:
"": "index"

initialize: ->
@collection = new Fanboy.Collections.Movies()
@collection.fetch()

index: ->
view = new Fanboy.Views.MoviesIndex(collection: @collection)
$('#movies-container').html(view.render().el)

/view/movies_index.js.coffee

class Fanboy.Views.MoviesIndex extends Backbone.View
template: JST['movies/index']

initialize: ->
@collection.on('reset', @render, this)

render: ->
$(@el).html(@template(movies: @collection))
this

/templates/movies/index.jst.eco

<h4>Top movies of all time </h4>

<ul>
<% for movie in @movies.models : %>
<li><%= movie.get('Title') %></li>
<% end %>
</ul>

在这里我可以看到 h4 标签,但看不到标题列表。我在这里做错了什么?请有人帮忙。

更新:- 我做了一些调试。由于定义的 url 是通过 Omdb API 来的,并且 View 是在主干中异步加载的。因此问题。

所以我将 setTimeout 设置了几秒钟,现在我可以显示列表了。但这会使应用程序变慢。我现在可以做什么?

尽管 @collection.on('reset', @render, this) 应该已经处理了这个问题。但是为什么不能呢?

最佳答案

使用'sync'代替'reset':

@collection.on('sync', @render, this)

"sync" (model_or_collection, response, options) — when a model or collection has been successfully synced with the server.

Backbone.js Catalog of Events

关于javascript - 无法从 Rails 上的收集主干获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42291753/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com