gpt4 book ai didi

backbone.js - 如何在 Marionette.js 中使用 Backbone.Validation 插件

转载 作者:行者123 更新时间:2023-12-02 01:54:11 24 4
gpt4 key购买 nike

我在 Marionette.js 应用程序中使用 Twitter Bootstrap3 和 Backone.Validation( http://thedersen.com/projects/backbone-validation/) 插件,但有些方法根本无法正确使用。
任何人都可以举一个简单的例子来说明如何在 Marionette.js 中使用 Backbone.Validation
(示例类似于 http://thedersen.com/projects/backbone-validation/#examples 和 _http://jsfiddle.net/thedersen/c3kK2/)

更新:
我有以下编码,我希望一旦提交表单就会触发验证,但不幸的是什么也没发生,甚至没有错误。
在我的 app.js(全局)中

//Backbone.Validation
Backbone.Validation.configure({
forceUpdate: true
});

_.extend(Backbone.Validation.callbacks, {
valid: function (view, attr, selector) {
var $el = view.$('[name=' + attr + ']'),
$group = $el.closest('.form-group');

$group.removeClass('has-error');
$group.find('.help-block').html('').addClass('hidden');
},
invalid: function (view, attr, error, selector) {
var $el = view.$('[name=' + attr + ']'),
$group = $el.closest('.form-group');

$group.addClass('has-error');
$group.find('.help-block').html(error).removeClass('hidden');
}
});

我的模板如下所示:
<script id="signup-form" type='text/template'>
<form class="form-signin control-group">
<table>
<tr><th><h2 class="form-signin-heading">Please sign up</h2></th></tr>
<tr><td><input type="text" class="form-control" name="username" placeholder="Email address"></td></tr>
<tr><td><input type="password" class="form-control" name="password" placeholder="Password"></td></tr>
<tr><td><button class="btn btn-success form-control js-submit">Sign up</button></td></tr>
</table>
</form>

我的模型看起来像这样:
Entities.User = Backbone.Model.extend({
urlRoot: "signup",

defaults: {

},

idAttribute: "_id",

validation: {
email: {
required: true,
pattern: 'email'
},
password: {
minLength: 8
}
},
validate:true
});

我的观点是这样的
Show.SignupPanel = Marionette.ItemView.extend({

template: "#signup-form",


events: {
'click button.js-submit': 'signupClicked'
},

signupClicked: function (e) {
//stop the default action of <a> tag and page refresh
e.preventDefault();
var data = Backbone.Syphon.serialize(this);
if(this.model.isValid(true))
this.trigger("form:submit", data);
},

initialize: function () {
// This hooks up the validation
// See: http://thedersen.com/projects/backbone-validation/#using-form-model-validation/validation-binding
Backbone.Validation.bind(this);
},

remove: function () {
// Remove the validation binding
// See: http://thedersen.com/projects/backbone-validation/#using-form-model-validation/unbinding
Backbone.Validation.unbind(this);
return Backbone.View.prototype.remove.apply(this, arguments);
}

});

最佳答案

关于backbone.js - 如何在 Marionette.js 中使用 Backbone.Validation 插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21117372/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com