Knockout 2.2.0 error with jQuery 1.9
Asked Answered
S

2

14

I copied one of examples of knockoutjs:

    <!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>

When I updated jQuery to Version 1.9, I'm got following error:

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

I'd appreciate it if someone could explain if the bug is in jQuery or KO.

Screwy answered 23/1, 2013 at 9:1 Comment(7)
Try updating Knockout to 2.2.1.Formal
Did this previously work with an earlier version of jQuery?Intrusive
It seems it works with KO 2.2.1 and jQuery 1.9.0 jsfiddle.net/MExgFDwt
Possible dup of #11023329Intrusive
right, now i see it is working with 2.2.1, anyway still where is the bug not clearScrewy
The error was caused by Knockout. See my updated answer below.Contingency
I had the same issue. Upgrading to Knockout 2.2.1 solved it.Examine
C
28

The Cause

You are not using the most current version of Knockout. The previous version, 2.2.0, is incompatible with jQuery 1.9.x and on. See this Knockout dev thread:

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

This means that Knockout 2.2.0 is calling an undefined jQuery method, thus triggering the JS error you specified.

Solutions

  1. Consider updating to the latest version of Knockout which is compatible with jQuery 1.9
  2. If you can't, use the jQuery Migrate plugin which adds backward-compatibility to jQuery 1.9
  3. If all else fails, you'll need to revert back to jQuery 1.8
Contingency answered 23/1, 2013 at 9:10 Comment(3)
Confused... I thought Knockout had no dependencies and was pure JavaScript?Inhere
@FooBar Knockout is indeed not depended on jQuery. However, it does include jQuery-specific code, in order to make it easier to use the two libraries together. One such outdated code snippet was included in Knockout 2.2.0 and generated an error when Knockout was used with jQuery. See the 2.2.0 source (line 706).Contingency
Updated link to the 2.2.0 source.Contingency
F
8

Updating Knockout to 2.2.1 solves the problem for me:

So just change:

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

To:

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

And it'll work.

Formal answered 23/1, 2013 at 9:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.