Ajax queue Backbone js

前端 未结 2 965
面向向阳花
面向向阳花 2021-01-11 18:08

I am running Backbone js 0.9.2 on Rails 3.2.2,I have a page for adding cost rows.A cost have 3 TextFields: title, description and price.

I am saving each cost on blu

相关标签:
2条回答
  • 2021-01-11 18:32

    You could override the save method and create a queue with a deferred object . For example,

    var MDef = Backbone.Model.extend({
        url: "/echo/json/?delay=3",
    
        initialize: function() {
            this.queue = $.Deferred();
            this.queue.resolve();
        },
    
        save: function(attrs,options) {
            var m = this; 
            console.log("set "+JSON.stringify(attrs));
    
            // this.queue = this.queue.pipe with jquery<1.8
            this.queue = this.queue.then(function() {
                console.log("request "+JSON.stringify(attrs));
                return Backbone.Model.prototype.save.call(m, attrs, options);
            });            
        }
    });
    
    var m = new MDef();
    m.save({title: "a title"});
    m.save({description: "a description"});
    m.save({price: "a price"});
    

    And a Fiddle : http://jsfiddle.net/nikoshr/8nEUm/

    0 讨论(0)
  • 2021-01-11 18:32

    User debounce from underscore.js.

    Creates and returns a new debounced version of the passed function that will postpone its execution until after wait milliseconds have elapsed since the last time it was invoked.

    This way it will only fire once after the last blur event.

    0 讨论(0)
提交回复
热议问题