Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript - Coffeescript & Backbone.js - TypeError: this._ensureElement is not a function

.: EDIT :. If you are having the same problem check your variable initialization, i forgot about new.

I keep getting TypeError: this._ensureElement is not a function and got TypeError: this._reset is not a function once earlier but I can't really recreate the exact setup for the latter.

I have my scripts in right order:

<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>

My model is registered before my collection.

It's a shopping cart app.

#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()
like image 350
Richard the Lionheart Avatar asked Nov 26 '13 20:11

Richard the Lionheart


People also ask

Is CoffeeScript obsolete?

As of today, January 2020, CoffeeScript is completely dead on the market (though the GitHub repository is still kind of alive).

Is CoffeeScript slower than JavaScript?

Short answer: No. CoffeeScript generates javascript, so its maximum possible speed equals to the speed of javascript.


Video Answer


1 Answers

You're missing the new operator.

window.view = new ViewItems()
window.item = new ModelItem()
like image 96
Casey Foster Avatar answered Oct 28 '22 13:10

Casey Foster