Visualizzazione dei risultati da 1 a 3 su 3

Discussione: Traduzione codice JS

  1. #1
    Utente di HTML.it
    Registrato dal
    Jan 2009
    Messaggi
    48

    Traduzione codice JS

    Ciao a tutti....^^

    scusate sto facendo un progetto e purtroppo mi hanno passato delle funzioni in un linguaggio che non è il mio... sto parlando di javascript

    qualcuno mi potrebbe dire in soldoni cosa fanno le singole funzioni?

    grazie anticipatamente

    codice:
    buttons.Button = Class.create({
    
      /**
       * params:
       *   
       * handler (optional) - the function to be invoked as handler of the click event on the button
       * domElement (optional) - the element of the button
       * enabled (optional) - If unset defaults to false
       * domElementName (optional) - If the dom creation is deferred
    due to different windows button usage, this is used to create the dom
       * domId (optional) - when created, the dom element id will be set to domId
       */
       
        initialize: function (params) {
        this.handler = params.handler ? params.handler : null;
        this.domElement = params.domElement ? params.domElement : null;
        this.domElementName = params.domElementName ? params.domElementName : null;
        this.domId = params.domId ? params.domId : null;
    
        if (this.domElement) {
          this.domElementName = this.domElement.name;
          
          /**
          if($(this.domElement.id).getAttribute("onclick")){    
            if(this.handler==null){
              this.handler = $(this.domElement.id).getAttribute("onclick");
            }
            this.domElement.onclick = null;
          }
          */     
                
          if (this.domElement.onclick) {
            this.handler = this.domElement.onclick;
            this.domElement.onclick = null;
          }
    
          if (this.domId) {
            this.domElement.id = this.domId;
          } else if (this.domElement.id) {
            this.domId = this.domElement.id;
          }
        }
    
        this.enabled = params.enabled === true ? true : false;
        this.active = false;
    
        if (this.domElement) {
          this.applyState();
        }
      },
      
      startHandler: function () {
        //if(this.domElement){
            this.stopHandler();
            Event.observe(this.domElement, "click", this.handler);
            this.active = true;
        //}
      },
      
      stopHandler: function () {
        if (this.active) {
          if(this.domElement){
          Event.stopObserving(this.domElement, 'click');
          this.active = false;
          }
        }
      },
    
      checkStartStopCondition: function () {
        //alert(this.handler + " - " + this.domElement + " - " + this.enabled);
        if (this.handler && this.domElement && this.enabled) {
          this.startHandler();
        } else {
          this.stopHandler();
        }
      },
    
      applyState: function () {
        this.checkStartStopCondition();
      },
        
      enable: function () {
        this.enabled = true;
        this.checkStartStopCondition();
      },
    
      disable: function () {
        this.enabled = false;
        this.checkStartStopCondition();
      },
    
      setHandler: function (newHandler) {
        this.handler = newHandler;
        this.checkStartStopCondition();
      },
    
      setDomElement: function (newDomElement) {
        this.domElement = newDomElement;
        this.applyState();
      },
        
      remove: function () {
        this.domElement.remove();
      },
        
      isEnabled: function () {
        return this.enabled;
      },
        
      getDomElement: function (document) {
        if (!this.domElement) {
          this.domElement = document.createElement(this.domElementName);
          $(this.domElement);
          
          if (this.domId) {
            this.domElement.id = this.domId;
          }
           
          this.applyState();
        }
        return this.domElement;
      }
    });

  2. #2
    non ti basta sapere cosa fa il tutto a scatola chiusa (idea dietro il linguaggio a oggetti)?

    Quando fai un:

    Codice PHP:
    var btn = new buttons.Button({
       
    handler MyClickFunct//MyClickFunct è una funzione definita come sotto
       
    domElement : $('ButtonID'), //$(string) è una funzione di PrototypeJS, una libreria di JS, che ritorna l'elemento DOM della pagina (il tag HTML) con id = alla stringa passata
       
    enabled true //Setto l'elemento abilitato
    });

    //Questa è la funzione che voglio applicare al click del buttone
    function MyClickFunct(){
       
    alert("I've clicked the button with ID "+this.id);

    a questo punto in btn c'è l'istanza del bottone che gestisce il bottone che gli hai passato in domElement.

    Questo oggetto offre alcuni metodi, a te penso interessino:
    Codice PHP:
    btn.enable(); //Abilita l'elemento ed attiva la funzione sul click
    btn.disable(); //Disabilita l'elemento e disattiva la funzione sul click
    btn.setHandler(MyNewFunct); //Cambia la funzione da applicare al click sull'elemento
    btn.setDomElement($('AltroButtonID')); //Cambia l'elemento che l'oggetto controlla e riapplica la funzione al click. 
    //N.B: non c'è nessun controllo sull'elemento precedente il quale resta nella pagina con la funzione al click applicata. Penso sia stato creato per essere usato dopo aver chiamato il metodo remove()
    btn.remove(); //Rimuove l'elemento dal DOM della pagina.
    btn.isEnabled(); //Ritorna true se l'elemento è abilitato, false altrimenti 
    secondo me gli altri metodi non ti servono perché sono ad uso interno, anche se non sono privati.
    I DON'T Double Click!

  3. #3
    Utente di HTML.it
    Registrato dal
    Jan 2009
    Messaggi
    48
    ti ringrazio con le info che mi hai postato sono riuscito a capire in linea di massima il concetto base che ci sta dietro.... ora è tutta una strada in discesa:P

    grazie


    grazie


    grazie

    !

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.