Doorkeeper gem を使用して安全な API を提供する localhost:3000 でデータを提供する Rails アプリがあります。
クライアントは、Railscasts Backbone エピソードの Raffler である Android フォンでテストしている Trigger.io アプリです。
問題点>
サーバーにアクセスするための正しい oauth トークンと URL を返す関数があります。新しいコレクションが作成される前に Collection クラスで使用できるように、その関数をどこで呼び出す必要があるか、および戻り値をどのように格納する必要があるかについて混乱しています。
クライアントがサーバーにクエリを実行すると、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 を指定する必要があります」というエラーが発生します。