【问题标题】:Trigger.io Backbone and OAuth2 using coffeescript使用咖啡脚本的 Trigger.io Backbone 和 OAuth2
【发布时间】:2012-09-12 05:11:02
【问题描述】:

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

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

问题>

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

  2. 当客户端查询服务器时,它返回 200 并且似乎将所请求的对象传回,但我的视图没有给出预期的结果 - 它在长度应该为 3 时返回零。

为了测试这一点,我在浏览器中输入了 url,复制了返回的 json 对象并将其直接传递给在 router.coffee/initialize 中实例化 @collection 的函数。这会在视图中获得所需的结果。

我尝试在 Trigger.io 的催化剂调试控制台中获取 json 对象,但没有任何乐趣。 Fetch 返回一个对象,但长度为 0

不知道如何调试超出我尝试过的,对于咖啡/Backbone 的新手。感谢您的帮助,谢谢!

raffler.coffee

window.Raffler ?= {
  Models: {}
  Collections: {}
  Views: {}
  Routers: {}
  init: ->
    new Raffler.Routers.Entries()
    Backbone.history.start()
}

$(document).ready ->
  Raffler.init()

entry.coffee

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

entries_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)

entries_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

index.html

.....
<head>
<script id="item-template" type="text/x-underscore-template">
        <h1> Raffler </h1>
        <%- entries.length  %>
</script>
</head> etc...

RE:问题 1 这是我目前正在尝试的:

entries_router.coffee

initialize: ->
    @collection = new Raffler.Collections.Entries()
    @collection.fetch()

entry.coffee

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 must be specified' 错误。

【问题讨论】:

    标签: backbone.js coffeescript oauth-2.0 trigger.io


    【解决方案1】:

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

    对于第二部分,我怀疑您遇到问题是由于 Same origin policy 来自 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.sync 的最后一行从$.ajax 更改为两个API 非常相似的情况。

    【讨论】:

    • 谢谢 Monk,是的,我使用 forge 的 ajax api 来访问我的服务器端点,并且一切正常,但就像你说的,我想知道我是否可以将它与 backone.sync 集成。我来试试
    • RE: url 我不确定如何为其实例化一个对象,该对象在集合本身被实例化时可用。在我创建 Raffer ?{} 命名空间并调用 init-> 的那一刻,它创建了一个新的路由器并启动了历史记录,然后路由器实例化了触发 no url 错误的集合。您将在哪里以及如何设置 url 变量?我一直在 Collections 类中尝试 url = ()->
    • RE: 覆盖同步你能提供你所看到的有问题的行的sn-p吗,我试图覆盖同步方法并且它抛出一个错误,谢谢 -非常感谢您的帮助!
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2013-06-15
      • 1970-01-01
      • 1970-01-01
      • 2014-04-27
      • 2013-01-09
      • 2012-08-26
      • 2013-08-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多