【问题标题】:How to call the render function multiple times inside one Backbone View?如何在一个 Backbone View 中多次调用渲染函数?
【发布时间】:2014-04-25 05:18:19
【问题描述】:

所以本质上,我想根据我们正在查看的“轮子”显示 3 个不同的视图。所以我创建了 3 个不同的 JST 模板。显示的初始默认视图是 Cars/first_wheel 模板。当我单击 id 为 #next-step 的按钮时,会触发渲染功能,因为我看到 alert(wheel) 消息框。但是对“$(@el).html(...)”的调用并没有改变显示的内容……这是为什么呢?

Backbone.View.prototype.eventAggregator = _.extend({}, Backbone.Events);

class Wheel.Views.CarsFirstwheel extends Backbone.View

  initialize: ->
    _.bindAll(this, 'render','nextwheel', 'prevwheel');
    @collection = new Wheel.Collections.Cars
    Car_state = new Wheel.Models.Car(
      current_wheel: 0
      next_wheel: 1
      prev_wheel: 2
      current_view: ""
    )
    @collection.add(Car_state)
    @eventAggregator.bind("call_render", @render);

  wheel1_template: JST['Cars/first_wheel'],
  wheel2_template: JST['Cars/second_wheel']
  wheel3_template: JST['Cars/third_wheel']

  render: ->
    wheel = @collection.at(0).get 'current_wheel'
    switch 0
      when 0
        $(@el).html(@wheel1_template())
        alert(wheel)
      when 1
        $(@el).html(@wheel2_template())
        alert(wheel)
      when 2
        $(@el).html(@wheel3_template())
        alert(wheel)
    this       

  nextwheel: ->
    Car_state = @collection.at(0)
    count = ((Car_state.get 'current_wheel')+1)%3
    Car_state.set 
      current_wheel: count
    Car_state.save
    @collection.fetch
    @eventAggregator.trigger("call_render")

  prevwheel: ->
    Car_state = @collection.at(0)
    count = (Car_state.get 'current_wheel'-1)%3
    Car_state.set 
      current_wheel: count
    Car_state.save
    @collection.fetch

  events: ->
    'click #next-wheel': 'nextwheel'

【问题讨论】:

  • 为什么要触发 call_render 函数而不是仅仅调用 render 函数?
  • @GriffinM,当我放一个“@render”时,它不起作用......
  • @GriffinM,忘记在渲染后加上括号,然后它可以工作......谢谢

标签: javascript backbone.js coffeescript


【解决方案1】:

没关系 - 我忘记将“开关 0”中的 0 改回滚轮。当我这样做时,一切正常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-21
    相关资源
    最近更新 更多