jquery – 如何在requirejs中使用knockout加载knockout.validation

前端之家收集整理的这篇文章主要介绍了jquery – 如何在requirejs中使用knockout加载knockout.validation前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我将在require js中定义我的模型,我需要在模块中使用 knockoutknockout validation plugin以及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);
   }           
 });

但是当我运行这个时,我得到了以下错误.

Uncaught ReferenceError: ko is not defined

我也尝试调试,发现所有其他librariesknockout,jquery正在加载完美.

这是我的配置部分

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

解决方法

你的模型对我很好,这是我的require配置:
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",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','UAE']);
    self.selectedCountry = ko.observable();
    self.errors = ko.validation.group(self);
  };           
});

jQuery defines named AMD module ‘jquery’ (all lower case) when it detects AMD/RequireJS.开始,你不再需要require-jquery了

原文链接:https://www.f2er.com/jquery/180672.html

猜你在找的jQuery相关文章