Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempting to use with require.js causes "ko is not defined" #259

Closed
colin-young opened this issue Apr 5, 2013 · 8 comments
Closed

Attempting to use with require.js causes "ko is not defined" #259

colin-young opened this issue Apr 5, 2013 · 8 comments
Labels
Milestone

Comments

@colin-young
Copy link

I'm configuring require like so:

paths: {
    blockui: 'jquery.blockUI',
    jquery: 'jquery-1.9.1.min',
    jqueryui: 'jquery-ui-1.10.1.min',
    knockout: 'knockout-2.2.1',
    komapping: 'knockout.mapping-latest',
    validation: 'knockout.validation',
    token: 'jquery.tokeninput'
}

But when I later use the validation:

define(['jquery', 'knockout', 'komapping', 'Views/add-report-wizard-model', 'token', 'validation'], function ($, ko, mapping, ViewModel, token, validation) { ... });

It fails with "ko is not defined." Is there an example somewhere of how I need to reference knockout.validation with require.js?

@stevegreatrex
Copy link
Contributor

Unfortunately knockout validation doesn't play well with requirejs at the moment. The problem is that knockout will use requirejs and so doesn't set the global window.ko property on which knockout validation relies.

You can work around this for the time being using the pattern below, but it isn't particularly nice.

require.config({
    baseUrl: "/scripts",
    paths: {
        "knockout": "knockout-2.2.1",
        "knockout.validation": "knockout.validation"
    },
    shim: {
        "knockout.validation": ["knockout"]
    }
});

require(["knockout"], function (ko) {
    //manually set the global ko property
    window.ko = ko;

    //then bring in knockout validation
    require(["knockout.validation"], function() {
        var ViewModel = function () {
            var self = this;

            self.field = ko.observable("Required Value").extend({ required: true });
        };

        ko.applyBindings(new ViewModel());
    });
});

We probably should support requirejs going forward though...

@colin-young
Copy link
Author

I'm already setting some global stuff for other reasons, so it's not too bad for me. Thanks for the speedy response.

@dotnetwise
Copy link

Bummer.

@dealproc
Copy link

is this in a release?

@stevegreatrex
Copy link
Contributor

not in a release, but if you grab the source from GitHub then requirejs is properly supported

@dealproc
Copy link

gotcha.

@crissdev crissdev added this to the v2.0 milestone Nov 17, 2014
@crissdev crissdev removed the todo label Dec 1, 2014
@DenEwout
Copy link

Hi, I'm experiencing some difficulties regarding this. Any update on a fix without using the workaround above?

@crissdev
Copy link
Member

@DenEwout Please ensure you're using the correct version (at least 2.0.0) and your requiresjs configuration is properly set up. If you still think there's an issue in the library then reply with an example on plnk.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants