Javascript 尝试使用Backbone.sync从Rails中的操作控制器获取数据-非restful
通过自定义的@sync从Rails控制器获取数据时,我真的很复杂 这是我的动作控制器:Javascript 尝试使用Backbone.sync从Rails中的操作控制器获取数据-非restful,javascript,ruby-on-rails,backbone.js,Javascript,Ruby On Rails,Backbone.js,通过自定义的@sync从Rails控制器获取数据时,我真的很复杂 这是我的动作控制器: class CargasController < ApplicationController def readExcel @data = [1,2,3,4,5,6] respond_to do |format| format.json { render json: @data } end end end 这是我的观点。(在执行click事件后,我调用
class CargasController < ApplicationController
def readExcel
@data = [1,2,3,4,5,6]
respond_to do |format|
format.json { render json: @data }
end
end
end
这是我的观点。(在执行click事件后,我调用readExcel方法
class App.Views.ShowRuta extends Backbone.View
events:
'click button': 'readExcel'
clasName: 'information'
template: JST['app/templates/excel_step_one']
initialize: ->
@modelo = @model.toJSON()
@render()
render: ->
this.$el.html( @template(@model.toJSON() ) )
this
readExcel: ->
$('h2').html(" Cargando ruta - Espere")
modelo = @modelo
$('.contenedor').fadeOut '10000', ->
$('.col-md-7 h1').html('')
$('.col-md-7 h4').html('')
$('.jumbotron').html('')
$('.contenedor').fadeIn '10000'
modelo = new App.Models.Ruta({id: modelo.id })
datos = modelo.readExcel()
$('.jumbotron').html(datos.readyState)
console.log datos
#debugger
当我执行“console.log datos”时,得到“resolved”
但是,当我添加到调试器时,在最后一行中,dato将获得一个“挂起”状态
如何从“待定”传递到“已解决”?好的,我刚刚发现了我的响应 主干网模型的一个例子
class App.Models.Ruta extends Backbone.Model
urlRoot: '/rutas/'
readExcel:(opts) ->
url = 'rutas/readExcel'
options =
url: url
type: 'GET'
_.extend options, opts
return (@sync or Backbone.sync).call this, null, this, options
class App.Models.Direction extends Backbone.Model
urlRoot: '/directions/'
defaults:
id: 1
address: 'default address'
latitude: 'default latitude'
longitude: 'default longitude'
priority: 'default priority'
sync: (method, model, opts) ->
url: 'directions/custom'
options =
url: 'directions/custom'
type: 'GET'
Backbone.sync.call this, null, this, _.extend(options
, opts)
然后,它就是我的控制器(在本例中为directions_controller.rb
class DirectionsController < ApplicationController
respond_to :json
def index
respond_with(@directions = Direction.all)
end
def custom
#You can add a query Rails
@response = {id: 100, address: 'Direcction', latitude: 199, longitude: 199}
respond_with(@response)
end
end
现在,当您通过主干调用时(例如通过某个主干视图)
现在您可以得到一个rails控制器值:)
我希望这个解决方案能解决你遇到的任何人的问题。我整个周末都在寻找解决方案
resources :directions do
collection do
get :custom
end
end
this.model = new App.Models.Direction
this.model.sync()
this.model.fetch()