@@ -96,28 +96,71 @@ function Collection(attributes) {
9696 this . events . changed = new ModelEvent ( this ) ;
9797 this . events . erased = new ModelEvent ( this ) ;
9898 this . events . inserted = new ModelEvent ( this ) ;
99+ this . events . reset = new ModelEvent ( this ) ;
99100 this . events . click = new ModelEvent ( this ) ;
100101 this . events . contextmenu = new ModelEvent ( this ) ;
101102} ;
102103
103- Collection . prototype . add = function ( item ) {
104- item . events . changed . attach ( this . on_item_changed ) ;
104+ Collection . prototype . add = function ( item , suppressevent ) {
105+ const _add_model = ( m , se ) => {
106+ m . events . changed . attach ( this . on_item_changed ) ;
105107
106- this . items . push ( item ) ;
107- this . events . inserted . notify ( item ) ;
108+ this . items . push ( m ) ;
108109
109- $ ( '#' + item . uid ) . off ( 'click contextmenu' ) ;
110- $ ( '#' + item . uid ) . on ( 'click' , item , this . on_item_click ) ;
111- $ ( '#' + item . uid ) . on ( 'contextmenu' , item , this . on_item_ctxmenu ) ;
110+ // if ( !(suppressevent === true) )
111+ // this.events.inserted.notify(m);
112+
113+ // $('#' + m.uid).off('click contextmenu')
114+ // .on('click', m, this.on_item_click)
115+ // .on('contextmenu', m, this.on_item_ctxmenu);
116+ }
117+
118+ if ( item instanceof Array ) {
119+ const items = item ;
120+ items . forEach ( i => {
121+ _add_model ( i , true ) ;
122+ } ) ;
123+
124+ // if ( !(suppressevent === true) )
125+ this . events . inserted . notify ( items ) ;
126+
127+ items . forEach ( i => {
128+ $ ( '#' + i . uid ) . off ( 'click contextmenu' )
129+ . on ( 'click' , i , this . on_item_click )
130+ . on ( 'contextmenu' , i , this . on_item_ctxmenu ) ;
131+ } ) ;
132+ } else {
133+ _add_model ( item )
134+
135+ // if ( !(suppressevent === true) )
136+ this . events . inserted . notify ( item ) ;
137+
138+ $ ( '#' + item . uid ) . off ( 'click contextmenu' )
139+ . on ( 'click' , item , this . on_item_click )
140+ . on ( 'contextmenu' , item , this . on_item_ctxmenu ) ;
141+ }
112142} ;
113143
144+ Collection . prototype . set = function ( items ) {
145+ if ( items instanceof Array ) {
146+ this . empty ( true ) ;
147+
148+ items . forEach ( i => {
149+ this . add ( i , true ) ;
150+ } ) ;
151+
152+ // this.items = items;
153+ this . events . reset . notify ( items ) ;
154+ }
155+ }
156+
114157Collection . prototype . find = function ( key , val ) {
115158 return this . items . find ( function ( elem , i , arr ) {
116159 return elem [ key ] == val ;
117160 } ) ;
118161} ;
119162
120- Collection . prototype . empty = function ( ) {
163+ Collection . prototype . empty = function ( suppressevent ) {
121164 this . items . forEach ( function ( model , i , a ) {
122165 $ ( '#' + model . uid ) . off ( ) ;
123166 } ) ;
@@ -126,7 +169,8 @@ Collection.prototype.empty = function() {
126169
127170 if ( ! ! this . list ) this . view . find ( this . list ) . empty ( ) ;
128171
129- this . events . erased . notify ( ) ;
172+ if ( ! ( suppressevent === true ) )
173+ this . events . erased . notify ( ) ;
130174} ;
131175
132176Collection . prototype . size = function ( ) {
0 commit comments