Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > de.comp.lang.javascript > #4943

Re: not a function

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups de.comp.lang.javascript
Subject Re: not a function
Date 2018-04-25 21:04 +0200
Organization PointedEars Software (PES)
Message-ID <5377515.lOV4Wx5bFT@PointedEars.de> (permalink)
References <pbppha$jg2$1@news.albasani.net>

Show all headers | View raw


Jan Novak wrote:

> Ich habe folgenden JS Code aus bootstrap-treeview übernommen:

Nein, hast Du _nicht_, sondern Du hast *wieder* ohne Sinn und Verstand
Copy & Pray gemacht :-(

<https://github.com/jonmiles/bootstrap-treeview/blob/542f57eab636e14417216e95fa7f7049bbf3d69f/public/index.html#L567-L579>
 
> var initSelectableTree = $.getJSON(/include/fetch.php', function(data) {
                                     ^
>         return $('#treeview-selectable').treeview({
>          data: data,
>          multiSelect: multiSelect,
>          onNodeSelected: function(event, node) {
>            $('#selectable-output').prepend('<p>' + node.text + ' was
> selected</p>');
>          },
>          onNodeUnselected: function (event, node) {
>            $('#selectable-output').prepend('<p>' + node.text + ' was
> unselected</p>');
>          }
>        });
>      });
> 
> 
> wenn ich nun
> 
>       var $selectableTree = initSelectableTree();
> 
> aufrufe, dann erhalte ich den Fehler
> 
> Uncaught TypeError: initSelectableTree is not a function

Vorher solltest Du auch schon einen Syntaxfehler erhalten.
 
> Was fehlt denn da noch?

Dir ist trotz des vorherigen Threads, den Du vor anderthalb Monaten hier 
angefangen hattest, offenbar *immer* *noch* *nicht* klar, wie *asynchrone* 
Verarbeitung funktioniert, und wie sie sich von *synchroner* Verarbeitung 
unterscheidet.

  function (data) { … }

als zweites Argument von $.getJSON() ist offensichtlich ein *Callback*, der 
ausgeführt wird, wenn der Server auf den HTTP-Request (URI angegeben durch 
das erste Argument) *geantwortet* hat.

Die Antwort vom Server trifft in der Regel ein, *nachdem* $.getJSON() 
ausgeführt wurde *und* *zurückgekehrt* ist.  Der Rückgabewert des Callbacks 
landet also _nicht_ in “initSelectableTree”, sondern wird (in der Regel) 
*weggeworfen*.

Und der Rückgabewert von jQuery.getJSON() ist, wie Du hättest nachlesen 
können, _nicht_ ausführbar (callable), sondern ein Objekt, was das Promise-
Interface implementiert (ab jQuery 1.5; vorher wohl der “undefined”-Wert):

<http://api.jquery.com/jquery.getjson/>

Ich mal’s Dir nochmal als UML-Sequenzdiagramm auf (Festbreitenschriftart 
verwenden).

Du willst folgendes machen:
  
  .---------------- Client --------------.
  :                                      :
                                          
   Script                   HTTP API                               Server
     :                         :                                     :
    .-.       $.getJSON()      :                                     :
    : :---------------------->.-.  GET /include/fetch.php HTTP/1.x   :
    : :                       : :---------------------------------->.-.
    : :  undefined/:: Promise : :                                   : :
    : :<- - - - - - - - - - --'-'                                   : :
    : :                                                             : :
    : :      DOM API    function (data)       HTTP/1.x 200 OK       : :
    : :         :             .-.<- - - - - - - - - - - - - - - - --'-'
    : :        .-.<-----------: :                                    :
    : :        : :            : :                                    :
    : :        '-'- - - - - ->: :                                    :
    : :         :             '-'                                    :
    '-'         :              :                                     :
     :          :              X                                     :
     X          :                                                    :

Demgegenüber machen die Bootstrap-Leute im Originalcode etwas *völlig* 
*anderes*:

  .---------------- Client --------------.         *kein* Server
  :                                      :
                                          
     :
    .-.               
    : :  (function () { … }())
    : :----------------------->.-.
    : :                        : :
    : :<- - - - - - - - - - - -'-'
    : :
    : :  initSelectableTree()
    : :----------------------->.-.
    : :                        : :
    : :<- - - - - - - - - - - -'-'
    : :
    '-'
     :
     X
    
(Schau Dir mal an, wo “defaultData” herkommt.)


Jetzt klar?

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.

Back to de.comp.lang.javascript | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

not a function Jan Novak <repcom@gmail.com> - 2018-04-25 13:41 +0200
  Re: not a function Sascha Hüdepohl <news@juenger-der-himmlischen-teekanne.de> - 2018-04-25 17:49 +0200
    Re: not a function Jan Novak <repcom@gmail.com> - 2018-04-26 07:39 +0200
  Re: not a function Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-04-25 21:04 +0200
    Re: not a function Jan Novak <repcom@gmail.com> - 2018-04-26 07:55 +0200
      Re: not a function "Peter J. Holzer" <hjp-usenet3@hjp.at> - 2018-04-28 19:50 +0200
      Re: not a function Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-04-29 01:14 +0200
    Re: not a function Jan Novak <repcom@gmail.com> - 2018-04-26 08:07 +0200
      Re: not a function Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-04-29 01:30 +0200
        Re: not a function Jan Novak <repcom@gmail.com> - 2018-05-02 15:06 +0200
          Re: not a function Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-05-05 01:00 +0100

csiph-web