Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/53.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails+Backbone.js:访问视图中的对象时遇到问题_Ruby On Rails_Dom_View_Backbone.js_Coffeescript - Fatal编程技术网

Ruby on rails Rails+Backbone.js:访问视图中的对象时遇到问题

Ruby on rails Rails+Backbone.js:访问视图中的对象时遇到问题,ruby-on-rails,dom,view,backbone.js,coffeescript,Ruby On Rails,Dom,View,Backbone.js,Coffeescript,主干网出现了几天的问题,无法找到解决方法,也无法找到stackoverflow的解决方案 我使用Rails 3.1+最新主干+咖啡脚本: 以下是我的一个观点,作为所有观点中相同问题的一个例子: Blog.Views.Payment ||= {} class Blog.Views.Payment.PaymentView extends Backbone.View className: 'payment_method' tagName: 'td' events: 'cl

主干网出现了几天的问题,无法找到解决方法,也无法找到stackoverflow的解决方案

我使用Rails 3.1+最新主干+咖啡脚本:

以下是我的一个观点,作为所有观点中相同问题的一个例子:

Blog.Views.Payment ||= {}

class Blog.Views.Payment.PaymentView extends Backbone.View
  className:  'payment_method'
  tagName:    'td'
  events:
    'click #renderPayment':  'renderPayment'

  initialize: ->
    # CAN'T access @options.payment_methods here 
    @options.payment_methods.bind(###stuff###)

  render: ->
    # CAN'T access @options.payment_methods here either...
    $(@el).html ("### something with #{@options.payment_methods or @options.payment_methods.model}")
    return @

  updateView: ()->
    # updating view stuff...

  renderPayment:  ->
    # ACCESSING @options.payment_methods fine here!!!
    if ($("#payment_details").length == 0)
      $(@el).append("<ul id='payment_details'>
                       <li id='payment_type'>#{@options.payment_methods.get(1).get('payment_type')}</li>
                     </ul>
                       ").effect("highlight", 700)
我的付款模式:

class Blog.Models.PaymentMethod extends Backbone.Model
  paramRoot: 'payment_method'

  defaults:
    payment_type: null
    # ...

class Blog.Collections.PaymentMethodsCollection extends Backbone.Collection
  model: Blog.Models.PaymentMethod
  url: '/payment_methods'

您确实需要查找并理解->和=>之间的区别。看起来类中的大多数方法都在使用->,而它们应该使用=>。无论你在哪里使用@something,它都是通往这个.something的捷径,而且=>确保这确实是你所期望的。使用->,它将绑定到发出调用的函数中的任何对象,而不是类实例。

选项将传递给视图的initialize方法。因此,您可以通过这种方式直接访问它们

initialize: (options) ->
  # Now you have options
  options.payment_methods.bind(###stuff###)
在render中调用@options应该可以,除非在视图上下文中没有调用render,即从其他回调调用。要解决此问题,请使用fat箭头定义render,它将始终在视图上下文中调用

render: =>
  $(@el).html( ...etc...)
如果您想访问页面上已经存在的元素,那么您需要做一些事情。最重要的是,将el中的DOM元素传递给视图构造函数,您的视图将接管并拥有该元素。例如

view = new Blog.Views.Payment.PaymentView(
  el: $("#product_whatever").get(0)
)
然后在该视图中,您希望将jQuery查找的范围限定到视图的元素。例如:

render: =>
  $(@el).find('div.somewhere_else').html("change this")
coffeescript中的主干快捷方式是

@$('div.somewhere_else').html(...)

祝你好运

我可能错了,但你的问题的症状似乎与这里的症状相符-呵呵,谢谢!我昨天偶然发现了那篇文章,因为它似乎真的符合我的症状。还是没有成功!:-/@麦克斯洛德非常感谢你的解释。在render:=>中,如果我发出警报@options.payment\u methods.get1.get'payment\u type',它会给出错误消息this.options.payment\u methods.get1未定义,但仍然显示带有正确数据的警报。有什么想法吗?确保实例化Blog.Routers.PostsRouter并启动整个过程的代码在文档准备就绪后运行?
render: =>
  $(@el).find('div.somewhere_else').html("change this")
@$('div.somewhere_else').html(...)