简体   繁体   English

如何使用q.js链接主干模型?

[英]How to use q.js to chain backbone model?

I have the following: 我有以下几点:

var q = new app.models.OverwriteLineItemsProcess();
q.set('id', $("#process_id").val());
q.saveSource($("#source_quote").val());
q.lockSource();

saveSource is sending data to the backend using ajax. saveSource正在使用ajax将数据发送到后端。 So is lockSource . lockSource也是lockSource

I want to execute in this SEQUENTIAL manner: saveSource >> lockSource. 我想以这种顺序方式执行:saveSource >> lockSource。

How do I write the q.js to make it work? 如何编写q.js以使其正常工作?

By q.js, I mean https://github.com/kriskowal/q 通过q.js,我的意思是https://github.com/kriskowal/q

UPDATE: added saveSource and lockSource 更新:添加了saveSource和lockSource

saveSource: function (quotation_id) {;
        var type = "PUT";
        var verb = "Updated";
        var headers = {
             'X-HTTP-Method-Override': type
        };

        var url = app.base_url + "/overwrite_line_items/" + this.id;
        this.set('source_quote', quotation_id);
        var data = this.toFormData();
        var result = false;
        var currentModel = this;
        var settings = {
            headers: headers,
            type: type,
            url: url,
            data: data,
            success: function(json) {
                response = JSON && JSON.parse(json) || $.parseJSON(json);
                console.log(response);
                currentModel.lockSource();
                $("#facebox-source-quote-status").html('<font color="green">SELECTED</font>');
            },
            error: function(response) {
                $("#facebox-source-quote-status").html('<font color="red">UNABLE TO SELECT</font>');
            },
            dataType: 'json'
        };
        $.ajax(settings).done(function() {
        });
    },

    lockSource: function () {
        var type = "PUT";
        var verb = "Updated";
        var headers = {
             'X-HTTP-Method-Override': type
        };

        var url = app.base_url + "/quotations/is_editable/" + this.attributes.source_quote;

        var data = this.toFormData();
        var result = false;
        var currentModel = this;
        var settings = {
            headers: headers,
            type: type,
            url: url,
            data: data,
            success: function(response) {
                console.log(response);
            },
            error: function(response) {
                $("#facebox-source-quote-status").html('<font color="red">UNABLE TO SELECT</font>');
            },
            dataType: 'json'
        };
        $.ajax(settings).done(function() {
        });
    },

The jQuery.ajax function which you're using already returns a promise for its result. 您正在使用的jQuery.ajax函数已经为其结果返回了一个promise。 You just need to return that from your functions: 您只需要从函数中返回它:

saveSource: function (quotation_id) {;
    …
    var settings = {
        headers: headers,
        type: type,
        dataType: 'json', // jQuery will automatically parse it for you
        url: url,
        data: data
    };
    return $.ajax(settings).done(function() {
//  ^^^^^^
        $("#facebox-source-quote-status").html('<font color="green">SELECTED</font>');
        // notice I did remove the currentModel.lockSource(); call from the callback
    }, function() {
        $("#facebox-source-quote-status").html('<font color="red">UNABLE TO SELECT</font>');
    });
},
lockSource: function () {
    …
    var settings = // analoguous, no callbacks here
    return $.ajax(settings).fail(function(response) {
        $("#facebox-source-quote-status").html('<font color="red">UNABLE TO SELECT</font>');
    });
}

Now you can easily chain them: 现在,您可以轻松地将它们链接起来:

var q = new app.models.OverwriteLineItemsProcess();
q.set('id', $("#process_id").val());
q.saveSource($("#source_quote").val()).then(function(saveResponse) {
    console.log(saveResponse);
    return q.lockSource();
}).done(function(lockResponse) {
    console.log(lockResponse);
});

You don't even need Q for that. 您甚至不需要Q If you want to use it, wrap the $.ajax() calls in a Q() invocation, as explained in the Converting JQuery Promises to Q section of the docs . 如果要使用它,请将$.ajax()调用包装在Q()调用中,如docs的“ 将JQuery Promises转换为Q”部分所述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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