0
我正在寫一個jQuery插件來呈現畫布中的圖像。JQuery插件體系結構,在哪裏定義對象和變量的生命週期?
的最終目標是才達到類似
var myImageSource = new ImageSource(path,width,height);
$("#myCanvas").Render({"source" : myImageSource});
插件requiers幾類,herpers和其他一些jQuery插件才能正常工作。
讓我們說我有
- 鼠標滾輪的jQuery插件
- 一個緩存庫不是一個jQuery插件,但與原型的對象,一些枚舉
我有一個依賴動畫引擎,是一個循環,需要一個全局變量(或至少在插件級別)
function runAnimations(timeStamp) {
window.requestAnimationFrame(runAnimations);
for (var i = 0; i < animations.length; i++) {
animations[i].update(timeStamp);
}
}
而且我確定自己的對象,如
- 點
- 矩形
- 視見
- ImageSource的
- 動畫1
所以我嘗試是這樣的:
- Reference to other script library (like Cache)
- Reference to other JQuery Plugin
; (function ($, window, document, undefined) {
//global variable declaration
var animations = [];
var isAnimationLoopStarted = false;
//global functions
function runAnimations(timeStamp) {
window.requestAnimationFrame(runAnimations);
for (var i = 0; i < animations.length; i++) {
animations[i].update(timeStamp);
}
}
//objects declarations
function Rect(x, y, height, width) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
Rect.prototype.moveTo = function (x, y) {
this.x = x;
this.y = y;
};
//other object declarations Point, ImageSource, ViewPort etc..
//plugin interface
var methods = {
init: function() {
return this.each(function() {
});
});
},
destroy: function() {
return this.each(function() {
});
}
};
$.fn.render = function (method) {
if (method === 'destroy') {
return methods.destroy.apply(this);
} else {
return methods.init.apply(this);
}
}
})(jQuery, window, document);
所以我的問題是:
- 你認爲這是確定走這條路?
- 如果我這樣做,ImageSource的定義不會成爲可用外 插件
- 我應該放棄的ImageSource對象使用數組代替,所以我有 與對象定義沒有問題
- 什麼在插件內部定義的全局變量的生命週期就像動畫一樣,它會一直可用嗎?
- 使用變量如動畫還是更好 使用.data jquery函數是最佳做法,但在這種情況下如何共享 變量?
感謝您通過事先的任何幫助
弗雷德
可怕的問題標題。使它成爲一個問題? – JohnFx 2012-04-26 23:52:56
我改變了山雀樂,我希望現在好一點。 – 2012-04-27 06:30:02