繁体   English   中英

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

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

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

每次我创建一个“消息”解析对象时,它都会在我的Parse Core中复制该对象。 太奇怪了 第一次运行代码时,一切都很好,并且Parse仅创建一个对象。 但是,当我再次运行代码时,它将复制最近的对象两次。 如果我第三次运行它,它将复制最近的对象五次。 复制数量会根据已创建的对象数量而增加。 有谁知道如何确保它在我的Parse Core后端中创建一个对象? 非常感谢!!! 我希望可以发布图片,但是我是新手,所以stackoverflow不会让我

这是我创建Parse对象的地方:

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

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

这是我创建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');
      }

    });

    }

   });

是! 因此,我在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.

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