2013-10-18 26 views
0

工作各種JS IDE,當使用Ext.define定義自定義對象時,它們都會丟失。當通過原型(函數定義,然後原型函數等)定義簡單的js對象時,它們都可以正常工作。是否有替代Ext.define重用和現有的定義 - 東西來裝飾一個現有的對象,而不是定義它?我甚至不介意它是否不採用基類的東西,我只關心我的定義 - 另外,我不需要動態加載,所以我不擔心加載順序等。Ext.define的原型替代品?

對於記錄,我看到有Ext.extend,但我寧願不探索它,因爲它已被棄用。下面

例子:

function Uploader() { 
} 

Uploader.prototype.execute = function(url) { 
    //do stuff here. 
} 

Ext.???(Uploader, { 
    initComponent: function() { 
     // handle ext specific stuff 
    } 
}) 

回答

0

好吧,就混入讀了之後,我有一個可行的模式。

繼續使用define擴展您的ext對象(例如面板),並使用mixins數組添加您的原型對象。定義的類必須具有與原型不同的名稱,否則Ext不會將其創建爲定義。

下面是一個代碼塊演示技術:

Ext.onReady(function(){ 

    var Plain = function() { 
    } 

    Plain.prototype.message = "Hello prototype"; 

    Plain.prototype.say = function() { 
     console.log(this.message); 
    } 

    Plain.prototype.hello = function() { 
     console.log("hello guy!"); 
    } 

    var p = new Plain(); 
    p.say(); 

    Ext.define("PlainPanel", { 
     message: "Hello Ext", 
     extend: 'Ext.panel.Panel', 
     mixins: [Plain] 
    }); 

    var p2 = new PlainPanel(); 
    p2.say(); 
    p2.hello(); 
    console.log(p2); 
}); 
1

這裏的混入通常是如何定義和使用。

Ext.define('Plain', { 
    message: 'Hello prototype', 
    say: function() { 
     console.log(this.message); 
    }, 
    hello: function() { 
     console.log('hello guy!'); 
    } 
}); 

Ext.define('PlainPanel', { 
    extend: 'Ext.panel.Panel', 
    mixins: { 
     plain: 'Plain' 
    }, 
    message: 'Hello Ext' 
}); 

Ext.onReady(function() { 
    var p2 = Ext.create('PlainPanel'); 
    p2.say(); // Hello Ext 
    p2.hello(); // hello guy! 
    console.log(p2); 
}); 

而不是定義mixins爲陣列,它定義爲一個對象,使得所述屬性名稱將成爲內部參考該混入和的值是混入的類名的。這允許您引用mixin的方法,即使裝飾類具有自己的具有相同名稱的方法。