Show More
@@ -661,10 +661,9 b' define(["widgets/js/manager",' | |||||
661 |
|
661 | |||
662 | _.extend(ViewList.prototype, { |
|
662 | _.extend(ViewList.prototype, { | |
663 | initialize: function(create_view, remove_view, context) { |
|
663 | initialize: function(create_view, remove_view, context) { | |
664 | this.state_change = Promise.resolve(); |
|
|||
665 | this._handler_context = context || this; |
|
664 | this._handler_context = context || this; | |
666 | this._models = []; |
|
665 | this._models = []; | |
667 | this.views = []; |
|
666 | this.views = []; // list of promises for views | |
668 | this._create_view = create_view; |
|
667 | this._create_view = create_view; | |
669 | this._remove_view = remove_view || function(view) {view.remove();}; |
|
668 | this._remove_view = remove_view || function(view) {view.remove();}; | |
670 | }, |
|
669 | }, | |
@@ -673,42 +672,36 b' define(["widgets/js/manager",' | |||||
673 | /** |
|
672 | /** | |
674 | * the create_view, remove_view, and context arguments override the defaults |
|
673 | * the create_view, remove_view, and context arguments override the defaults | |
675 | * specified when the list is created. |
|
674 | * specified when the list is created. | |
676 | * returns a promise that resolves after this update is done |
|
675 | * after this function, the .views attribute is a list of promises for views | |
|
676 | * if you want to perform some action on the list of views, do something like | |||
|
677 | * `Promise.all(myviewlist.views).then(function(views) {...});` | |||
677 | */ |
|
678 | */ | |
678 | var remove = remove_view || this._remove_view; |
|
679 | var remove = remove_view || this._remove_view; | |
679 | var create = create_view || this._create_view; |
|
680 | var create = create_view || this._create_view; | |
680 | if (create === undefined || remove === undefined){ |
|
|||
681 | console.error("Must define a create a remove function"); |
|
|||
682 | } |
|
|||
683 | var context = context || this._handler_context; |
|
681 | var context = context || this._handler_context; | |
684 |
var |
|
682 | var i = 0; | |
685 | var that = this; |
|
|||
686 | this.state_change = this.state_change.then(function() { |
|
|||
687 | var i; |
|
|||
688 |
|
|
683 | // first, skip past the beginning of the lists if they are identical | |
689 |
|
|
684 | for (; i < new_models.length; i++) { | |
690 |
|
|
685 | if (i >= this._models.length || new_models[i] !== this._models[i]) { | |
691 |
|
|
686 | break; | |
692 |
|
|
687 | } | |
693 |
|
|
688 | } | |
|
689 | ||||
694 |
|
|
690 | var first_removed = i; | |
695 |
|
|
691 | // Remove the non-matching items from the old list. | |
696 | for (var j = first_removed; j < that._models.length; j++) { |
|
692 | var removed = this.views.splice(first_removed, this.views.length-first_removed); | |
697 | remove.call(context, that.views[j]); |
|
693 | for (var j = 0; j < removed.length; j++) { | |
|
694 | removed[j].then(function(view) { | |||
|
695 | remove.call(context, view) | |||
|
696 | }); | |||
698 |
|
|
697 | } | |
699 |
|
698 | |||
700 |
|
|
699 | // Add the rest of the new list items. | |
701 |
|
|
700 | for (; i < new_models.length; i++) { | |
702 |
|
|
701 | this.views.push(Promise.resolve(create.call(context, new_models[i]))); | |
703 |
|
|
702 | } | |
704 |
|
|
703 | // make a copy of the input array | |
705 |
|
|
704 | this._models = new_models.slice(); | |
706 | return Promise.all(added_views).then(function(added) { |
|
|||
707 | Array.prototype.splice.apply(that.views, [first_removed, that.views.length].concat(added)); |
|
|||
708 | return that.views; |
|
|||
709 | }); |
|
|||
710 | }); |
|
|||
711 | return this.state_change; |
|
|||
712 | }, |
|
705 | }, | |
713 |
|
706 | |||
714 | remove: function() { |
|
707 | remove: function() { | |
@@ -718,14 +711,13 b' define(["widgets/js/manager",' | |||||
718 | * returns a promise that resolves after this removal is done |
|
711 | * returns a promise that resolves after this removal is done | |
719 | */ |
|
712 | */ | |
720 | var that = this; |
|
713 | var that = this; | |
721 | this.state_change = this.state_change.then(function() { |
|
714 | return Promise.all(this.views).then(function(views) { | |
722 | for (var i = 0; i < that.views.length; i++) { |
|
715 | for (var i = 0; i < that.views.length; i++) { | |
723 |
that._remove_view.call(that._handler_context, |
|
716 | that._remove_view.call(that._handler_context, views[i]); | |
724 | } |
|
717 | } | |
725 | that._models = []; |
|
|||
726 | that.views = []; |
|
718 | that.views = []; | |
|
719 | that._models = []; | |||
727 | }); |
|
720 | }); | |
728 | return this.state_change; |
|
|||
729 | }, |
|
721 | }, | |
730 | }); |
|
722 | }); | |
731 |
|
723 |
General Comments 0
You need to be logged in to leave comments.
Login now