1

Doorkeeper gem を使用して安全な API を提供する localhost:3000 でデータを提供する Rails アプリがあります。

クライアントは、Railscasts Backbone エピソードの Raffler である Android フォンでテストしている Trigger.io アプリです。

問題点>

  1. サーバーにアクセスするための正しい oauth トークンと URL を返す関数があります。新しいコレクションが作成される前に Collection クラスで使用できるように、その関数をどこで呼び出す必要があるか、および戻り値をどのように格納する必要があるかについて混乱しています。

  2. クライアントがサーバーにクエリを実行すると、200 が返され、要求されたオブジェクトが返されたように見えますが、私のビューでは期待どおりの結果が得られません。

これをテストするために、ブラウザに URL を入力し、返された json オブジェクトをコピーして、router.coffee/initialize で @collection をインスタンス化する関数に直接渡しました。これにより、ビューで目的の結果が得られます。

Trigger.io の Catalyst デバッグ コンソールで json オブジェクトを取得しようとしましたが、うまくいきませんでした。Fetch はオブジェクトを返しますが、長さは 0 です

私が試したことを超えてデバッグする方法がわからない、コーヒー/バックボーンの新機能。あなたの助けに感謝します、ありがとう!

ラフラーコーヒー

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

エントリ_ルーター.コーヒー

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)

エントリ_インデックス.コーヒー

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 ここに私が現在試みているものがあります:

エントリ_ルーター.コーヒー

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

最初の問題を解決する 2 つの方法を次に示します。urlCollection インスタンスに属性を設定できます。したがって、生成された 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

2 番目の部分については、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です。2 つの API は非常に似ているため、これはおそらくBackbone.syncfromの最後の行を変更しただけのケースです。$.ajax

于 2012-09-14T08:40:12.670 に答える
0

万が一の場合に備えて、https://github.com/martindavis/trigger-backbone.sync

于 2013-09-17T20:30:20.903 に答える