2013-01-23 11 views
13

ho copiato uno dei esempi di knockoutjs:Knockout errore 2.2.0 con jQuery 1.9

<!DOCTYPE html> 
    <html> 
    <head> 
     <script src="http://code.jquery.com/jquery-1.8.0.min.js"></script> 
    <script src="http://cdnjs.cloudflare.com/ajax/libs/knockout/2.2.0/knockout-min.js"></script> 

    <meta charset=utf-8 /> 
    <title>JS Bin</title> 

    </head> 
    <body> 


    <h2>Participants</h2> 
    Here are the participants: 
    <div data-bind="template: { name: 'person-template', data: buyer }"></div> 
    <div data-bind="template: { name: 'person-template', data: seller }"></div> 





    <script id="person-template" type="text/html"> 
     <h3 data-bind="text: name"></h3> 
     <p>Credits: <span data-bind="text: credits"></span></p> 
    </script> 

    <script type="text/javascript"> 
     function MyViewModel() { 
      this.buyer = { name: 'Franklin', credits: 250 }; 
      this.seller = { name: 'Mario', credits: 5800 }; 
     } 
     ko.applyBindings(new MyViewModel()); 
    </script> 
    </html> 

Quando ho aggiornato jQuery per Versione 1.9, sto ottenuto seguente errore:

Uncaught TypeError: Object function (e,t){return new st.fn.init(e,t,X)} has no method 'clean' 

Sarei grato se qualcuno potesse spiegare se il bug si trova in jQuery o KO.

+5

Provare ad aggiornare Knockout su '2.2.1'. – Blender

+0

Questo precedentemente funzionava con una versione precedente di jQuery? – halfer

+0

Sembra che funzioni con KO 2.2.1 e jQuery 1.9.0 http://jsfiddle.net/MExgF/ – nemesv

risposta

28

La Causa

Non si sta utilizzando la versione più recente di eliminazione diretta. La versione precedente, 2.2.0, non è compatibile con jQuery 1.9.xe su. Vedi this Knockout dev thread:

Knockout 2.2.0 uses jQuery.clean() which is deprecated and does not exist in 1.9.

Questo significa che Knockout 2.2.0 sta chiamando un metodo di jQuery non definito, innescando così l'errore JS specificato.

Solutions

  1. Considerare l'aggiornamento a the latest version of Knockout che è compatibile con jQuery 1.9
  2. Se non è possibile, utilizzare il jQuery Migrate plugin che aggiunge all'indietro-compatibilità di jQuery 1.9
  3. Se tutto il resto fallisce, è necessario tornare a jQuery 1.8
+1

Confuso ... pensavo che Knockout non avesse dipendenze ed era puro JavaScript? – SimonGates

+1

@FooBar Knockout non è effettivamente dipendente da jQuery.Tuttavia, include il codice specifico di jQuery, al fine di semplificare l'uso delle due librerie. Uno di questi snippet di codice obsoleto è stato incluso in Knockout 2.2.0 e ha generato un errore quando Knockout è stato utilizzato con jQuery. Vedi [fonte 2.2.0] (https://github.com/SteveSanderson/knockout/blob/gh-pages-2.2.0/js/knockout-2.2.0.debug.js) (riga 706). – Boaz

+0

Vedo, grazie boaz – SimonGates

8

Aggiornamento Knockout al 2.2.1 risolve il problema per me:

Quindi, basta cambiare:

<script src="http://cdnjs.cloudflare.com/ajax/libs/knockout/2.2.0/knockout-min.js"></script> 

A:

<script src="http://cdnjs.cloudflare.com/ajax/libs/knockout/2.2.1/knockout-min.js"></script> 

E che funzionerà.

Problemi correlati