【问题标题】:Backbone is unable to find the dynamic div created by templateBackbone 找不到模板创建的动态 div
【发布时间】:2014-02-20 17:51:01
【问题描述】:
  • 获取集合(正常)
  • 索引渲染视图(确定)
  • 为集合中的单个项目渲染视图(不正常)

    class MyApp.Routers.ProjectsRouter extends Backbone.Router
    
      initialize: ->
        @projects = new MyApp.Collections.ProjectsCollection()
    
      routes:
        "projects" : "projects"
    
      projects: ->
        @projects.fetch().done =>
          @view = new MyApp.Views.Projects.IndexView(collection: @projects)
          $("#projects").html(@view.render().el)
    
    class MyApp.Views.Projects.IndexView extends Backbone.View
      template: JST["backbone/templates/projects/index"]
    
      addAll: =>
        @collection.each(@addOne)
    
      addOne: (project) =>
        view = new MyApp.Views.Projects.ProjectView({ model: project })
        **$(".row-fluid").append(view.render().el)**
    
      render: =>
        @$el.html(@template('<div class="row-fluid"></div>'))
        @addAll()
        this
    
    class MyApp.Views.Projects.ProjectView extends Backbone.View
      template: JST["backbone/templates/projects/project"]
    
      render: ->
        @$el.append(@template())
        this
    

".row-fluid" 没有建立,我已经使用 firebug 调试了它。有人请帮助我,我从过去的 5 到 6 小时一直在敲我的头。

谢谢

【问题讨论】:

  • 愚蠢的问题,但在模板中找到.row-fluid?愿意展示模板代码吗?
  • 你能准备一些简单的jsFiddle吗? $(".row-fluid") 是全局选择器,所以你的 DOM 树中没有这样的元素。
  • 缺少大括号传入集合 -> @view = new MyApp.Views.Projects.IndexView({collection: @projects})
  • @msvalkon 添加模板代码@$el.html(@template('
    '))

标签: ruby-on-rails backbone.js coffeescript


【解决方案1】:

试试这个:

class MyApp.Routers.ProjectsRouter extends Backbone.Router
  projects: ->
    @projects.fetch().done =>
      @view = new MyApp.Views.Projects.IndexView(collection: @projects)
      $("#projects").html(@view.render().el)
      @view.addAll()

class MyApp.Views.Projects.IndexView extends Backbone.View
  render: =>
    @$el.html(@template('<div class="row-fluid"></div>'))
    this

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-08
    • 1970-01-01
    • 1970-01-01
    • 2015-04-04
    • 2014-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多