gpt4 book ai didi

javascript - 嵌套 Backbone 模型未获取数据

转载 作者:行者123 更新时间:2023-11-28 15:51:53 24 4
gpt4 key购买 nike

我有一个 Backbone/Rails 应用程序,它列出服务器并显示已部署到特定服务器的应用程序。它由 Rails API 支持。我从 Rails erb 文件中的引导数据填充服务器,但是当我尝试加载特定服务器的已部署应用程序时,永远不会调用该服务器(由日志消息确认)。如果我只需访问该网址,就可以恢复已部署的应用程序,因此我很确定我的 Backbone 应用程序出了问题。

这是我的应用程序启动:

window.WhatsDeployed =
Models: {}
Collections: {}
Views: {}
Routers: {}
initialize: (initialModels) ->
@start(initialModels)

start: (initialModels) ->
@collection = new WhatsDeployed.Collections.Servers()
@view = new WhatsDeployed.Views.ServersIndex({collection: @collection })
@collection.reset(initialModels)

我的观点

class WhatsDeployed.Views.ServersIndex extends Backbone.View
el:"#serverDetails"

template: JST['servers/index']

initialize: ->
@collection.bind("reset", this.render, this)

render: ->
@selected = _.first(@collection.models)
$(@el).html @template({collection: @collection, selected: @selected})
this

我的服务器模型

class WhatsDeployed.Models.Server extends Backbone.Model
deployed_apps: ->
@_deployed_apps = new WhatsDeployed.Collections.DeployedApps({server: @})
@_deployed_apps.fetch()
console.log(@_deployed_apps)
@_deployed_apps

我的 DeployedApps 集合

class WhatsDeployed.Collections.DeployedApps extends Backbone.Collection
url: ->
'/servers/#{@server.id}/deployed_apps.json'

model: WhatsDeployed.Models.DeployedApp

initialize: (options) ->
@server = options.server

最后是我的生态模板

<h1>Servers</h1>
<p>
<select id="servers">
<% for server in @collection.models: %>
<option id="<%= server.id %>"><%= server.attributes["name"] %></option>
<% end %>
</select>
</p>

<table>
<tr>
<th>Deployed Apps</th>
</tr>
<% for app in @selected.deployed_apps(): %>
<tr>
<td>Hi <%= app %></td>
</tr>
<% end %>
</table>

ServerModel 中的 fetch 调用不会失败并且似乎可以工作,但 API 从未被调用,并且集合的数据似乎不正确。

我是 Backbone 的新手,所以我已经为此苦苦挣扎了一段时间,但我可能缺少一些简单的东西(我希望)。任何帮助将不胜感激。

最佳答案

您必须添加成功回调才能使用 @_deployed_apps 的获取结果,如下所示:

class WhatsDeployed.Models.Server extends Backbone.Model
deployed_apps: ->
@_deployed_apps = new WhatsDeployed.Collections.DeployedApps({server: @})
@_deployed_apps.fetch().success ()=>
console.log(@_deployed_apps)

关于javascript - 嵌套 Backbone 模型未获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20250401/

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