javascript - BackboneJS Hide Model when date has expired -
i have backbone app display collection of models based on json data. within json data, have enddate-which gives me realtime date. based on competition module. want accomplish if given date has expired want hide (or maybe remove) model collection, competition no longer available.
so far competition.js, model in bottom looks this:
competition.view = backbone.view.extend({ tagname: 'ul', template: 'competition', initialize: function() { this.listento(this.model, 'sync', this.render); }, serialize: function() { homecoming this.model.tojson(); } }); competition.competitionmodel = backbone.model.extend({ url: function() { homecoming app.apio + '/i/contests'; }, comparator: function(item) { homecoming item.get('enddate'); }, defaults: { "data": [] } }); then in main module, import competition.js, , here fetch model , render in specific html element (dont know if necessary copy/paste here original question):
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(); }, }); homecoming competitionoverview; } so, how can accomplish hide/remove models dates have expired?
thanks in advance...
you state have collection of models, competition.competitionmodel extends backbone.model instead of backbone.collection. in reply assume competitionmodel backbone.collection , not backbone.model.
that said, think have 2 options:
check in render function of competition.view whether should show based on end-date:
competition.view = backbone.view.extend({ tagname: 'ul', template: 'competition', initialize: function() { this.listento(this.model, 'sync', this.render); }, serialize: function() { homecoming this.model.tojson(); }, render: function(){ //dependending on in format date might have convert first. if(this.model.get('enddate') < new date().gettime()){ //render view } }); or, , think more clean, check date info comes in server. think backbone triggers "add" event on collection when collection fetched server. so, again, create competition model competition collection , hear add together event. alter
competition.competitionmodel = backbone.collection.extend({ initialize: function () { this.on("add", checkdate) }, checkdate: function(model, collection, options){ //again, right conversion //but here should compare end date expire date if(model.get('enddate') < new date().gettime()){ this.remove(model.id); } }, url: function () { homecoming app.apio + '/i/contests'; }, comparator: function (item) { homecoming item.get('enddate'); }, defaults: { "data": [] } }); javascript json backbone.js
No comments:
Post a Comment