骨干查看El没有定义 [英] Backbone view el not defined

查看:115
本文介绍了骨干查看El没有定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的code -

I have this code -

class TableManager 
  tables : {}
  tableViews :{}
  nextId : 0
  rootEl : 'body'
  addTable:(json,el)->
    newTableModel = new TableModel(json,@nextId)
    newTableModel
    @tables[@nextId] = newTableModel
    el = "#table1"
    newView = new TableView({model : newTableModel, columns : json["Columns"], el : el, id : @nextId})
    @tableViews[@nextId] = newView
    newTableModel.renderModel()
    @nextId++

class TableView extends Backbone.View
  tableId : ''
  columns : ''
  thead : ''
  tbody : ''
  input : ''
  rows : ''
  inputId : ''
  typingTimer : ''
  doneTypingInterval : 2000
  el : '#table1'
  initialize:()->
    @model.bind "render", @render 
    @tableId = "resultsTable#{@options.id}"
    @inputId = "filterInput#{@options.id}"
    @columns = @options.columns

  render:()=>
    console.log "EL"
    console.log $(@el)
    console.log @el

本的console.log @el始终是不确定的。我不知道为什么,我设置this.el正确我想?是不是因为渲染被称为事件触发的结果?

The console.log @el is always undefined. I don't know why, I'm setting this.el correctly I thought? Is it because render is being called as a result of an event firing?

推荐答案

您没有一个 ID =表1元素中的DOM当你实例化你的的TableView 。例如,如果你这样做,而不在DOM什么:

You don't have an id="table1" element in the DOM when you instantiate your TableView. For example, if you do this without anything in the DOM:

class V extends Backbone.View
    el: '#no-such-element'
    render: => console.log @el

v = new V
v.render()

你会得到一个未定义在控制台中。

演示: http://jsfiddle.net/ambiguous/ne39B/

如果您想骨干创建一个 ID =表1元素,那么你就需要使用不同的属性来告诉主干这样做; 查看#EL 文档指出:

If you want Backbone to create an id="table1" element then you'll need to use different properties to tell Backbone to do so; the View#el documentation states:

this.el 从视图的标签名的className ID 属性属性

所以,你有一些选择:


  1. 确保#表1 是DOM,当您创建视图。

  2. 通过当你实例化的视图: V =新的TableView(EL:some_dom_object)

  3. 使用标签名的className ID ,和属性查看属性得到骨干,构建相应的DOM元素你。

  1. Make sure #table1 is in the DOM when you create the view.
  2. Pass the el to the view when you instantiate it: v = new TableView(el: some_dom_object).
  3. Use the tagName, className, id, and attributes view attributes to get Backbone to construct the appropriate DOM element for you.

此外,骨干网将不添加视图的到DOM对你来说,即使骨干建立视图的你必须将它添加到DOM自己。

Also, Backbone won't add the view's el to the DOM for you, even if Backbone builds the view's el you'll have to add it to the DOM yourself.

这篇关于骨干查看El没有定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