2013-03-24 3 views
2

У меня есть класс Base, который наследуется от [_WidgetBase, _TemplatedMixin]. Base правильно работает. Теперь я наследовать в другом классе этот Base, который не работаетКонструктор виджета Dijit выдает ошибку «вызывающий цепной конструктор»

define([ 
    "dojo/_base/declare", "dojo/parser", ... 
], function(declare, parser, ...){ 
    return declare("mc.widgets.Base", [_WidgetBase, _TemplatedMixin], { 
     templateString: 
      '<div class="mc-note-base">'+ 
      '</div>', 
     constructor: function(argv){ 
      var self = this.inherited(arguments); 
      return self; 
     }, 
     data: function(){ 

     }, 
     postCreate: function(){ 
      ... 
     } 
    }) 
}); 

Производный класс

define([ 
    "dojo/_base/declare", "mc/base/path", "mc/widgets/Base" 
], function(declare, path, Base){ 
    return declare("mc.widgets.Derived", [Base], {}); 
}) 

Производный класс бросает

Error: declare mc.widgets.Derived: calling chained constructor with inherited

ответ

2

Это происходит потому, что constructor часть жизненного цикла виджета обрабатывается специальным механизмом цепочки, предназначенным для более гибкого создания виджета. Вы можете read here для получения дополнительной информации, а та часть, которая относится к вашей ситуации говорит:

Superclass constructors are always called automatically, and always before the subclass constructor. This convention reduces boilerplate in 90% of cases. If it doesn’t fit your needs see Manual Constructor Chaining below. For all other methods, use this.inherited(arguments) to call the superclass method of the same name.

Если вы просто удалите this.inherited(arguments) вызов из метода конструктора Втулки, ваша проблема будет решена. Here is a simple jsfiddle, который имитирует ваш виджет и демонстрирует решение.

Смежные вопросы