Inheritance 继承和淘汰

Inheritance 继承和淘汰,inheritance,knockout.js,coffeescript,Inheritance,Knockout.js,Coffeescript,如果我称之为: class ViewModel constructor: -> $.ajax({url: '#.json', type: 'GET', dataType: 'json'}) .done @buildModel buildModel: (data) => @model = ko.mapping.fromJS(data) @model.update = => delete @model.update

如果我称之为:

class ViewModel
  constructor: ->
    $.ajax({url: '#.json', type: 'GET', dataType: 'json'})
    .done @buildModel

  buildModel: (data) =>
    @model = ko.mapping.fromJS(data)
    @model.update = =>
      delete @model.update
      jsonForm = ko.mapping.toJSON(@model)
      $.ajax({url: '#.json', data: jsonForm, type: 'PUT', contentType:"application/json; charset=utf-8", dataType: 'json'})
      .done @buildModel

    ko.applyBindings(@model)

###################################################################

class FormViewModel extends ViewModel
  buildModel: =>
    super()
一切都很好。如果我试图继承

$(document).bind 'pageinit', =>
  @form = new ViewModel
获取错误:

$(document).bind 'pageinit', =>
  @form = new FormViewModel

为什么ko.applyBindings对这种继承不满意?

FormViewModel
buildModel
函数中使用
super

  • super()
    的意思是:调用同名的父方法,不带任何参数
  • super
    的意思是:使用我收到的任何参数调用同名的父方法
因此,子类总是使用data=
undefined
调用父
buildModel
函数

还要注意,我认为您不需要在
buildModel
函数中调用
ko.applyBindings
。整个视图模型只需调用一次

Uncaught Error: Unable to parse bindings.
Message: ReferenceError: update is not defined;
Bindings value: click: update