Je suis sur le point de porter une série de mes projets jQuery sur Vanilla Javascript (javascript pur, pas de framework) et je voudrais savoir s'il existe des [framework adaptateurs/adaptatifs agnostiques framework] existants?Agnostic Javascript Framework Adaptateur pour jQuery et Mootools?
Par exemple, je suis envisager quelque chose comme ceci:
// My Project
(function(){
// Fetch all the elements using Sizzle Selector System
var $els = Agnostic.find('.penguins');
$els.hide();
// Perform a Ajax Request
Agnostic.ajax({
dataType: 'json',
sucess: function(){
},
error: function(){
}
});
});
/**
* Our Agnostic Framework
* Provides a framework agnostic interface for jQuery and MooTools
*/
var Agnostic = {
framework: null,
Framework: null,
/**
* Initialise our Agnostic Framework
*/
init: function(){
switch (true) {
case Boolean(jQuery||false):
Agnostic.Framework = jQuery;
Agnostic.framework = 'jQuery';
break;
case Boolean(MooTools||false):
// Check for Sizzle
if (typeof Sizzle === 'undefined') {
throw new Error('MooTools interface requires the Sizzle Selector Engine.');
}
Agnostic.Framework = MooTools;
Agnostic.framework = 'MooTools';
break;
default:
throw new Error('Could not detect a framework.');
break;
}
}
/**
* Our Element Object
* Used to Wrap the Framework's Object to provide an Agnostic API
*/
Element: {
/**
* Create the Element Wrapper
*/
create: function(Object){
var El = new Agnostic.Element;
El.Object = Object;
},
/**
* Hide the Element
*/
hide: function(){
switch (Agnostic.framework) {
case 'jQuery':
this.Object.hide();
break;
case 'MooTools':
this.Object.setStyle('display','none');
break;
}
},
/**
* Show the Element
*/
show: function(){
switch (Agnostic.framework) {
case 'jQuery':
this.Object.show();
break;
case 'MooTools':
this.Object.setStyle('display','');
break;
}
}
},
/**
* Fetch elements from the DOM using the Sizzle Selector Engine
*/
find: function(selector){
var Element = null;
// Fetch
switch (Agnostic.framework) {
case 'jQuery':
Element = jQuery(selector);
break;
case 'MooTools':
Element = new Elements(new Sizzle(selector));
break;
}
// Wrap
Element = Agnostic.Element.create(Element);
// Return Element
return Element;
},
/**
* Perform an Ajax Request
* We use the jQuery.ajax interface here
* But they are more or less the same
*/
ajax: function(request){
// Send Request
switch (Agnostic.framework) {
case 'jQuery':
jQuery.ajax(request);
break;
case 'MooTools':
(new Request(request)).send();
break;
}
// Wrap
Element = Agnostic.Element.create(Element);
// Return Element
return Element;
}
};
Je ne comprends pas, vous voulez un cadre qui ressemble comme jQuery, sent jQuery, goûte même jQuery ... Pourquoi ne pas simplement utiliser jQuery ... –
@Andrew - Parce qu'il ne veut pas être dépendant du framework. –
@Andrew - Chase est sur place. Je vais porter mes plugins jQuery existants à vanilla javascript donc 90% du code n'utilisera pas de framework, pour les choses où ne pas utiliser un framework est irréalisable comme les sélecteurs et la manipulation DOM (comme les faire dans vanilla js est atroce) quantité de code doit utiliser un adaptateur de cadre agnostique. Le choix d'avoir l'interface principalement jQuery est dû à la part de marché importante de jQuery, et est donc une bonne décision d'adoption. En ce qui concerne le portage de mes plugins dans les versions jQuery et MooTools - il est loin d'être idéal de devoir gérer deux bases de code – balupton