【问题标题】:javascript - Coffeescript & Backbone.js - TypeError: this._ensureElement is not a functionjavascript - Coffeescript & Backbone.js - TypeError: this._ensureElement 不是函数
【发布时间】:2013-12-12 05:29:53
【问题描述】:

.: 编辑 :. 如果您遇到同样的问题,请检查您的变量初始化,我忘记了 new

我不断收到 TypeError: this._ensureElement is not a function 并且更早收到 TypeError: this._reset is not a function 但我无法真正重新创建后者的确切设置。

我的脚本顺序正确:

<script src="js/components/jquery.js"></script>
<script src="js/components/underscore.js"></script>
<script src="js/components/backbone.js"></script>
<script src="js/script.js"></script>

我的模型在我的收藏之前注册。

这是一个购物车应用程序。

#namespacing

App =
  Collection : {}
  Model : {}
  View : {}

###
MODEL
###

class ModelItem extends Backbone.Model

  #default values

  defaults:
    name : 'Product Name' 
    quantity : 0
    unit : 'kg'

  #Increase or decrease the quantity

  change_quantity : (type) ->
    qty = @get 'quantity'
    @set 'quantity', if type is 'increase' then ++qty else --qty

###
COLLECTION
###

class CollectionItems extends Backbone.Collection

  model: ModelItem

###
VIEW
###

class ViewItems extends Backbone.View

window.view = ViewItems()
window.item = ModelItem()

【问题讨论】:

  • 错误在哪一行?

标签: javascript backbone.js coffeescript


【解决方案1】:

您缺少new 运算符。

window.view = new ViewItems()
window.item = new ModelItem()

【讨论】:

  • 我期待一些愚蠢的错误,我只是没想到它会那么愚蠢。
  • 我马上就知道答案了,因为我已经多次做过同样的事情;)
  • 天哪,我觉得自己很傻。感谢您为我节省了这么多时间!
【解决方案2】:

如果你使用咖啡脚本来初始化视图,你可能会遇到以下情况:

@createSubView ViewClass
   model: model

Coffee-script 不会因为某种原因而抱怨。但是js输出会出问题。

以下修复了问题

@createSubView ViewClass,
   model: model

我希望这会有所帮助。 这可能是 Coffee-Script 团队的票...

【讨论】:

    猜你喜欢
    • 2013-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-12
    • 2020-06-12
    • 2021-06-05
    • 2011-11-22
    • 1970-01-01
    相关资源
    最近更新 更多