简体   繁体   English

Parse.com中的重复问题-JavaScript SDK

[英]Repeating issue in Parse.com - JavaScript SDK

Parse.com with JavaScript SDK - unnecessary duplictions 使用JavaScript SDK的Parse.com-不必要的重复

Every time I create a Parse object of "message", it duplicates that object in my Parse Core. 每次我创建一个“消息”解析对象时,它都会在我的Parse Core中复制该对象。 It is so bizarre. 太奇怪了 The first time I run the code, everything is fine and Parse will create only one object. 第一次运行代码时,一切都很好,并且Parse仅创建一个对象。 But when I run the code again, it will duplicate the most recent object twice. 但是,当我再次运行代码时,它将复制最近的对象两次。 If I run it a third time, it will duplicate the most recent object five times. 如果我第三次运行它,它将复制最近的对象五次。 The number of duplications increases based upon how many objects have already been created. 复制数量会根据已创建的对象数量而增加。 Does anyone have any idea how to make sure that it create one object in my Parse Core backend? 有谁知道如何确保它在我的Parse Core后端中创建一个对象? Thank you so much!!! 非常感谢!!! I wish I could post a picture, but I am a newbie and stackoverflow wont let me 我希望可以发布图片,但是我是新手,所以stackoverflow不会让我

This is where I create the Parse object: 这是我创建Parse对象的地方:

App.Models.Message = Parse.Object.extend({
    className: 'Message',
    idAttribute: 'objectId',

    defaults: {
        name    : '',
        email  : '',
        subject : '',
        message : ''
    }
});

This is where I create an instance of the Parse object, and where I save it to Parse: 这是我创建Parse对象的实例的地方,并将其保存到Parse的地方:

 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');
      }

    });

    }

   });

Yes! 是! So I figured out the problem with the help of Hector Ramos from the Parse Developers Google group. 因此,我在Parse Developers Google小组的Hector Ramos的帮助下找出了问题所在。 https://groups.google.com/forum/#!topic/parse-developers/2y-mI4TgpLc https://groups.google.com/forum/#!topic/parse-developers/2y-mI4TgpLc

It was my client-side code. 这是我的客户端代码。 Instead of creating an event attached to my App.Views.Contact(); 而不是创建附加到我App.Views.Contact()的事件; aka - an instance of Parse.View.extend({}), I went ahead and created a 'click' event using jquery within the sendMessage function that I recently defined. 又名-Parse.View.extend({})的实例,我继续使用最近定义的sendMessage函数中的jquery创建了一个'click'事件。 If you declare an event in the events object within the Parse view, it will recur over itself if the view wasn't re-initialized or destroyed and recreated properly. 如果您在Parse视图内的events对象中声明一个事件,则如果该视图未重新初始化或销毁并正确创建,该事件将自行重现。

So what happened with me was the submit function that I declared in the events object kept recuring over itself and making duplicate calls to Parse.com. 因此,我所发生的事情是我在事件对象中声明的submit函数不断进行自我检查并重复调用Parse.com。 My view was static, it wasn't destroyed properly, re-initialized, or reloaded. 我的视图是静态的,没有被正确销毁,重新初始化或重新加载。 You will see what I did below: 您将在下面看到我的操作:

Originally I had this: 本来我有这个:

 events: {
  'click .submit' : 'submit',
  },

& this & 这个

 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*/


Now I have I completely removed the events object that I had and declared a sendMessage function: 现在,我已经完全删除了我拥有的事件对象,并声明了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*/,


And now it works perfectly fine. 现在,它工作正常。 Credit is due Hector Ramos who is a Parse.com Developer and who helped me realize that the problem was the actual event. 归功于Parse.com开发人员Hector Ramos,他帮助我意识到问题出在实际中。 If you guys have any easy way of stoping an event from making several duplicate calls to the back or from reoccurring several times, then please let me know. 如果你们有什么简单的方法可以阻止某个事件重复打几次或回头再打一次,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM