Backbone Model Instance返回TypeError

时间:2016-09-22 11:56:38

标签: javascript jquery backbone.js

以下是我的文件:

bb.js

var bb = {
    models: {},
    collections: {},
    views: {},
    router: null
};

models.js |只有路线模型

bb.models.Route = Backbone.Model.extend({
    code: null,
    terminals: [],
    stops_forwards: [],
    stops_backwards: [],
    departure_times_forwards: [],
    departure_times_backwards: [],

    initialize: function (pk) {
        var obj = this.request(pk);

        this.code = obj.code;
        this.terminals = obj.terminals;
        this.stops_forwards = obj.stops_forwards;
        this.stops_backwards = obj.stops_backwards;
        this.departure_times_forwards = obj.departure_times_forwards;
        this.departure_times_backwards = obj.departure_times_backwards;
    },

    request: function (pk) {
        var res = $.ajax({
            url: "http://1.2.3.4:1900/api/v1/route/"+pk,
            method: "GET"
        });
        // <1>
        if (res.status === 200) {
            return res.responseJSON;
        }
    }
});

我想在Route.request方法中调用Method.initialize方法,以便获取数据。我想通过以下方式在Mozilla控制台中进行测试:

var myR = new bb.models.Route("285");

但是,这会引发错误:

  

TypeError :obj未定义。

我得到的数据类似于:

