我有一个使用 Doorkeeper gem 在 localhost:3000 上提供数据的 Rails 应用程序以提供安全的 API
客户端是一个 Trigger.io 应用程序,我正在用我的 Android 手机测试,Railscasts Backbone 剧集中的 Raffler。
问题>
我有一个函数可以返回正确的 oauth 令牌和 url 以访问服务器。我很困惑我应该在哪里调用该函数以及我应该如何存储返回值,以便在创建新集合之前它对 Collection 类可用。
当客户端查询服务器时,它返回 200 并且似乎将请求的对象传回,但我的视图没有给出预期的结果 - 它返回零的长度应该是三。
为了测试这一点,我在浏览器中输入了 url,复制了返回的 json 对象并将其直接传递给在 router.coffee/initialize 中实例化 @collection 的函数。这会在视图中获得所需的结果。
我尝试在 Trigger.io 的催化剂调试控制台中获取 json 对象,没有任何乐趣。Fetch 返回一个对象,但长度为 0
不知道如何调试超出我尝试过的,新的咖啡/主干。感谢您的帮助,谢谢!
raffler.coffee
window.Raffler ?= {
Models: {}
Collections: {}
Views: {}
Routers: {}
init: ->
new Raffler.Routers.Entries()
Backbone.history.start()
}
$(document).ready ->
Raffler.init()
条目.咖啡
class Raffler.Collections.Entries extends Backbone.Collection
url: 'http://192.168.1.14:3000/api/v1/entries?access_token=022f854...
initialize: -> # this returns a valid url&token for accessing the server
entry_router.coffee
class Raffler.Routers.Entries extends Backbone.Router
routes:
'': 'index'
initialize: ->
@collection = new Raffler.Collections.Entries()
@collection.fetch()
index: ->
view = new Raffler.Views.EntriesIndex(collection: @collection)
$('#container').append(view.render().el)
entry_index.coffee
class Raffler.Views.EntriesIndex extends Backbone.View
template: _.template( $('#item-template').html() )
initialize: ->
@collection.on('fetch', @render, this)
render: ->
$(@el).html(@template(entries: @collection))
this
索引.html
.....
<head>
<script id="item-template" type="text/x-underscore-template">
<h1> Raffler </h1>
<%- entries.length %>
</script>
</head> etc...
RE:问题1这是我目前正在尝试的:
entry_router.coffee
initialize: ->
@collection = new Raffler.Collections.Entries()
@collection.fetch()
条目.咖啡
class Raffler.Collections.Entries extends Backbone.Collection
url: @url
@url = () ->
return "http://192.168.1.14:3000/api/v1/entries?access_token=#{params.access_token}"
导致“必须指定 url”错误。