简体   繁体   English

Coffeescript Backbone扩展/类继承

[英]Coffeescript Backbone extends / Class Inheritance

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 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 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/ 这是问题的一个方面: http//jsfiddle.net/Y9bPX/11/

Your indentation is off. 你的缩进是关闭的。 Your CoffeeScript looks like this: 你的CoffeeScript看起来像这样:

class MyApp.Models.BaseClass extends Backbone.Model

Linked: () =>
  @._linked
#...

but it should look like this: 但它应该是这样的:

class MyApp.Models.BaseClass extends Backbone.Model

  Linked: () =>
    @._linked
  #...

Your lack of indentation gives you an empty MyApp.Models.BaseClass and then a bunch of inaccessible functions inside an anonymous object in the JavaScript: 你缺少缩进会给你一个空的MyApp.Models.BaseClass ,然后在JavaScript中的匿名对象中有一堆无法访问的函数:

// CoffeeScript boilerplate...
MyApp.Models.BaseClass = (function(_super) {
  // Standard CoffeeScript class boilerplate...
})(Backbone.Model);

({
  Linked: function() {
    return _this._linked;
  },
  // etc...
});

So fix your indentation in your MyApp.Models.BaseClass and you should be fine. 因此,在MyApp.Models.BaseClass修复缩进,你应该没问题。 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. 请记住,CoffeeScript的整个块结构都是基于缩进的,所以如果你没有缩进,那么你就会有一堆废话。

If your test code is as you posted it (and the namespace differences on the classes is a pasting mistake) , the error is that you're not calling the constructor on Page, instead you're making a reference to it: 如果您的测试代码与发布时一样 (并且类上的命名空间差异是一个粘贴错误) ,则错误是您没有在Page上调用构造函数,而是在引用它:

page = new MyApp.Models.Page;

should be 应该

page = new MyApp.Models.Page();

Check it out here (open a console): 在这里查看(打开控制台):

http://jsfiddle.net/Y9bPX/3/ http://jsfiddle.net/Y9bPX/3/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM