knockout.js - 在 Durandal/SPA 应用程序中配置 Knockout 验证

标签 knockout.js requirejs single-page-application durandal knockout-validation

我无法使用 durandal 启动和运行 knockout.validation。 knockout 本身工作正常。如果有人明白这一点,我将不胜感激发布配置。

// main.js
require.config({
    paths: {
    "libs": "../scripts",
    "knockout": "../scripts/knockout-2.2.1",
    'knockout.validation': '../scripts/knockout.validation'
},
shim: {
    'knockout.validation': {
        deps: ["knockout"]
    }
}
});

define(function (require) {
validation = require('libs/knockout.validation')
// other dependencies are omitted
ko.validation = validation;
// ko works fine
// ko validation has been set
}

// my viewmodel
define(['services/logger',
'durandal/app',
'durandal/system',
'durandal/plugins/router',
'services/dataservice'],
function (logger, app, system, router, dataservice) {

var user_name = ko.observable().extend( {required: true } );
var user_password = ko.observable().extend( {required: true });

// Unable to get property 'extend' of undefined or null reference
// the same happens, if I define '/libs/knockout.validation locally

kockout.validation 的行为是否与其他插件不同?

最佳答案

这是一个 knockout validation 和 durandal vm 的工作示例;

define(['plugins/router', 'durandal/app', 'config'],function (router, app, config) {

    ko.validation.init({
        messagesOnModified: false
    });
    ko.validation.registerExtenders();
    var email = ko.observable().extend({
        required: { message: 'You must enter a valid email id' },
        email: { message: 'Email address you entered is not valid' }
    });
    var password = ko.observable().extend({
        required: { message: 'Enter password, minimum of 6 characters' },
        minLength: 6
    });

    var vm = {
        activate: activate,
        attached: attached,
        router: router,
        email: email,
        password: password,
        signIn: signIn,
        viewUrl : 'signin/index.html'
    };

    vm.errors = ko.validation.group(vm);
    return vm;

    function activate(id, querystring) {}

    function attached(view, parent) { }

    function signIn() {
        if (!vm.isValid()) {
            vm.errors.showAllMessages(true);
            return false;
        }
        //rest of logic         
    }
});

关于knockout.js - 在 Durandal/SPA 应用程序中配置 Knockout 验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18033268/

相关文章:

javascript - 是否可以在新的 React 网站的 iframe 中嵌入旧的 JS 应用程序

javascript - 有没有办法只在需要的时候加载 JS 和 CSS?

javascript - 我可以使用 react-router 为 github-pages 站点创建路由吗?

javascript - knockout 不评估 IE7 中的表达式

javascript - knockoutjs中的MVVM在哪里?

javascript - 如何获取模板 html 文本

javascript - 多文件 JavaScript 库

javascript - RequireJS:多个 main.js?

node.js - Angular 形式组 patchValue 将值包裹在大括号中

knockout.js - MVCContrib 网格 - 我可以指定 tbody 属性吗?