[英]Repeating issue in Parse.com - JavaScript SDK
每次我創建一個“消息”解析對象時,它都會在我的Parse Core中復制該對象。 太奇怪了 第一次運行代碼時,一切都很好,並且Parse僅創建一個對象。 但是,當我再次運行代碼時,它將復制最近的對象兩次。 如果我第三次運行它,它將復制最近的對象五次。 復制數量會根據已創建的對象數量而增加。 有誰知道如何確保它在我的Parse Core后端中創建一個對象? 非常感謝!!! 我希望可以發布圖片,但是我是新手,所以stackoverflow不會讓我
App.Models.Message = Parse.Object.extend({
className: 'Message',
idAttribute: 'objectId',
defaults: {
name : '',
email : '',
subject : '',
message : ''
}
});
App.Views.Contact = Parse.View.extend({
el : '#middle',
template : _.template($('#contactTemp').html()),
events: {
'click .submit' : 'submit',
},
initialize : function () {
this.render();
},
render : function () {
this.$el.html(this.template);
},
submit : function (e) {
e.preventDefault();
var message = new App.Models.Message({
name: $('.nameVal').val(),
email: $('.emailVal').val(),
subject: $('.subVal').val(),
message:$('.messVal').val(),
});
message.save(null, {
success:function() {
console.log("Success");
},
error:function(e) {
alert('There was an error in sending the message');
}
});
}
});
是! 因此,我在Parse Developers Google小組的Hector Ramos的幫助下找出了問題所在。 https://groups.google.com/forum/#!topic/parse-developers/2y-mI4TgpLc
這是我的客戶端代碼。 而不是創建附加到我App.Views.Contact()的事件; 又名-Parse.View.extend({})的實例,我繼續使用最近定義的sendMessage函數中的jquery創建了一個'click'事件。 如果您在Parse視圖內的events對象中聲明一個事件,則如果該視圖未重新初始化或銷毀並正確創建,該事件將自行重現。
因此,我所發生的事情是我在事件對象中聲明的submit函數不斷進行自我檢查並重復調用Parse.com。 我的視圖是靜態的,沒有被正確銷毀,重新初始化或重新加載。 您將在下面看到我的操作:
本來我有這個:
events: {
'click .submit' : 'submit',
},
& 這個
submit : function (e) {
e.preventDefault();
var message = new App.Models.Message({
name: $('.nameVal').val(),
email: $('.emailVal').val(),
subject: $('.subVal').val(),
message:$('.messVal').val(),
});
message.save(null, {
success:function() {
console.log("Success");
},
error:function(e) {
alert('There was an error in sending the message');
}
});
} /*end of submit*/
現在,我已經完全刪除了我擁有的事件對象,並聲明了sendMessage函數:
initialize : function () {
this.render();
},
render : function () {
this.$el.html(this.template);
this.sendMessage();
},
sendMessage : function () {
$('.submit').on('click', function(){
var message = new App.Models.Message({
name: $('.nameVal').val(),
email: $('.emailVal').val(),
subject: $('.subVal').val(),
message:$('.messVal').val(),
});
message.save(null, {
success:function() {
console.log("Success");
},
error:function() {
alert('There was an error in sending the message');
}
});
}); /*end of jquery submit*/
}/*end of send message function*/,
現在,它工作正常。 歸功於Parse.com開發人員Hector Ramos,他幫助我意識到問題出在實際中。 如果你們有什么簡單的方法可以阻止某個事件重復打幾次或回頭再打一次,請告訴我。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.