2012-03-23 54 views
1

我想創建一個繼承一個類的類。手動構造函數與dojo鏈接:參數問題

在這個類中,我想創建2個將傳遞給父類構造函數的對象。

要做到這一點,我不得不使用人工構造函數鏈和稱之爲「繼承」(見http://dojotoolkit.org/reference-guide/1.7/dojo/declare.html#manual-constructor-chaining

我的問題是,我不能正確地將參數傳遞給繼承的方法。當我使用後續代碼時:

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。

當我將'參數'作爲參數傳遞給繼承,然後定義了被調用者。是否可以動態添加更多的參數給參數對象?

如果不是我可以調用與此構造函數動態創建的對象的父?

謝謝!

回答

5

的第一個參數this.inherited必須始終從字面上arguments。這使得dojo.declare可以根據arguments.callee找出超類方法。鑑於這種情況,如果你想發送不同的參數到超類方法,那麼你應該有一個數組作爲第二個參數到this.inherited。我還沒有確認這工程建設者,但我會嘗試以下方法:

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

我很好奇,看看它的工作原理。

+0

謝謝!這就是我所做的,進一步閱讀文檔。所以它修復了對父構造函數的調用問題。現在的問題是,當我創建這個對象,我要緩存功能用不上(獲取,查詢,...)。所以還是有些問題。 – unludo 2012-03-24 09:34:26

0

最近道場的版本[1]允許你參考作爲第一個參數傳遞給當前正在執行的功能,以this.inherited,以使得其在嚴格模式使用。

作爲副作用,第二個參數的確可以是一個數組(甚至從非嚴格代碼):

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

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

[1] 1.12或更高版本,如果我沒有錯。