Javascript BackboneJS在日期过期时隐藏模型

Javascript BackboneJS在日期过期时隐藏模型,javascript,json,backbone.js,Javascript,Json,Backbone.js,我有一个主干应用程序,其中显示基于JSON数据的模型集合。在JSON数据中,我有endDate,它提供了实时日期。它基于一个竞赛模块。我想要实现的是,如果给定的日期已经过期,我想隐藏,甚至可能从集合中删除模型,以便不再提供竞争 到目前为止,my competition.js底部的模型如下所示: Competition.View = Backbone.View.extend({ tagName: 'ul', template: 'competition', initiali

我有一个主干应用程序,其中显示基于JSON数据的模型集合。在JSON数据中,我有endDate,它提供了实时日期。它基于一个竞赛模块。我想要实现的是,如果给定的日期已经过期,我想隐藏,甚至可能从集合中删除模型,以便不再提供竞争

到目前为止,my competition.js底部的模型如下所示:

Competition.View = Backbone.View.extend({
    tagName: 'ul',
    template: 'competition',
    initialize: function() {
        this.listenTo(this.model, 'sync', this.render);             
    },
    serialize: function() {
        return this.model.toJSON();
    }
});

Competition.CompetitionModel = Backbone.Model.extend({
    url: function() {
        return App.APIO + '/i/contests';
    },
    comparator: function(item) {
        return item.get('endDate');
    },

    defaults: {
        "data": []
    }
});
然后在我的主模块中,我导入competition.js,在这里我获取模型并在特定的HTML元素中呈现它。我不知道是否有必要将其复制/粘贴到这里以解决我的原始问题:

function (App, Backbone, Competition) {

    var CompetitionOverview = App.module();

    CompetitionOverview.View = Backbone.View.extend({
        template: 'competitionOverview',
        initialize: function(){
            this.render();
        },
        beforeRender: function() {
            var competitionModel = new Competition.CompetitionModel();
            this.insertView('.singleComp', new Competition.View({model: competitionModel}));
            competitionModel.fetch();   
        },

    });

    return CompetitionOverview;
}
那么,我如何实现隐藏/删除已过期的模型


提前感谢…

您声明您有模型集合,但您的Competition.CompetitionModel扩展了主干.Model而不是主干.Collection。在我的回答中,我假设CompetitionModel是一个主干.Collection,而不是主干.Model

也就是说,我认为你有两个选择:

检查竞赛的渲染功能。查看是否应根据结束日期实际显示某些内容:

Competition.View = Backbone.View.extend({
    tagName: 'ul',
    template: 'competition',
    initialize: function() {
         this.listenTo(this.model, 'sync', this.render);             
     },
     serialize: function() {
         return this.model.toJSON();
     },
     render: function(){
          //dependending on in which format your date is you might have to convert first. 
          if(this.model.get('endDate') < new Date().getTime()){
                //render the view
          }
});
或者,我认为这更干净,只要数据从服务器传入,就检查日期。我认为当从服务器获取集合时,主干会触发集合上的add事件。因此,再次将您的竞赛模型设置为竞赛集合,并收听add活动。改变

Competition.CompetitionModel = Backbone.Collection.extend({
    initialize: function () {
        this.on("add", checkDate)
    },
    checkDate: function(model, collection, options){
        //again, get the right conversion
        //but here you should compare your end date with the expire date
        if(model.get('endDate') < new Date().getTime()){
            this.remove(model.id);
        }
    },
    url: function () {
        return App.APIO + '/i/contests';
    },
    comparator: function (item) {
        return item.get('endDate');
    },

    defaults: {
        "data": []
    }
});

如果下面的答案帮助你解决了这个问题,请接受答案。非常感谢。