2

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

// 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 的行为是否与其他插件不同?

4

2 回答 2

4

这是一个敲除验证和 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         
    }
});
于 2014-02-22T18:13:18.893 回答
2

正如我在评论中提到的,淘汰验证目前还不能很好地与 require.js 一起使用。Durandal 在开发过程中依赖 require.js,直到您优化您的应用程序。

此链接详细介绍了目前的解决方法。

http://github.com/Knockout-Contrib/Knockout-Validation/issues/259

于 2013-08-03T17:43:18.453 回答