手动构造函数链接与dojo:参数问题

时间:2012-03-23 15:22:30

标签: inheritance constructor dojo

我正在尝试创建一个继承一个类的类。

在这个类中,我想创建两个对象,这些对象将被传递给父类的构造函数。

要做到这一点,我必须使用手动构造函数链接并调用'inherited'(参见http://dojotoolkit.org/reference-guide/1.7/dojo/declare.html#manual-constructor-chaining

我的问题是我无法正确地将参数传递给继承的方法。当我使用followind代码时:

   define([ "dojo/_base/declare", "dojo/store/JsonRest", "dojo/store/Memory", "dojo/store/Cache", "dojo/store/Observable"],

   function(declare, JsonRest, Memory, Cache, Observable)
   {
      var userStore;
      return declare("app.UserStore", [Cache],
         {
            "-chains-":
            {
               constructor: "manual"
            },
            constructor: function()
            {
               this.masterStore = new JsonRest({
                  target: "/User/json",
                  idProperty: "name"
               });

               this.cacheStore = new Memory({ idProperty: "name" });

               this.inherited([this.masterStore, this.cacheStore]);
            }
         });
   });

我在declare.js中得到了一个未定义的arg.callee。

当我将'arguments'作为参数传递给inherited时,定义了callee。是否可以动态地向参数对象添加更多参数?

如果不是,我怎么能在这个构造函数中用动态创建的对象调用父元素?

谢谢!

2 个答案:

答案 0 :(得分:5)

this.inherited的第一个参数必须始终为arguments。这样dojo.declare可以找出基于arguments.callee的超类方法。鉴于这种情况,如果你想向超类方法发送不同的参数,那么你应该有一个数组作为this.inherited的第二个参数。我还没有确认这适用于构造函数,但我会尝试以下方法:

this.inherited(arguments, [this.masterStore, this.cacheStore]);

我很想知道它是否有效。

答案 1 :(得分:0)

最近版本的Dojo [1]允许您将对当前正在执行的函数的引用作为this.inherited的第一个参数传递,以便在严格模式下使用它。

作为副作用,第二个参数确实可以是一个数组(即使是非严格的代码):

constructor: function fn() {
    //...

    this.inherited(fn, [this.masterStore, this.cacheStore]);
}

[1] 1.12或更高版本,如果我没弄错的话。

相关问题