【发布时间】:2013-11-28 03:42:22
【问题描述】:
我有一个 Backbone/Rails 应用程序,它列出了服务器并显示了已部署到特定服务器的应用程序。它由 Rails API 支持。我从 rails erb 文件中的引导数据填充服务器,但是当我尝试加载特定服务器的已部署应用程序时,永远不会调用服务器(由日志消息确认)。如果我只访问 url,我可以恢复已部署的应用程序,所以我很确定我的 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 的新手,所以我已经为此苦苦挣扎了一段时间,但我可能缺少一些简单的东西(我希望)。任何帮助将不胜感激。
【问题讨论】:
-
如果这些是准确的代码,缩进将是不正确的。
-
是的,它没有正确粘贴,但它在本地正确缩进。
标签: javascript ruby-on-rails backbone.js