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 に答える