{
    "code":285,
    "terminals":[
    "EVKA 1",
    "KONAK"
    ],
    "stops_forwards":[
    [
        0,
        40661
    ],
    [
        1,
        40658
    ],
    [
        2,
        40656
    ],
    [
        3,
        40645
    ],
    [
        4,
        40649
    ],
    [
        5,
        40647
    ],
    [
        6,
        40653
    ],
    [
        7,
        40619
    ],
    [
        8,
        40633
    ],
    [
        9,
        40623
    ],
    [
        10,
        40627
    ],
    [
        11,
        40631
    ],
    [
        12,
        40614
    ],
    [
        13,
        40530
    ],
    [
        14,
        40528
    ],
    [
        15,
        40592
    ],
    [
        16,
        40590
    ],
    [
        17,
        40588
    ],
    [
        18,
        40586
    ],
    [
        19,
        40514
    ],
    [
        20,
        40512
    ],
    [
        21,
        40510
    ],
    [
        22,
        40508
    ],
    [
        23,
        40506
    ],
    [
        24,
        40504
    ],
    [
        25,
        40488
    ],
    [
        26,
        40464
    ],
    [
        27,
        40462
    ],
    [
        28,
        40016
    ],
    [
        29,
        40014
    ],
    [
        30,
        40012
    ],
    [
        31,
        40010
    ],
    [
        32,
        40004
    ],
    [
        33,
        40002
    ],
    [
        34,
        12070
    ],
    [
        35,
        12068
    ],
    [
        36,
        12064
    ],
    [
        37,
        12066
    ],
    [
        38,
        12060
    ],
    [
        39,
        10240
    ],
    [
        40,
        10036
    ]
    ],
    "stops_backwards":[
    [
        0,
        10036
    ],
    [
        1,
        10119
    ],
    [
        2,
        10231
    ],
    [
        3,
        10235
    ],
    [
        4,
        12057
    ],
    [
        5,
        12065
    ],
    [
        6,
        12069
    ],
    [
        7,
        12071
    ],
    [
        8,
        40001
    ],
    [
        9,
        40009
    ],
    [
        10,
        40011
    ],
    [
        11,
        40013
    ],
    [
        12,
        40015
    ],
    [
        13,
        40461
    ],
    [
        14,
        40475
    ],
    [
        15,
        40503
    ],
    [
        16,
        40505
    ],
    [
        17,
        40507
    ],
    [
        18,
        40509
    ],
    [
        19,
        40511
    ],
    [
        20,
        40513
    ],
    [
        21,
        40515
    ],
    [
        22,
        40587
    ],
    [
        23,
        40589
    ],
    [
        24,
        40591
    ],
    [
        25,
        40593
    ],
    [
        26,
        40529
    ],
    [
        27,
        40531
    ],
    [
        28,
        40613
    ],
    [
        29,
        40630
    ],
    [
        30,
        40626
    ],
    [
        31,
        40624
    ],
    [
        32,
        40632
    ],
    [
        33,
        40620
    ],
    [
        34,
        40652
    ],
    [
        35,
        40646
    ],
    [
        36,
        40648
    ],
    [
        37,
        40644
    ],
    [
        38,
        40657
    ],
    [
        39,
        40659
    ],
    [
        40,
        40661
    ]
    ],
    "departure_times_forwards":[
    [
        "06:00:00",
        "06:17:00",
        "06:35:00",
        "06:51:00",
        "07:08:00",
        "07:25:00",
        "07:42:00",
        "07:59:00",
        "08:16:00",
        "08:33:00",
        "08:50:00",
        "09:05:00",
        "09:20:00",
        "09:35:00",
        "09:50:00",
        "10:05:00",
        "10:20:00",
        "10:35:00",
        "10:50:00",
        "11:05:00",
        "11:20:00",
        "11:34:00",
        "11:49:00",
        "12:04:00",
        "12:18:00",
        "12:33:00",
        "12:48:00",
        "13:02:00",
        "13:17:00",
        "13:32:00",
        "13:46:00",
        "14:01:00",
        "14:16:00",
        "14:30:00",
        "14:45:00",
        "15:00:00",
        "15:14:00",
        "15:29:00",
        "15:44:00",
        "15:58:00",
        "16:13:00",
        "16:28:00",
        "16:42:00",
        "16:57:00",
        "17:12:00",
        "17:26:00",
        "17:41:00",
        "17:56:00",
        "18:10:00",
        "18:25:00",
        "18:40:00",
        "18:54:00",
        "19:09:00",
        "19:24:00",
        "19:38:00",
        "19:53:00",
        "20:10:00",
        "20:30:00",
        "20:55:00",
        "21:15:00",
        "21:35:00",
        "21:55:00",
        "22:15:00",
        "22:35:00",
        "22:55:00",
        "23:15:00"
    ],
    [
        "06:45:00",
        "07:02:00",
        "07:19:00",
        "07:36:00",
        "07:53:00",
        "08:10:00",
        "08:27:00",
        "08:44:00",
        "09:01:00",
        "09:18:00",
        "09:35:00",
        "09:57:00",
        "10:12:00",
        "10:27:00",
        "10:42:00",
        "10:57:00",
        "11:12:00",
        "11:27:00",
        "11:42:00",
        "11:57:00",
        "12:15:00",
        "12:29:00",
        "12:44:00",
        "12:59:00",
        "13:13:00",
        "13:28:00",
        "13:43:00",
        "13:57:00",
        "14:12:00",
        "14:27:00",
        "14:41:00",
        "14:56:00",
        "15:11:00",
        "15:25:00",
        "15:40:00",
        "15:55:00",
        "16:09:00",
        "16:24:00",
        "16:39:00",
        "16:53:00",
        "17:08:00",
        "17:23:00",
        "17:37:00",
        "17:52:00",
        "18:07:00",
        "18:21:00",
        "18:36:00",
        "18:51:00",
        "19:05:00",
        "19:20:00",
        "19:35:00",
        "19:49:00",
        "20:04:00",
        "20:19:00",
        "20:33:00",
        "20:46:00",
        "21:00:00",
        "21:20:00",
        "21:40:00",
        "22:00:00",
        "22:20:00",
        "22:40:00",
        "23:00:00",
        "23:20:00",
        "23:40:00",
        "00:00:00"
    ],
    [
        "06:05:00",
        "06:20:00",
        "06:35:00",
        "06:50:00",
        "07:05:00",
        "07:20:00",
        "07:35:00",
        "07:50:00",
        "08:05:00",
        "08:20:00",
        "08:35:00",
        "08:50:00",
        "09:05:00",
        "09:22:00",
        "09:40:00",
        "09:57:00",
        "10:15:00",
        "10:32:00",
        "10:50:00",
        "11:07:00",
        "11:25:00",
        "11:42:00",
        "12:00:00",
        "12:17:00",
        "12:35:00",
        "12:52:00",
        "13:10:00",
        "13:27:00",
        "13:45:00",
        "14:02:00",
        "14:20:00",
        "14:37:00",
        "14:55:00",
        "15:12:00",
        "15:30:00",
        "15:47:00",
        "16:02:00",
        "16:17:00",
        "16:32:00",
        "16:47:00",
        "17:02:00",
        "17:17:00",
        "17:32:00",
        "17:47:00",
        "18:02:00",
        "18:17:00",
        "18:32:00",
        "18:47:00",
        "19:02:00",
        "19:17:00",
        "19:32:00",
        "19:50:00",
        "20:10:00",
        "20:30:00",
        "20:50:00",
        "21:10:00",
        "21:30:00",
        "21:50:00",
        "22:10:00",
        "22:30:00",
        "22:50:00",
        "23:10:00",
        null,
        null,
        null,
        null
    ],
    [
        "06:45:00",
        "07:00:00",
        "07:20:00",
        "07:40:00",
        "07:55:00",
        "08:10:00",
        "08:25:00",
        "08:40:00",
        "08:50:00",
        "09:10:00",
        "09:25:00",
        "09:40:00",
        "09:55:00",
        "10:12:00",
        "10:30:00",
        "10:47:00",
        "11:05:00",
        "11:22:00",
        "11:40:00",
        "11:57:00",
        "12:15:00",
        "12:32:00",
        "12:50:00",
        "13:07:00",
        "13:25:00",
        "13:42:00",
        "14:00:00",
        "14:17:00",
        "14:35:00",
        "14:52:00",
        "15:10:00",
        "15:27:00",
        "15:45:00",
        "16:02:00",
        "16:20:00",
        "16:37:00",
        "16:52:00",
        "17:07:00",
        "17:22:00",
        "17:37:00",
        "17:52:00",
        "18:07:00",
        "18:22:00",
        "18:37:00",
        "18:52:00",
        "19:07:00",
        "19:22:00",
        "19:37:00",
        "19:52:00",
        "20:07:00",
        "20:22:00",
        "20:37:00",
        "20:55:00",
        "21:15:00",
        "21:35:00",
        "21:55:00",
        "22:15:00",
        "22:35:00",
        "22:55:00",
        "23:15:00",
        "23:35:00",
        "00:00:00",
        null,
        null,
        null,
        null
    ],
    [
        "06:00:00",
        "06:17:00",
        "06:35:00",
        "06:51:00",
        "07:08:00",
        "07:25:00",
        "07:42:00",
        "07:59:00",
        "08:16:00",
        "08:33:00",
        "08:50:00",
        "09:05:00",
        "09:20:00",
        "09:35:00",
        "09:50:00",
        "10:05:00",
        "10:20:00",
        "10:35:00",
        "10:50:00",
        "11:05:00",
        "11:20:00",
        "11:34:00",
        "11:49:00",
        "12:04:00",
        "12:18:00",
        "12:33:00",
        "12:48:00",
        "13:02:00",
        "13:17:00",
        "13:32:00",
        "13:46:00",
        "14:01:00",
        "14:16:00",
        "14:30:00",
        "14:45:00",
        "15:00:00",
        "15:14:00",
        "15:29:00",
        "15:44:00",
        "15:58:00",
        "16:13:00",
        "16:28:00",
        "16:42:00",
        "16:57:00",
        "17:12:00",
        "17:26:00",
        "17:41:00",
        "17:56:00",
        "18:10:00",
        "18:25:00",
        "18:40:00",
        "18:54:00",
        "19:09:00",
        "19:24:00",
        "19:38:00",
        "19:53:00",
        "20:10:00",
        "20:30:00",
        "20:55:00",
        "21:15:00",
        "21:35:00",
        "21:55:00",
        "22:15:00",
        "22:35:00",
        "22:55:00",
        "23:15:00"
    ],
    [
        "06:45:00",
        "07:02:00",
        "07:19:00",
        "07:36:00",
        "07:53:00",
        "08:10:00",
        "08:27:00",
        "08:44:00",
        "09:01:00",
        "09:18:00",
        "09:35:00",
        "09:57:00",
        "10:12:00",
        "10:27:00",
        "10:42:00",
        "10:57:00",
        "11:12:00",
        "11:27:00",
        "11:42:00",
        "11:57:00",
        "12:15:00",
        "12:29:00",
        "12:44:00",
        "12:59:00",
        "13:13:00",
        "13:28:00",
        "13:43:00",
        "13:57:00",
        "14:12:00",
        "14:27:00",
        "14:41:00",
        "14:56:00",
        "15:11:00",
        "15:25:00",
        "15:40:00",
        "15:55:00",
        "16:09:00",
        "16:24:00",
        "16:39:00",
        "16:53:00",
        "17:08:00",
        "17:23:00",
        "17:37:00",
        "17:52:00",
        "18:07:00",
        "18:21:00",
        "18:36:00",
        "18:51:00",
        "19:05:00",
        "19:20:00",
        "19:35:00",
        "19:49:00",
        "20:04:00",
        "20:19:00",
        "20:33:00",
        "20:46:00",
        "21:00:00",
        "21:20:00",
        "21:40:00",
        "22:00:00",
        "22:20:00",
        "22:40:00",
        "23:00:00",
        "23:20:00",
        "23:40:00",
        "00:00:00"
    ]
    ],
    "departure_times_backwards":[
    [
        "06:00:00",
        "06:17:00",
        "06:35:00",
        "06:51:00",
        "07:08:00",
        "07:25:00",
        "07:42:00",
        "07:59:00",
        "08:16:00",
        "08:33:00",
        "08:50:00",
        "09:05:00",
        "09:20:00",
        "09:35:00",
        "09:50:00",
        "10:05:00",
        "10:20:00",
        "10:35:00",
        "10:50:00",
        "11:05:00",
        "11:20:00",
        "11:34:00",
        "11:49:00",
        "12:04:00",
        "12:18:00",
        "12:33:00",
        "12:48:00",
        "13:02:00",
        "13:17:00",
        "13:32:00",
        "13:46:00",
        "14:01:00",
        "14:16:00",
        "14:30:00",
        "14:45:00",
        "15:00:00",
        "15:14:00",
        "15:29:00",
        "15:44:00",
        "15:58:00",
        "16:13:00",
        "16:28:00",
        "16:42:00",
        "16:57:00",
        "17:12:00",
        "17:26:00",
        "17:41:00",
        "17:56:00",
        "18:10:00",
        "18:25:00",
        "18:40:00",
        "18:54:00",
        "19:09:00",
        "19:24:00",
        "19:38:00",
        "19:53:00",
        "20:10:00",
        "20:30:00",
        "20:55:00",
        "21:15:00",
        "21:35:00",
        "21:55:00",
        "22:15:00",
        "22:35:00",
        "22:55:00",
        "23:15:00"
    ],
    [
        "06:45:00",
        "07:02:00",
        "07:19:00",
        "07:36:00",
        "07:53:00",
        "08:10:00",
        "08:27:00",
        "08:44:00",
        "09:01:00",
        "09:18:00",
        "09:35:00",
        "09:57:00",
        "10:12:00",
        "10:27:00",
        "10:42:00",
        "10:57:00",
        "11:12:00",
        "11:27:00",
        "11:42:00",
        "11:57:00",
        "12:15:00",
        "12:29:00",
        "12:44:00",
        "12:59:00",
        "13:13:00",
        "13:28:00",
        "13:43:00",
        "13:57:00",
        "14:12:00",
        "14:27:00",
        "14:41:00",
        "14:56:00",
        "15:11:00",
        "15:25:00",
        "15:40:00",
        "15:55:00",
        "16:09:00",
        "16:24:00",
        "16:39:00",
        "16:53:00",
        "17:08:00",
        "17:23:00",
        "17:37:00",
        "17:52:00",
        "18:07:00",
        "18:21:00",
        "18:36:00",
        "18:51:00",
        "19:05:00",
        "19:20:00",
        "19:35:00",
        "19:49:00",
        "20:04:00",
        "20:19:00",
        "20:33:00",
        "20:46:00",
        "21:00:00",
        "21:20:00",
        "21:40:00",
        "22:00:00",
        "22:20:00",
        "22:40:00",
        "23:00:00",
        "23:20:00",
        "23:40:00",
        "00:00:00"
    ],
    [
        "06:05:00",
        "06:20:00",
        "06:35:00",
        "06:50:00",
        "07:05:00",
        "07:20:00",
        "07:35:00",
        "07:50:00",
        "08:05:00",
        "08:20:00",
        "08:35:00",
        "08:50:00",
        "09:05:00",
        "09:22:00",
        "09:40:00",
        "09:57:00",
        "10:15:00",
        "10:32:00",
        "10:50:00",
        "11:07:00",
        "11:25:00",
        "11:42:00",
        "12:00:00",
        "12:17:00",
        "12:35:00",
        "12:52:00",
        "13:10:00",
        "13:27:00",
        "13:45:00",
        "14:02:00",
        "14:20:00",
        "14:37:00",
        "14:55:00",
        "15:12:00",
        "15:30:00",
        "15:47:00",
        "16:02:00",
        "16:17:00",
        "16:32:00",
        "16:47:00",
        "17:02:00",
        "17:17:00",
        "17:32:00",
        "17:47:00",
        "18:02:00",
        "18:17:00",
        "18:32:00",
        "18:47:00",
        "19:02:00",
        "19:17:00",
        "19:32:00",
        "19:50:00",
        "20:10:00",
        "20:30:00",
        "20:50:00",
        "21:10:00",
        "21:30:00",
        "21:50:00",
        "22:10:00",
        "22:30:00",
        "22:50:00",
        "23:10:00",
        null,
        null,
        null,
        null
    ],
    [
        "06:45:00",
        "07:00:00",
        "07:20:00",
        "07:40:00",
        "07:55:00",
        "08:10:00",
        "08:25:00",
        "08:40:00",
        "08:50:00",
        "09:10:00",
        "09:25:00",
        "09:40:00",
        "09:55:00",
        "10:12:00",
        "10:30:00",
        "10:47:00",
        "11:05:00",
        "11:22:00",
        "11:40:00",
        "11:57:00",
        "12:15:00",
        "12:32:00",
        "12:50:00",
        "13:07:00",
        "13:25:00",
        "13:42:00",
        "14:00:00",
        "14:17:00",
        "14:35:00",
        "14:52:00",
        "15:10:00",
        "15:27:00",
        "15:45:00",
        "16:02:00",
        "16:20:00",
        "16:37:00",
        "16:52:00",
        "17:07:00",
        "17:22:00",
        "17:37:00",
        "17:52:00",
        "18:07:00",
        "18:22:00",
        "18:37:00",
        "18:52:00",
        "19:07:00",
        "19:22:00",
        "19:37:00",
        "19:52:00",
        "20:07:00",
        "20:22:00",
        "20:37:00",
        "20:55:00",
        "21:15:00",
        "21:35:00",
        "21:55:00",
        "22:15:00",
        "22:35:00",
        "22:55:00",
        "23:15:00",
        "23:35:00",
        "00:00:00",
        null,
        null,
        null,
        null
    ],
    [
        "06:00:00",
        "06:17:00",
        "06:35:00",
        "06:51:00",
        "07:08:00",
        "07:25:00",
        "07:42:00",
        "07:59:00",
        "08:16:00",
        "08:33:00",
        "08:50:00",
        "09:05:00",
        "09:20:00",
        "09:35:00",
        "09:50:00",
        "10:05:00",
        "10:20:00",
        "10:35:00",
        "10:50:00",
        "11:05:00",
        "11:20:00",
        "11:34:00",
        "11:49:00",
        "12:04:00",
        "12:18:00",
        "12:33:00",
        "12:48:00",
        "13:02:00",
        "13:17:00",
        "13:32:00",
        "13:46:00",
        "14:01:00",
        "14:16:00",
        "14:30:00",
        "14:45:00",
        "15:00:00",
        "15:14:00",
        "15:29:00",
        "15:44:00",
        "15:58:00",
        "16:13:00",
        "16:28:00",
        "16:42:00",
        "16:57:00",
        "17:12:00",
        "17:26:00",
        "17:41:00",
        "17:56:00",
        "18:10:00",
        "18:25:00",
        "18:40:00",
        "18:54:00",
        "19:09:00",
        "19:24:00",
        "19:38:00",
        "19:53:00",
        "20:10:00",
        "20:30:00",
        "20:55:00",
        "21:15:00",
        "21:35:00",
        "21:55:00",
        "22:15:00",
        "22:35:00",
        "22:55:00",
        "23:15:00"
    ],
    [
        "06:45:00",
        "07:02:00",
        "07:19:00",
        "07:36:00",
        "07:53:00",
        "08:10:00",
        "08:27:00",
        "08:44:00",
        "09:01:00",
        "09:18:00",
        "09:35:00",
        "09:57:00",
        "10:12:00",
        "10:27:00",
        "10:42:00",
        "10:57:00",
        "11:12:00",
        "11:27:00",
        "11:42:00",
        "11:57:00",
        "12:15:00",
        "12:29:00",
        "12:44:00",
        "12:59:00",
        "13:13:00",
        "13:28:00",
        "13:43:00",
        "13:57:00",
        "14:12:00",
        "14:27:00",
        "14:41:00",
        "14:56:00",
        "15:11:00",
        "15:25:00",
        "15:40:00",
        "15:55:00",
        "16:09:00",
        "16:24:00",
        "16:39:00",
        "16:53:00",
        "17:08:00",
        "17:23:00",
        "17:37:00",
        "17:52:00",
        "18:07:00",
        "18:21:00",
        "18:36:00",
        "18:51:00",
        "19:05:00",
        "19:20:00",
        "19:35:00",
        "19:49:00",
        "20:04:00",
        "20:19:00",
        "20:33:00",
        "20:46:00",
        "21:00:00",
        "21:20:00",
        "21:40:00",
        "22:00:00",
        "22:20:00",
        "22:40:00",
        "23:00:00",
        "23:20:00",
        "23:40:00",
        "00:00:00"
    ]
    ]
}

进一步调试

当我调试时,我实际看到代码运行到我标记为<1>的行。然后,如果语句不起作用,可能函数返回undefined。无论我在哪里,我都没有看到任何拼写错误或任何东西。

此外,服务器肯定会返回200.

环境

  • Backbone 1.3.3
  • Underscore 1.8.3
  • Jquery 3.1.0

0 个答案:

没有答案