javascript - Filter Backbone collection and keep original -
In the following code, I have set a view, where I am filtering an archive based on date input. This code works correctly for the first time, but obviously the collection starts again and again if I filter the date again, it does not filter from the original archive. What is the best practice to filter the spinal cord and keep the original copy?
window.DataIndexView = backbone.View.extend ({tagName: 'section', className: 'data', event: {"#changedate click": "filterDate"}, start: Function () {_.bindAll ('render', 'filterdate'); template = _tremlet ($ ("# data_template") .html ()); this.collection.bind ('reset', this. Render ());}, render: function () {var data = this.collection; $ (this.el). Html (this.template ({}); data.each (function (point) {}); Return it;}, the filtered date function () {var startdate = this. $ ("#startdate"). Val (); var filtered = _.filter (this.collection, function) {return moment (item. Get ("date"). ValueOf () & gt; moment (start, 'mm -DD-YYYY '). ValueOf ();}); this.collection.reset (filter);}});
_ Filters not to touch your collection. It gives a new array
I believe what you want, can be obtained with something like
filterDate: function () {var Startdate = This. $ ("#startdate") Val (); // Originally a collection clone, instead of copying each object, it will keep the same reference, this .original_collection = New YourCollectionType (this.collection.models); This.collection.reset (_ filter (this cubation, function) {return moment (item gate ("date"). ValueOf () & gt; moment (initial, 'mm-dd-y y y y '). Value of ();})); // triggered automatically with render}}
Comments
Post a Comment