【问题标题】:Context In Backbone View主干视图中的上下文
【发布时间】:2014-07-04 11:33:31
【问题描述】:

我正在创建一个基本的调查应用程序,以更好地学习骨干。我正在努力做到这一点,以便用户只能参加一次特定的调查。我在 Rails 端构建了这个,如果同一用户尝试再次进行相同的调查,它会返回错误。

成功和错误函数被正确调用。我遇到的问题是当我尝试调用@saveSurvey 时。我确定问题在于成功函数上下文中的 this.saveSurvey 实际上并不是视图的上下文。知道我应该如何传递上下文来实际调用这个函数吗?

class SurveyMe.Views.SurveyShow extends Backbone.View

  template: JST['templates/surveys/survey_show']

  initialize: ->
    @model.on('all', @render, this)
    @questionNumber = 0
    @questionLimit = @model.get("questions").length - 1
    @completion = new SurveyMe.Models.Completion
    #@listenTo(@completion,'change', @saveSurvey)

  render: ->
    $(@el).html(@template(survey: @model, questionNumber: @questionNumber))
    this

  back: ->
    if @questionNumber <= @questionLimit and @questionNumber > 0
      @questionNumber -= 1
      $("#container").html(@render().el)
    else
      Backbone.history.navigate("surveys",trigger: true)

  events:
      'click #answer': 'updateQuestion'
      'click #back': 'back'

  updateQuestion: ->
    choice = new SurveyMe.Models.Choice
    choice.save(
      choice:
        appuser_id: Cookie.get('survey_user_id')
        question_id: @model.get('questions')[@questionNumber]["id"]
        answer_id: "4"
      )
    if @questionNumber < @questionLimit
      @questionNumber += 1
      $("#container").html(@render().el)
      @renderQuestion()
    else
      @completion.set(survey_id: @model.get('id'), appuser_id: Cookie.get('survey_user_id'))
      @completion.save null,
        success: ->
          @saveSurvey()
        error: ->
          alert('fail fail fail')
      Backbone.history.navigate("surveys",trigger: true)

  renderQuestion: ->
    question = new SurveyMe.Models.Question(id: @model.get("questions")[@questionNumber]["id"])
    questionView = new SurveyMe.Views.Question(model: question)
    $('#questions').html(questionView.render().el)

  saveSurvey: ->
    alert('great success')
    @model.save(
      number_taken: @model.get('number_taken') + 1
    )

【问题讨论】:

    标签: javascript backbone.js coffeescript


    【解决方案1】:

    这是一个 CoffeeScript 问题。

    您需要使用粗箭头 (=&gt;) 语法来维护成功回调中 this 的上下文。

      @completion.save null,
        # Use the fat arrow here "=>" instead of the skinny arrow "->"
        success: =>
          @saveSurvey()
        # You would also need to do it here if you planned to use @ to 
        # refer to the parent context in the error callback
        error: ->
          alert('fail fail fail')
    

    当您想在函数中使用@ 来引用父函数的上下文时,您需要这样做。

    CoffeeScript 提供了 fat-arrow 和 @ 快捷方式,以便在函数中维护 this 范围。在幕后,它只是做了所有_this = this 类型的事情,你通常会在常规的旧 JavaScript 中自己做。

    以下是相关文档:http://coffeescript.org/#fat-arrow

    【讨论】:

    • 完全正确!我在学习主干的同时也在学习咖啡脚本。可能不是最好的计划;)
    • 不,这是一个很棒的计划! Fat arrow 是我最喜欢的 CoffeeScript 便利之一。我敢肯定,一旦你掌握了窍门,你也会爱上它的。
    • 您也可以使用success: @saveSurvey.bind(@),总体效果相同。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多