Home > other >  How can I tell my javascript to wait until the server is done?
How can I tell my javascript to wait until the server is done?

Time:01-20

In my web app, I have a page where users can copy books from their library.

On the website, when a user clicks the copy button, the app executes this bit of Backbone.js code:

clonebook: function () {
    var self = this;
    this.book.clone().then((r) => {
        self.model.collection.add(r);
    });
},
    

In My SQL Server database, book looks like this:

bookId, bookTitle, authorId, libraryId, rowOrderNumber

The problem is, if the user tries to clone multiple books really fast by hitting the copy button, the rowOrderNumber can get out of whack.

Is there a way to tell the Backbone clone method to wait until the database or server has completed a clone process before going on to the next one?

Thanks!

CodePudding user response:

The most common UX pattern for this is to disable the button when the process starts, and enable when finished.

clonebook: function () {
    var self = this;
    // disable clone button
    this.book.clone().then((r) => {
        self.model.collection.add(r);
        // enable clone button
    });
},

CodePudding user response:

I didn't use backbone, but

clonebook: function () {
    var self = this;
    self.loading = true
    this.book.clone().then((r) => {
        self.model.collection.add(r).then(() => {
          self.loading = false
        });
    });
},

Not you have to use this loading somehow to disable the button

  • Related