Backbone.js 使用coffeescript的Trigger.io主干和OAuth2
我有一个rails应用程序在localhost:3000上提供数据,使用Doorkeeper gem提供安全的API 该客户端是一个Trigger.io应用程序,我正在用我的android手机测试,Railscasts主干集的抽奖器 问题>Backbone.js 使用coffeescript的Trigger.io主干和OAuth2,backbone.js,coffeescript,oauth-2.0,trigger.io,Backbone.js,Coffeescript,Oauth 2.0,Trigger.io,我有一个rails应用程序在localhost:3000上提供数据,使用Doorkeeper gem提供安全的API 该客户端是一个Trigger.io应用程序,我正在用我的android手机测试,Railscasts主干集的抽奖器 问题> 我有一个函数,返回访问服务器的正确oauth令牌和url。我不知道应该在哪里调用该函数,以及应该如何存储返回值,以便在创建新集合之前集合类可以使用它 当客户机查询服务器时,它返回200,并且似乎将请求的对象传回,但是我的视图没有给出预期的结果——当长度应该是
window.Raffler ?= {
Models: {}
Collections: {}
Views: {}
Routers: {}
init: ->
new Raffler.Routers.Entries()
Backbone.history.start()
}
$(document).ready ->
Raffler.init()
A.咖啡
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.Collections.Entries extends Backbone.Collection
url: @url
@url = () ->
return "http://192.168.1.14:3000/api/v1/entries?access_token=#{params.access_token}"
咖啡
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)
initialize: ->
@collection = new Raffler.Collections.Entries()
@collection.fetch()
咖啡
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...
A.咖啡
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.Collections.Entries extends Backbone.Collection
url: @url
@url = () ->
return "http://192.168.1.14:3000/api/v1/entries?access_token=#{params.access_token}"
结果出现“必须指定url”错误。这里有两种方法可以解决第一个问题。您可以在集合实例上设置
url
属性。因此,您可以执行以下操作,而不是返回生成的URL:
对于第二部分,我怀疑您遇到了来自JavaScript的HTTP请求的问题。使用Trigger.io Forge时,通常的解决方案是使用模块发出跨域请求,填充集合的简单方法是:
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}")
)
更有用的方法可能是重写以返回到
forge.requests.ajax
。这可能只是将主干.sync
的最后一行从$.ajax
更改为另一行的情况,因为这两个API非常相似。这里有两种方法可以解决第一个问题。您可以在集合实例上设置url
属性。因此,您可以执行以下操作,而不是返回生成的URL:
对于第二部分,我怀疑您遇到了来自JavaScript的HTTP请求的问题。使用Trigger.io Forge时,通常的解决方案是使用模块发出跨域请求,填充集合的简单方法是:
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}")
)
更有用的方法可能是重写以返回到
forge.requests.ajax
。这可能只是将主干.sync
的最后一行从$.ajax
更改为$.ajax
,因为这两个api非常相似。以防万一它对任何人都有帮助-以防万一它对任何人都有帮助-谢谢Monk,是的,我使用了forge的ajax api来访问我的服务器端点,一切都很好,但正如你所说,我想知道我是否可以将它与backone.sync集成。我给它shotRE:url,我不确定如何为它实例化一个对象,当集合本身被实例化时,该对象将可用。在我创建Raffer?{}名称空间并调用init->创建一个新路由器并启动历史记录的那一刻,路由器然后实例化集合,从而触发无url错误。在哪里以及如何设置url变量?我一直在集合类中尝试url=()->re:overriding sync您是否能够提供您所看到的有问题的行的片段,我尝试过重写sync方法,但它抛出了一个错误,谢谢-非常感谢您的帮助!谢谢Monk,是的,我使用forge的AJAXAPI访问我的服务器端点,一切都很好,但正如你所说,我想知道我是否可以将其与backone.sync集成。我给它shotRE:url,我不确定如何为它实例化一个对象,当集合本身被实例化时,该对象将可用。在我创建Raffer?{}名称空间并调用init->创建一个新路由器并启动历史记录的那一刻,路由器然后实例化集合,从而触发无url错误。在哪里以及如何设置url变量?我一直在集合类中尝试url=()->re:overriding sync您是否能够提供您所看到的有问题的行的片段,我尝试过重写sync方法,但它抛出了一个错误,谢谢-非常感谢您的帮助!