##// 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 _.extend(ViewList.prototype, {
597 _.extend(ViewList.prototype, {
598 initialize: function(create_view, remove_view, context) {
598 initialize: function(create_view, remove_view, context) {
599 this.state_change = Promise.resolve();
600 this._handler_context = context || this;
599 this._handler_context = context || this;
601 this._models = [];
600 this._models = [];
602 this.views = [];
601 this.views = []; // list of promises for views
603 this._create_view = create_view;
602 this._create_view = create_view;
604 this._remove_view = remove_view || function(view) {view.remove();};
603 this._remove_view = remove_view || function(view) {view.remove();};
605 },
604 },
@@ -608,42 +607,36 b' define(["widgets/js/manager",'
608 /**
607 /**
609 * the create_view, remove_view, and context arguments override the defaults
608 * the create_view, remove_view, and context arguments override the defaults
610 * specified when the list is created.
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 var remove = remove_view || this._remove_view;
614 var remove = remove_view || this._remove_view;
614 var create = create_view || this._create_view;
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 var context = context || this._handler_context;
616 var context = context || this._handler_context;
619 var added_views = [];
617 var i = 0;
620 var that = this;
618 // first, skip past the beginning of the lists if they are identical
621 this.state_change = this.state_change.then(function() {
619 for (; i < new_models.length; i++) {
622 var i;
620 if (i >= this._models.length || new_models[i] !== this._models[i]) {
623 // first, skip past the beginning of the lists if they are identical
621 break;
624 for (i = 0; i < new_models.length; i++) {
625 if (i >= that._models.length || new_models[i] !== that._models[i]) {
626 break;
627 }
628 }
629 var first_removed = i;
630 // 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]);
633 }
634
635 // Add the rest of the new list items.
636 for (; i < new_models.length; i++) {
637 added_views.push(create.call(context, new_models[i]));
638 }
622 }
639 // make a copy of the input array
623 }
640 that._models = new_models.slice();
624
641 return Promise.all(added_views).then(function(added) {
625 var first_removed = i;
642 Array.prototype.splice.apply(that.views, [first_removed, that.views.length].concat(added));
626 // Remove the non-matching items from the old list.
643 return that.views;
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)
644 });
631 });
645 });
632 }
646 return this.state_change;
633
634 // Add the rest of the new list items.
635 for (; i < new_models.length; i++) {
636 this.views.push(Promise.resolve(create.call(context, new_models[i])));
637 }
638 // make a copy of the input array
639 this._models = new_models.slice();
647 },
640 },
648
641
649 remove: function() {
642 remove: function() {
@@ -653,14 +646,13 b' define(["widgets/js/manager",'
653 * returns a promise that resolves after this removal is done
646 * returns a promise that resolves after this removal is done
654 */
647 */
655 var that = this;
648 var that = this;
656 this.state_change = this.state_change.then(function() {
649 Promise.all(this.views).then(function(views) {
657 for (var i = 0; i < that.views.length; i++) {
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 that.views = [];
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