Backbone.js model not defined in view?
-
27-10-2019 - |
Question
I've decided that I am going to try and learn backbone.js by making a web app. This is the first time I have done anything extensive in javascript, so the answer to my problem may be right in front of my face.
I have the web app on github, along with a running version that doesn't work. The javascript console just says that something is undefined when I don't think it should be. Here's the error that chrome gives for line 30 of app.js:
Uncaught TypeError: Cannot read property 'el' of undefined
I've sort of been referencing the backbone-fundamentals example modular to-do app, and it doesn't seem to have any issues with a similar thing.
If anyone could point out what I am doing wrong, I would be most appreciative!
Thanks in advance!
Solution
Your Machine view render method was missing return this
and by default all methods return undefined - that's why chaining didn't work
Edit: and a small tip:
jQuery, Underscore and Backbone register globally - and thanks to that you don't have to define them as dependencies every time - it's enough if you require them in your main script once and after that you can access them from the global scope as you normally would
OTHER TIPS
In my case i was returning this
without the return
keyword. As i was using javascript and not coffeescript it failed on me. So, if you are using javascript use return this;
instead of just this
in the last line of the render()
function.
I also had a problem when using this.collection.on('reset', this.render(), this)
. I instead had to use this.collection.on('reset', this.render.bind(this));