简体   繁体   English

如何在一个主干视图中多次调用render函数?

[英]How to call the render function multiple times inside one Backbone View?

So essentially, I want to display 3 different views depending on what "wheel" we're looking at. 因此,从本质上讲,我想根据我们要查看的“方向盘”显示3种不同的视图。 So I create 3 different JST templates. 因此,我创建了3个不同的JST模板。 The initial default view that shows is the Cars/first_wheel template. 显示的初始默认视图是Cars / first_wheel模板。 When I click on a button with id of #next-step, the render function is triggered as I see the alert(wheel) message box. 当我单击ID为#next-step的按钮时,渲染功能被触发,因为我看到了alert(wheel)消息框。 But the call to " $(@el).html(...)" doesn't change the contents of what's displayed...Why is that? 但是对“ $(@ 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'

Never mind - I forgot to change the 0 in "switch 0" back to wheel. 没关系-我忘了将“开关0”中的0改回滚轮。 When I do that, all works. 当我这样做时,一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM