Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.4k views
in Technique[技术] by (71.8m points)

jquery - 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"
    }
});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...