javascriptbackbone.jsbackbone.js-collectionsbackbone-model

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


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!


Solution

  • 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