本文介绍了如何在requirejs中使用knockout加载knockout.validation的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我将在require js中定义我的模型,并且我需要淘汰赛和,还有jquery.
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
我还尝试调试,发现所有其他库knockout
,jquery
都正在完美加载.
i also try to debug and found that all other librariesknockout
, jquery
are loading perfectly .
这是我的配置部分
require.config({
baseUrl: "/Scripts",
paths: {
"Signup" : "Signup",
"knockout": "knockout-2.3.0",
"knockout.validation": "knockout.validation",
"jquery": "require-jquery"
}
});
推荐答案
您的模型可以正常使用,这是我的require config:
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
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);
};
});
,您不再需要require-jquery
,因为 jQuery定义了名为AMD模块'jquery' (全部小写),当它检测到AMD/RequireJS时.
这篇关于如何在requirejs中使用knockout加载knockout.validation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!