1

我有一个使用 Doorkeeper gem 在 localhost:3000 上提供数据的 Rails 应用程序以提供安全的 API

客户端是一个 Trigger.io 应用程序,我正在用我的 Android 手机测试,Railscasts Backbone 剧集中的 Raffler。

问题>

  1. 我有一个函数可以返回正确的 oauth 令牌和 url 以访问服务器。我很困惑我应该在哪里调用该函数以及我应该如何存储返回值,以便在创建新集合之前它对 Collection 类可用。

  2. 当客户端查询服务器时,它返回 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”错误。

4

2 回答 2

1

这里有两种方法可以解决第一个问题。您可以url在 Collection 实例上设置属性。因此,您可以执行以下操作,而不是返回您生成的 URL:

class Raffler.Collections.Entries extends Backbone.Collection
  initialize: (args...) ->
    @url = 'http://192.168.1.14:3000/api/v1/entries?access_token=022f854'
    super(args...)

entries = new Raffler.Collections.Entries() entries.fetch() # will use the url attribute on the collection instance

您还可以将 URL 指定为参数fetch

entries.fetch(url: 'http://somewhereelse.com/') # will use a different URL

对于第二部分,我怀疑由于来自 JavaScript 的 HTTP 请求的同源策略,您遇到了问题。使用 Trigger.io Forge 时通常的解决方案是使用forge.requests模块进行跨域请求,填充集合的简单方法是:

entries = new Raffler.Collections.Entries()

forge.requests.ajax(
  url: 'http://192.168.1.14:3000/api/v1/entries?access_token=022f854'
  type: 'GET'
  dataType: 'json'
  success: (data) ->
    entries.reset(data)
  error: (e) ->
    forge.logging.error("Failed to get entries: #{e.message}")
)

一种更有用的方法可能是覆盖Backbone.sync以返回到forge.requests.ajax. 这可能只是更改Backbone.syncfrom的最后一行的情况,$.ajax因为这两个 API 非常相似。

于 2012-09-14T08:40:12.670 回答
0

以防万一它可以帮助任何人 - https://github.com/martindavis/trigger-backbone.sync

于 2013-09-17T20:30:20.903 回答