Backbone集合视图url触发两次

时间:2014-01-17 20:20:43

标签: javascript jquery backbone.js

所以我的下面的应用程序实际上是在发射“FIRE!”在页面加载中两次在控制台中。我不确定为什么在我只看到一次获取时骨干网正在触发url功能两次。关于为什么这可能导致它两次射击的任何想法?

    window.ScheduleApp = {
        Models: {},
        Collections: {},
        Views: {}
    };

    window.template = function(id) {
        return _.template($('#' + id).html());
    };

    //Define the Game Model.
    ScheduleApp.Game = Backbone.Model.extend({
        initialize: function() {
            this.gameId = this.get('Id');
            this.gameTime = this.get('Time');
        }
    });

    //Define the Games Collection that contains Game Models.
    ScheduleApp.Games = Backbone.Collection.extend({
        model: ScheduleApp.Game
    });

    //Define the Day Model.
    ScheduleApp.Day = Backbone.Model.extend({
        initialize: function() {
            this.games = new ScheduleApp.Games(this.get('Games'));
            this.games.parent = this;
            this.gameDayGDT = this.get('GeneratedDateTime');
            this.gameDayDate = this.get('Date');
        }
    });

    //Define the Days Collection that contains the Day Models.
    ScheduleApp.Days = Backbone.Collection.extend({
        model: ScheduleApp.Day,
        url: function() {
            console.log('FIRE!');
            return '/js/test.json'
        },
        parse: function(data) {
            var parsedSchedule = JSON.parse('[' + data.STUFF + ']');
            return parsedSchedule;

        }

    });

    ScheduleApp.DayCollectionView = Backbone.View.extend({
        el: '.container', //Container where the views get rendered to.

        initialize: function() {
            this.listenTo(this.collection, 'reset', this.render);
        },
        render: function(event) {

            if (this.collection.length === 0) {
                $('.container-hidden').show();
            }

            //Cycle through collection of each day.
            this.collection.each(function(day) {

                var dayView = new ScheduleApp.DayView({
                    model: day
                });

                this.$el.append(dayView.render().el);

            }, this);
            return this;
        }
    });

    ScheduleApp.DayView = Backbone.View.extend({
        tagName: 'div', 
        className: 'game-date', 
        template: _.template($("#gameSchedule").html(), this.model),
        initialize: function() {
            this.listenTo(this.model, "reset", this.render);
        },
        render: function() {
            this.$el.html(this.template(this.model.toJSON()));
            return this;
        }
    });


    var daysList = new ScheduleApp.Days();

    daysList.fetch({
        reset: true,
        update: true,
        cache: false,
        success: function(collection, response) {
            //console.log(collection);
        },
        error: function(model, resp) {
            // console.log('error arguments: ', arguments);
            // console.log("error retrieving model");
        }

    });

    //create new collection view.
    var daysCollectionView = new ScheduleApp.DayCollectionView({
        collection: daysList
    });

1 个答案:

答案 0 :(得分:1)

属于集合的所有模型根据集合网址构建其网址,如here所述。我的猜测是你的集合调用方法一次,然后你的模型/模型进行第二次调用,以构建模型URL

然后,这个方法对我来说似乎非常无害:它只是一个 getter 。我宁愿将console.log调用放在Collection#parseModel#initializer方法中,并计算在那里调用的次数。