Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Wywoływanie własnej metody wewnątrz klasy
Yorki
post 13.02.2012, 01:44:20
Post #1





Grupa: Zarejestrowani
Postów: 45
Pomógł: 2
Dołączył: 12.09.2005

Ostrzeżenie: (0%)
-----


Witam, chcę zrobić sobie klasę popup do której będę ładował odpowiedni adres i wyświetlał na swojej stronie, oto kod:

Kod
function Popup(){
    this.html = '';
    this.title = '';
    this.resize = false;
    this.width = 400;
    
    this.init = function(){
        var bar = $('<div class="popup-bar"></div>');
        var close = $('<div class="popup-close">x</div>').click( function() {
                $(this).parent().parent().remove();
            });
        var title = $('<div class="popup-title">'+this.title+'</div>');
        var content = $('<div class="popup-content">'+this.html+'</div>');
        bar.append(close).append(title);
        
        var win = $('<div class="popup-window"></div>');
        win.append(bar);
        win.append(content);        
        $('body').append(win);
    };
    
    this.url = function(url){
        $.get(url,
            function(data){
                this.html = data;
                this.init();
            });        
    }
}


Oraz wywołanie :

Kod
$(document).ready( function(){
    pop = new Popup();
    pop.url('/');
});


Dostaję błąd, że obiekt nie ma takiej metody.

Kod
Uncaught TypeError: Object #<Object> has no method 'init'


Co zrobiłem źle? Dodam, że wcześniej nie bawiłem się w klasy pisząc skrypty smile.gif

Ten post edytował Yorki 13.02.2012, 01:47:29
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 2)
wookieb
post 13.02.2012, 08:23:12
Post #2





Grupa: Moderatorzy
Postów: 8 989
Pomógł: 1550
Dołączył: 8.08.2008
Skąd: Słupsk/Gdańsk




[JAVASCRIPT] pobierz, plaintext
  1. this.url = function(url){
  2. var here = this;
  3. $.get(url,
  4. function(data){
  5. here.html(data);
  6. here.init();
  7. });
  8. }
[JAVASCRIPT] pobierz, plaintext


W function jest inny kontekst "this"


--------------------
Go to the top of the page
+Quote Post
Yorki
post 13.02.2012, 13:54:51
Post #3





Grupa: Zarejestrowani
Postów: 45
Pomógł: 2
Dołączył: 12.09.2005

Ostrzeżenie: (0%)
-----


Cytat(wookieb @ 13.02.2012, 08:23:12 ) *
[JAVASCRIPT] pobierz, plaintext
  1. this.url = function(url){
  2. var here = this;
  3. $.get(url,
  4. function(data){
  5. here.html(data);
  6. here.init();
  7. });
  8. }
[JAVASCRIPT] pobierz, plaintext


W function jest inny kontekst "this"


Racja.. przeoczyłem to wink.gif
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 19.07.2025 - 10:24