Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load knockout.validation with knockout in requirejs

I am going to define my model in require js and i need knockout and knockout validation plugin in my module and also jquery .

define(["knockout","jquery","knockout.validation"], function (ko,$,validation) {
  // knockout model here with some knockout validation 

    return function SignUpViewModel() {
    var self = this;
    self.name = ko.observable();
    self.email = ko.observable().extend({ required: true });
    self.password = ko.observable().extend({
       required: true,
       minLength: 6
   });
   self.confirmPassword = ko.observable().extend({ mustEqual: self.password() });
   self.company = ko.observable();
   self.availableCountries = ko.observableArray(['Pakistan', 'USA', 'Egypt', 'UAE']);
   self.selectedCountry = ko.observable();
   self.errors = ko.validation.group(self);
   }           
 });

But when i run this i got the following error .

Uncaught ReferenceError: ko is not defined

i also try to debug and found that all other librariesknockout, jquery are loading perfectly .

and here is my config portion

require.config({
  baseUrl: "/Scripts",
  paths: {
    "Signup" : "Signup",
    "knockout": "knockout-2.3.0",
    "knockout.validation": "knockout.validation",
    "jquery": "require-jquery"
    }
});
like image 668
Ancient Avatar asked Aug 06 '13 06:08

Ancient


2 Answers

Your model works fine with me, here's my require config:

requirejs.config({
  baseUrl: '/Scripts',
  paths: {
    'jquery': 'jquery-1.9.1.min',
    'knockout' : 'knockout-2.3.0',
  }
});
// myModel.js is the file containing your model code.
require( ["myModel", "knockout"], function(model, ko){
  ko.applyBindings(new model());
});

myModel.js

define(["knockout","jquery","knockout.validation"], function (ko,$,validation) {
// knockout model here with some knockout validation 

  return function SignUpViewModel() {
    var self = this;
    self.name = ko.observable();
    self.email = ko.observable().extend({ required: true });
    self.password = ko.observable().extend({
       required: true,
       minLength: 6
    });
    self.confirmPassword = ko.observable().extend({ mustEqual: self.password() });
    self.company = ko.observable();
    self.availableCountries = ko.observableArray(['Pakistan', 'USA', 'Egypt', 'UAE']);
    self.selectedCountry = ko.observable();
    self.errors = ko.validation.group(self);
  };           
});

and you don't need require-jquery anymore, since jQuery defines named AMD module 'jquery' (all lower case) when it detects AMD/RequireJS.

like image 102
Paul Valla Avatar answered Oct 04 '22 06:10

Paul Valla


require.config({
    baseUrl: "/Scripts",
    paths: {
        "Signup": "Signup",
        "knockout": "knockout-2.3.0",
        "knockout.validation": "knockout.validation",
        "jquery": "require-jquery"
    },
    shim: {
        "knockout.validation": {
            "deps": ["knockout"]
        }
    }
});

http://www.requirejs.org/docs/api.html#config-shim

like image 37
jgillich Avatar answered Oct 04 '22 08:10

jgillich