简体   繁体   中英

Repeating issue in Parse.com - JavaScript SDK

Parse.com with JavaScript SDK - unnecessary duplictions

Every time I create a Parse object of "message", it duplicates that object in my Parse Core. It is so bizarre. The first time I run the code, everything is fine and Parse will create only one object. 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? Thank you so much!!! I wish I could post a picture, but I am a newbie and stackoverflow wont let me

This is where I create the Parse object:

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:

 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. 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(); 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. 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.

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. 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:

  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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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