I am trying to break out some methods that I am using across my backbone models, and I don’t understand why it isn’t working.
base_class.js.coffee
class MyApp.Models.BaseClass extends Backbone.Model
Linked: () =>
@._linked
Link: (form) =>
if @._linked == false
$(form).backboneLink(@, {'prefixed':true})
@._linked = true
else
$(form).backbonePopulate(@, {'prefixed':true})
Dirty: () ->
@collection.Dirty()
@._dirty = true
Clean: () ->
@._dirty = false
isDirty: () =>
@._dirty
page.js.coffee
#= require ./base_class
class MyApp.Models.Page extends MyApp.Models.BaseClass
initialize: () ->
console.log('Page Object initialized')
@._dirty = false
@changes = []
@.name = 'Page'
@._linked = false
url: () ->
'/pages/' + @id
However when I go into the console
page = new MyApp.Models.Page(); #=> Page Object initialized
page.Link($('#myform')); #=> Uncaught TypeError: Object #<Page> has no method 'Link'
I dont understand why the methods aren’t being inherited.
Here is a jsfiddle of the issue: http://jsfiddle.net/Y9bPX/11/
Your indentation is off. Your CoffeeScript looks like this:
but it should look like this:
Your lack of indentation gives you an empty
MyApp.Models.BaseClassand then a bunch of inaccessible functions inside an anonymous object in the JavaScript:So fix your indentation in your
MyApp.Models.BaseClassand you should be fine. Remember that the entire block structure of CoffeeScript is based on the indentation so if you don’t have your indentation right then you have a bunch of nonsense.