unludo
unludo

Reputation: 5016

Manual constructor chaining with dojo : argument issue

I am trying to create a class inheriting one class.

In this class I want to create 2 objects which will be passed to the constructor of the parent class.

To do this, I have to use manual constructor chaining and call 'inherited' (see http://dojotoolkit.org/reference-guide/1.7/dojo/declare.html#manual-constructor-chaining)

My issue is that I can't correctly pass parameters to the inherited method. When I use the followind code:

   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]);
            }
         });
   });

I get an arg.callee undefined in declare.js.

When I pass 'arguments' as a parameter to inherited, then callee is defined. Is it possible to add more arguments dynamically to the arguments object?

If not how may I call the parent with dynamically created objects in this constructor?

Thanks!

Upvotes: 1

Views: 2970

Answers (2)

neonstalwart
neonstalwart

Reputation: 770

The first argument to this.inherited must always be arguments. This is so dojo.declare can figure out the superclass method based on arguments.callee. Given that this is the case, if you want to send different arguments to the superclass method then you should have an array as the 2nd argument to this.inherited. I haven't confirmed that this works for constructors but I would try the following:

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

I'm curious to find out if it works.

Upvotes: 5

Roy Tinker
Roy Tinker

Reputation: 10152

Recent versions of Dojo[1] allow you to pass a reference to the currently executing function as the first argument to this.inherited, to enable its use in strict mode.

As a side-effect, the second argument can indeed be an array (even from non-strict code):

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

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

[1] 1.12 or later, if I'm not mistaken.

Upvotes: 0

Related Questions