Javascript 主干同步后集合视图中的触发器功能
我正在主干网中构建一个应用程序。在我的收集模型中,我有一个同步功能,当用户单击按钮时触发。它向服务器提交一些表单数据,服务器返回一个包含成功或错误消息的JSON对象 问题在于,当集合模型同步时,我似乎无法在集合视图中触发事件: 以下是完整的脚本:Javascript 主干同步后集合视图中的触发器功能,javascript,jquery,ajax,backbone.js,Javascript,Jquery,Ajax,Backbone.js,我正在主干网中构建一个应用程序。在我的收集模型中,我有一个同步功能,当用户单击按钮时触发。它向服务器提交一些表单数据,服务器返回一个包含成功或错误消息的JSON对象 问题在于,当集合模型同步时,我似乎无法在集合视图中触发事件: 以下是完整的脚本: var RegisterModel = Backbone.Model.extend({url: 'api/process.php', defaults: { contentType: 'input', value: '', inputType: '
var RegisterModel = Backbone.Model.extend({url: 'api/process.php', defaults: { contentType: 'input', value: '', inputType: 'text', inputClass: 'required', serverResponseClass: 'alert alert-error', message: ' ', responseType: ' '} });
var RegisterView = Backbone.View.extend({
template: _.template('<p><% if (contentType == "input") { %><input placeholder="<%= label %>" name="<%= inputName %>" id="<%= inputName %>" value="<%= value %>" type="<%= inputType %>" class="<%= inputClass %>"/><% } %><% if (contentType == "button") {%> <input type="<%= inputName %>" class="<%= btnClass %>" id="<%= inputName %>"<% } %></p>'),
render: function() {
this.$el.html(this.template(this.model.toJSON()));
return this;
}
});
var RegisterCollection = Backbone.Collection.extend({
model: RegisterModel,
url: 'api/process.php',
updateModels: function (){
this.forEach(this.updateModel, this);
},
updateModel: function(registerModel) {
var inputName = '#'+registerModel.get('inputName');
var userInput = $(inputName).val();
registerModel.set({value: userInput});
},
clearErrors: function() {
var musketeers = this.where({contentType: "serverResponse"});
this.remove(musketeers);
},
syncCollection: function() {
Backbone.sync.call(this, 'create', this);
}
});
var RegisterCollectionView = Backbone.View.extend({
initialize: function(){
this.collection.on('reset', this.addAll, this);
this.collection.on('add', this.addAll, this);
this.collection.on('sync', this.onModelSaved);
/* this.collection.on('remove', function(){registerCollection.fetch(); }, this); */
},
el: "#backboneContainer",
events: {
"click #submit" : "validate",
},
validate: function(e) {
e.preventDefault();
if ($("#register").valid() === true) {
this.collection.clearErrors();
this.collection.updateModels();
this.collection.syncCollection();
}
},
errorOverlay: function(errorTitle, errorContent) {
$.notification({
title: errorTitle,
content: errorContent,
timeout: 5000,
icon: "!",
error: true,
border: false
});
},
onModelSaved: function () {
console.log('hello world!');
alert('hello world!');
},
successMessage: function(message) {
$.fn.modal({
layout: "elastic",
url: undefined,
content: message,
padding: "50px",
animation: "fadeInDown"
});
this.$el.append('<span class="icon">=</span>');
},
addOne: function(registerModel){
var registerView = new RegisterView({model: registerModel});
this.$el.append(registerView.render().el);
},
buildForm: function(registerModel) {
var registerView = new RegisterView({model: registerModel});
$('#register').append(registerView.render().el);
},
addAll: function(){
this.$el.empty();
this.$el.append('<div class="con"><div class="section current" title="Our First Section" ><div class="col_12"><h1>Piecharter.com</h1></div><div class="carton col_4"><h2>Registration</h2><div class="content"><form id="register" method="post"></form></div></div></div></div>');
this.collection.forEach(this.buildForm, this);
$("#register").validate();
/* this.$el.append('); */
},
render: function(){
this.addAll();
return this;
}
});
$('.modal').live("click", function () {
this.remove();
$('#overlays').hide();
});
/*VALIDATION HERE ~*/
jQuery.validator.addClassRules("username", {
minlength: 7,
maxlength: 21
});
jQuery.validator.addMethod("password", function( value, element ) {
var result = this.optional(element) || value.length >= 1 && /\d/.test(value) && /[a-z]/i.test(value);
if (!result) {
var validator = this;
setTimeout(function() {
validator.blockFocusCleanup = true;
element.focus();
validator.blockFocusCleanup = false;
}, 1);
}
return result;
}, "Your password must be at least 7 characters long and contain at least one number and one character.");
var RegisterModel=Backbone.Model.extend({url:'api/process.php',默认值:{contentType:'input',value:'',inputType:'text',inputClass:'required',serverResponseClass:'alert-alert error',消息:'',responseType:'}});
var RegisterView=Backbone.View.extend({
模板:u.template('=1&&/\d/.test(value)&/[a-z]/i.test(value);
如果(!结果){
var验证器=此;
setTimeout(函数(){
validator.blockFocusCleanup=true;
元素focus();
validator.blockFocusCleanup=false;
}, 1);
}
返回结果;
},“您的密码长度必须至少为7个字符,并且至少包含一个数字和一个字符。”);
我知道集合正在成功同步,所以问题一定是我在集合视图中侦听同步事件的方式有问题。有什么问题吗?我已经查看了文档,没有发现我构建此集合的方式有任何问题。您在尝试绑定时遗漏了
此
“onModelSaved”,因此您没有引用对象的“onModelSaved”方法,而是引用了全局“onModelSaved”变量(几乎可以肯定它不存在)
尝试:
你必须更详细地描述这个错误对我(或任何其他人)来说是什么,才能帮助你。理想情况下,你应该把这个新错误放在一篇新的帖子中,因为这确实是一个新的和不同的问题(对未来的读者来说,提出一个新问题比在这里塞进两个问题要容易得多)。控制台中没有错误。该方法不会触发。因此,即使使用
this.collection.on('sync',this.onModelSaved);
也不会收到“hello world!”警报?如果向syncCollection
添加警报,它是否会触发?是的,当我直接从syncCollection调用它们时,会触发警报。
this.collection.on('sync', this.onModelSaved);