Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Backbonejs仅在传递asyc:false作为fetch()方法的参数时才会获取值_Javascript_Backbone.js_Fetch - Fatal编程技术网

Javascript Backbonejs仅在传递asyc:false作为fetch()方法的参数时才会获取值

Javascript Backbonejs仅在传递asyc:false作为fetch()方法的参数时才会获取值,javascript,backbone.js,fetch,Javascript,Backbone.js,Fetch,我目前正在开发一个单页应用程序,我也在使用backbonejs。我的后端响应是JSON,我使用fetch()方法获取。我面临的问题是 只有当我使用fetch({async:false})时,userNames属性才会填充正确的值,如果我使用fetch(),则会得到一个空响应 我做错什么了吗?我错过了什么明显的东西吗? 我的代码如下 $(document).ready(function(){ var booking_model = Backbone.Model.extend({});

我目前正在开发一个单页应用程序,我也在使用backbonejs。我的后端响应是JSON,我使用fetch()方法获取。我面临的问题是 只有当我使用fetch({async:false})时,userNames属性才会填充正确的值,如果我使用fetch(),则会得到一个空响应

我做错什么了吗?我错过了什么明显的东西吗? 我的代码如下

$(document).ready(function(){
    var booking_model = Backbone.Model.extend({});

    var UserList = Backbone.Collection.extend({ 
    model: booking_model,
    url: 'js/bookings.json',
    parse: function(response) {return response};


});





    var users = new UserList(); //Line 26
    users.fetch({async: false});
    var userNames = users.pluck("bookingId");
    console.log(userNames)
});
我的JSON

[
        {
            "bookingId": 260,
            "bookingSourceId": "Online",
            "bookingTime": "Jan1391312: 00: 00AM",
            "noOfPeople": "10",
            "reserveDate": "ThuJan0200: 00: 00IST3913",
            "restId": 200,
            "timing_id": 200
        },
        {
            "bookingId": 280,
            "bookingTime": "Dec25 20121: 43: 49AM",
            "noOfPeople": "6",
            "reserveDate": "ThuJan2600: 00: 00IST2012",
            "restId": 220,
            "timing_id": 205
        },
        {
            "bookingId": 300,
            "bookingTime": "Dec26 20122: 12: 00AM",
            "noOfPeople": "4",
            "reserveDate": "FriJan2700: 00: 00IST2012",
            "restId": 260,
            "timing_id": 220
        },
        {
            "bookingId": 320,
            "bookingTime": "Dec27 20122: 14: 54AM",
            "noOfPeople": "10",
            "reserveDate": "SunJan2900: 00: 00IST2012",
            "restId": 260,
            "timing_id": 201
        },
        {
            "bookingId": 340,
            "bookingTime": "Dec25 20122: 35: 19AM",
            "noOfPeople": "8",
            "reserveDate": "TueJan2400: 00: 00IST2012",
            "restId": 220,
            "timing_id": 205
        },
        {
            "bookingId": 360,
            "bookingSourceId": "Online",
            "bookingTime": "May30 391312: 00: 00AM",
            "noOfPeople": "10",
            "reserveDate": "FriMay3000: 00: 00IST3913",
            "restId": 200,
            "timing_id": 200,
            "bookingUser": "hareesh",
            "bookingPhoneNo": "9052228181",
            "bookingEmail": "makamhareesh@gmail.com"
        }



    ]

你错过了一些明显的东西:

users.fetch({
  success: function() {
    var userNames = users.pluck("bookingId");
    console.log(userNames)
  }
});

当“fetch”操作是异步的时,紧跟其后的代码将在HTTP操作完成之前运行很长时间。

您缺少一些明显的东西:

users.fetch({
  success: function() {
    var userNames = users.pluck("bookingId");
    console.log(userNames)
  }
});
当“fetch”操作是异步的时,紧随其后的代码将在HTTP操作完成之前很久运行