jQuery.sub()
מחזירה: jQuery
תיאור: יוצר עותק חדש של jQuery שתכונותיו ושיטות ניתן לשנות מבלי להשפיע על אובייקט jQuery המקורי.
ישנם שני מקרים שימוש ספציפים שעבורם jQuery.sub() נוצר. הראשון מספק דרך שינוי שיטות jQuery ללא דריסה מלאה של שיטות מקור, ושני עוזר לעשות אנקפסולציה ושטח שמות בסיסי עבור תוספים של jQuery.
שימו לב ש jQuery.sub() לא מנסה לעשות כל סוג של בידוד - זה לא כוונתו.כל השיטות על הגרסה sub של jQuery ימשיכו להצביע על jQuery מקורי (אירועים יהיו קשורים ועדיין יפעלו דרך jQuery הראשי, שאילתות Ajax ואירועים יפעלו דרך jQuery הראשי, וכו'.).
שימו לב כי אם ברצונכם להשתמש בתוסף הזה עבור הפיתוח כדי מאוד לשקול שימוש משהו כמו יישומנים של jQuery UI, המנהלים גם מצב וגם תוספים של תתי שיטות. כמה דוגמאות של שימוש של jQuery UI לבניית תוספים.
שימוש מסוים של שיטה זו ניתן לתאר בצורה הטובה ביותר באמצעות כמה דוגמאות.
דוגמאות
דוגמה 1
מוסיף שיטה sub jQuery כך שהיא לא חשופה כלפי חוץ:
(function(){
var sub$ = jQuery.sub();
sub$.fn.myCustomMethod = function(){
return 'just for me';
};
sub$(document).ready(function() {
sub$('body').myCustomMethod() // 'just for me'
});
})();
typeof jQuery('body').myCustomMethod // undefined
דוגמה 2
דורס כמה שיטות jQuery כדי לספק פונקציונליות חדשה:
(function() {
var myjQuery = jQuery.sub();
myjQuery.fn.remove = function() {
// New functionality: Trigger a remove event
this.trigger("remove");
// Be sure to call the original jQuery remove method
return jQuery.fn.remove.apply( this, arguments );
};
myjQuery(function($) {
$(".menu").click(function() {
$(this).find(".submenu").remove();
});
// A new remove event is now triggered from this copy of jQuery
$(document).bind("remove", function(e) {
$(e.target).parent().hide();
});
});
})();
// Regular jQuery doesn't trigger a remove event when removing an element
// This functionality is only contained within the modified 'myjQuery'.
דוגמה 3
יוצר תוסף שמחזיר תוסף עם שיטות ספציפיות:
(function() {
// Create a new copy of jQuery using sub()
var plugin = jQuery.sub();
// Extend that copy with the new plugin methods
plugin.fn.extend({
open: function() {
return this.show();
},
close: function() {
return this.hide();
}
});
// Add our plugin to the original jQuery
jQuery.fn.myplugin = function() {
this.addClass("plugin");
// Make sure our plugin returns our special plugin version of jQuery
return plugin( this );
};
})();
$(document).ready(function() {
// Call the plugin, open method now exists
$('#main').myplugin().open();
// Note: Calling just $("#main").open() won't work as open doesn't exist!
});
האם מידע זה היה מועיל?

