我将在require js中定义我的模型,我需要在模块中使用
knockout和
knockout 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了