##// END OF EJS Templates
Update viewlists to store a list of promises for views
Jason Grout -
Show More
@@ -596,10 +596,9 b' define(["widgets/js/manager",'
596 596
597 597 _.extend(ViewList.prototype, {
598 598 initialize: function(create_view, remove_view, context) {
599 this.state_change = Promise.resolve();
600 599 this._handler_context = context || this;
601 600 this._models = [];
602 this.views = [];
601 this.views = []; // list of promises for views
603 602 this._create_view = create_view;
604 603 this._remove_view = remove_view || function(view) {view.remove();};
605 604 },
@@ -608,42 +607,36 b' define(["widgets/js/manager",'
608 607 /**
609 608 * the create_view, remove_view, and context arguments override the defaults
610 609 * specified when the list is created.
611 * returns a promise that resolves after this update is done
610 * after this function, the .views attribute is a list of promises for views
611 * if you want to perform some action on the list of views, do something like
612 * `Promise.all(myviewlist.views).then(function(views) {...});`
612 613 */
613 614 var remove = remove_view || this._remove_view;
614 615 var create = create_view || this._create_view;
615 if (create === undefined || remove === undefined){
616 console.error("Must define a create a remove function");
617 }
618 616 var context = context || this._handler_context;
619 var added_views = [];
620 var that = this;
621 this.state_change = this.state_change.then(function() {
622 var i;
617 var i = 0;
623 618 // first, skip past the beginning of the lists if they are identical
624 for (i = 0; i < new_models.length; i++) {
625 if (i >= that._models.length || new_models[i] !== that._models[i]) {
619 for (; i < new_models.length; i++) {
620 if (i >= this._models.length || new_models[i] !== this._models[i]) {
626 621 break;
627 622 }
628 623 }
624
629 625 var first_removed = i;
630 626 // Remove the non-matching items from the old list.
631 for (var j = first_removed; j < that._models.length; j++) {
632 remove.call(context, that.views[j]);
627 var removed = this.views.splice(first_removed, this.views.length-first_removed);
628 for (var j = 0; j < removed.length; j++) {
629 removed[j].then(function(view) {
630 remove.call(context, view)
631 });
633 632 }
634 633
635 634 // Add the rest of the new list items.
636 635 for (; i < new_models.length; i++) {
637 added_views.push(create.call(context, new_models[i]));
636 this.views.push(Promise.resolve(create.call(context, new_models[i])));
638 637 }
639 638 // make a copy of the input array
640 that._models = new_models.slice();
641 return Promise.all(added_views).then(function(added) {
642 Array.prototype.splice.apply(that.views, [first_removed, that.views.length].concat(added));
643 return that.views;
644 });
645 });
646 return this.state_change;
639 this._models = new_models.slice();
647 640 },
648 641
649 642 remove: function() {
@@ -653,14 +646,13 b' define(["widgets/js/manager",'
653 646 * returns a promise that resolves after this removal is done
654 647 */
655 648 var that = this;
656 this.state_change = this.state_change.then(function() {
649 Promise.all(this.views).then(function(views) {
657 650 for (var i = 0; i < that.views.length; i++) {
658 that._remove_view.call(that._handler_context, that.views[i]);
651 that._remove_view.call(that._handler_context, views[i]);
659 652 }
660 that._models = [];
661 653 that.views = [];
654 that._models = [];
662 655 });
663 return this.state_change;
664 656 },
665 657 });
666 658
General Comments 0
You need to be logged in to leave comments. Login now