backbonejs fetch返回两次(失败,然后成功)

时间:2012-02-01 06:05:57

标签: javascript codeigniter rest backbone.js

我正在调用我的api,它通过json返回一个任务对象(例如下面的返回json):

[{"pkTaskId":"96","fldName":"Change page to template","fldStatus":"Assigned","fldNotes":"http:\/\/williamsconcepts.com\/ci\/codeigniter\/libraries\/template\/reference.html\r\n\r\n111","fldDateDue":"0000-00-00 00:00:00","fldDateCompleted":"0000-00-00 00:00:00"},{"pkTaskId":"103","fldName":"fix list creation","fldStatus":"Assigned","fldNotes":"for some reason there is an SQL syntax error\r\n\r\nok","fldDateDue":"0000-00-00 00:00:00","fldDateCompleted":"0000-00-00 00:00:00"},{"pkTaskId":"104","fldName":"navicat db admin tool","fldStatus":"Assigned","fldNotes":"Try this out:\nhttp:\/\/www.navicat.com\/download\/download.html","fldDateDue":"0000-00-00 00:00:00","fldDateCompleted":"0000-00-00 00:00:00"},{"pkTaskId":"105","fldName":"Styling dropdowns","fldStatus":"Assigned","fldNotes":"Link:\nhttp:\/\/jqueryui.com\/demos\/autocomplete\/#combobox","fldDateDue":"0000-00-00 00:00:00","fldDateCompleted":"0000-00-00 00:00:00"},{"pkTaskId":"147","fldName":"api create task","fldStatus":"Assigned","fldNotes":"","fldDateDue":"0000-00-00 00:00:00","fldDateCompleted":"0000-00-00 00:00:00"}]



    $(function(){
    window.Task = Backbone.Model.extend({});

    window.TaskList = Backbone.Collection.extend({
        model: Task,
        url: "http://localhost/tasker/index.php/api/tasks/username/lucasmp"
    });

    window.tasks = new TaskList();

    window.AppView = Backbone.View.extend({
        initialize: function() {
            tasks.fetch({
                success: function() {
                    console.log(tasks.toJSON());
                }
            });   
        }
    });

    window.App = new AppView;    
});

$('#fetch').click(function(){
    tasks.fetch({
        success: function() {
            alert("success");
            console.log(tasks.toJSON());
        },
        error: alert("error")
    });    
});

我遇到了一个问题,但是我的fetch.click发生了两次;一旦返回fetch错误,则返回fetch success。是什么导致这种情况被解雇两次?

1 个答案:

答案 0 :(得分:1)

请试试这个

error: function(){alert("error")}