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; |
|
683 | // first, skip past the beginning of the lists if they are identical | |
686 | this.state_change = this.state_change.then(function() { |
|
684 | for (; i < new_models.length; i++) { | |
687 | var i; |
|
685 | if (i >= this._models.length || new_models[i] !== this._models[i]) { | |
688 | // first, skip past the beginning of the lists if they are identical |
|
686 | break; | |
689 | for (i = 0; i < new_models.length; i++) { |
|
|||
690 | if (i >= that._models.length || new_models[i] !== that._models[i]) { |
|
|||
691 | break; |
|
|||
692 | } |
|
|||
693 | } |
|
|||
694 | var first_removed = i; |
|
|||
695 | // Remove the non-matching items from the old list. |
|
|||
696 | for (var j = first_removed; j < that._models.length; j++) { |
|
|||
697 | remove.call(context, that.views[j]); |
|
|||
698 | } |
|
|||
699 |
|
||||
700 | // Add the rest of the new list items. |
|
|||
701 | for (; i < new_models.length; i++) { |
|
|||
702 | added_views.push(create.call(context, new_models[i])); |
|
|||
703 | } |
|
687 | } | |
704 | // make a copy of the input array |
|
688 | } | |
705 | that._models = new_models.slice(); |
|
689 | ||
706 | return Promise.all(added_views).then(function(added) { |
|
690 | var first_removed = i; | |
707 | Array.prototype.splice.apply(that.views, [first_removed, that.views.length].concat(added)); |
|
691 | // Remove the non-matching items from the old list. | |
708 | return that.views; |
|
692 | var removed = this.views.splice(first_removed, this.views.length-first_removed); | |
|
693 | for (var j = 0; j < removed.length; j++) { | |||
|
694 | removed[j].then(function(view) { | |||
|
695 | remove.call(context, view) | |||
709 | }); |
|
696 | }); | |
710 |
} |
|
697 | } | |
711 | return this.state_change; |
|
698 | ||
|
699 | // Add the rest of the new list items. | |||
|
700 | for (; i < new_models.length; i++) { | |||
|
701 | this.views.push(Promise.resolve(create.call(context, new_models[i]))); | |||
|
702 | } | |||
|
703 | // make a copy of the input array | |||
|
704 | this._models = new_models.slice(); | |||
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