Uncaught TypeError: Cannot call 'extend' method from undefined

I am trying to get the CoffeeScript class for the Backbone.Model extension. I created a new rails 3.1 application, created a Stone scaffold with three attributes, and fixed a Todos.coffee example fragment in a .js.coffee stone. I have both backbone.js and underscore.js in the app / assets / javascripts folder. When I run this under the Chrome Java console, I get the message above in the console log. Any ideas?

Current code:

$ -> class Todo extends Backbone.Model # Default attributes for the todo. defaults: content: "empty todo..." done: false # Ensure that each todo created has `content`. initialize: -> if !@get ("content") @set({ "content": @defaults.content }) # Toggle the `done` state of this todo item. toggle: -> @save({ done: !@get ("done") }) # Remove this Todo from *localStorage* and delete its view. clear: -> @destroy() @view.remove() 

The application.js used is what Rails 3.1 created. I copied backbone.js and underscore.js from the Todos github repository, https://github.com/JasonGiedymin/backbone-todojs-coffeescript

+4
source share
1 answer

The problem is that underscore.js loads after backbone.js when the prereq preview file is loaded in front of it. (Note in Backbone.js source that it immediately sets var _ = root._ , so even if global _ declared later, it does not appear from the Backbone area.) Asterisks load JS files into your resource directory in alphabetical order by default.

You can fix it using Sprockets: Put

 //= require underscore.js 

front

 //= require_tree . 

to ensure that it is downloaded first.

+16
source

Source: https://habr.com/ru/post/1389696/


All Articles