In the project, jQuery and prototype are used together in places, (as historically). Tell me how to distinguish files containing jQuery from prototype? In order to catch all the prototype files and include them only where necessary.
1 answer
First of all, in jquery, any selection of elements is done by calling $("selector")
in prototype $("selector")
is the same as document.getElementById("selector")
.
For non-id samples in prototype, $$("selector")
, here it is.
The prototype handler is initialized as follows:
Event.observe(element, "eventName", function(e){}); // на element навешиваем обработчик события eventName, в нашем случае это анонимная функция // либо element.observe("eventName", function(e){}); // на элемент element - событие eventName // либо Event.observe("elementId", "eventName", function(e){})
For example, to handle a window load event, you can use the following construction
Event.observe(window,"load",function(){});
Well, the creation of classes in prototype:
var ClassName = Class.create({ initialize: function(){} // ..... })
Well, in prototype, the ajax API is completely different, an example of a prototype ajax request:
new Ajax.Request("requestUrl",{ parameters: { someKey: "someValue" }, onSuccess: function(request) { // request - объект xmlHtppRequest // соответственно ответ сервера например храниться в request.responseText // пример преобразования json ответа var fromJson = request.responseText.toJSON(true); }, onFailure: function(){}; // обработчик фейла ^^ });
It's generally quite simple to distinguish between jquery code and prototype code. JQuery "imposes" the use of its functions and in fact, as it were, "leaves" from native javascript. Thus, in jquery code, you will hardly see anything other than calling jquery or user-defined functions.
As for the prototype code - in the part of the code the prototype methods are used, in part - the native js (or user methods, classes, events, etc.)
If you have any questions please ask.
PS: I didn't write about jquery. usually all one way or another, this framework is known
- Thanks for the extended answer. And is it so, if on the page you meet the prototype code, then jQuery will not work correctly for anyone? - trec
- tyts and you will be happy;) - Zowie
- I know this method, I did it this way, but the project is huge, and I’m already tired of changing $. I decided that it would be easier to clean the project from prototype where it is not needed. - trec
- Do not clean the prototype :(. And so - where do not spit jQuery - Zowie