Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Allow ModelList add() fn to receive an index option #120

Merged
merged 1 commit into from

4 participants

@ghinch

In it's current state, ModelLists will always override any "index" option passed with new data, which results in the new records always being appended at the end rather than at a desired index. This fixes that issue.

@rgrove rgrove was assigned
@ericf ericf commented on the diff
src/app/js/model-list.js
@@ -902,7 +902,7 @@ Y.ModelList = Y.extend(ModelList, Y.Base, {
}
facade = Y.merge(options, {
- index: this._findIndex(model),
+ index: options.index || this._findIndex(model),
@ericf Owner
ericf added a note

This won't account for an index of 0. This might be a better way:

'index' in options ? options.index : this._findIndex(model)
@rgrove
rgrove added a note

Agreed. @ghinch, I'm in the process of merging this in now, so I'll go ahead and fix this. Thanks for the patch!

@ghinch
ghinch added a note

Ah good point. Maybe better to use YUI? :)

Y.Object.owns(options, 'index') ? options.index : this._findIndex(model)
@rgrove
rgrove added a note

Nah, in is much faster here, and we don't need the hasOwnProperty() check.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@yuibuild yuibuild merged commit dec656a into from
@rgrove rgrove was unassigned by jlecomte
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
2  build/model-list/model-list-debug.js
@@ -904,7 +904,7 @@ Y.ModelList = Y.extend(ModelList, Y.Base, {
}
facade = Y.merge(options, {
- index: this._findIndex(model),
+ index: options.index || this._findIndex(model),
model: model
});
View
2  build/model-list/model-list-min.js
@@ -1 +1 @@
-YUI.add("model-list",function(b){var g=b.Attribute.prototype,j=b.Lang,k=b.Array,c="add",e="create",h="error",i="load",d="remove",f="reset";function a(){a.superclass.constructor.apply(this,arguments);}b.ModelList=b.extend(a,b.Base,{model:b.Model,_isYUIModelList:true,initializer:function(m){m||(m={});var l=this.model=m.model||this.model;if(typeof l==="string"){this.model=b.Object.getValue(b,l.split("."));if(!this.model){b.error("ModelList: Model class not found: "+l);}}this.publish(c,{defaultFn:this._defAddFn});this.publish(f,{defaultFn:this._defResetFn});this.publish(d,{defaultFn:this._defRemoveFn});this.after("*:idChange",this._afterIdChange);this._clear();},destructor:function(){k.each(this._items,this._detachList,this);},add:function(n,l){var m=n._isYUIModelList;if(m||j.isArray(n)){return k.map(m?n.toArray():n,function(o){return this._add(o,l);},this);}else{return this._add(n,l);}},create:function(n,m,o){var l=this;if(typeof m==="function"){o=m;m={};}m||(m={});if(!n._isYUIModel){n=new this.model(n);}l.fire(e,b.merge(m,{model:n}));return n.save(m,function(p){if(!p){l.add(n,m);}o&&o.apply(null,arguments);});},each:function(q,p){var m=this._items.concat(),n,o,l;for(n=0,l=m.length;n<l;n++){o=m[n];q.call(p||o,o,n,this);}return this;},filter:function(o,s){var n=[],m=this._items,p,q,l,r;if(typeof o==="function"){s=o;o={};}for(p=0,l=m.length;p<l;++p){q=m[p];if(s.call(this,q,p,this)){n.push(q);}}if(o.asList){r=new b.ModelList({model:this.model});n.length&&r.add(n,{silent:true});return r;}else{return n;}},get:function(l){if(this.attrAdded(l)){return g.get.apply(this,arguments);}return this.invoke("get",l);},getAsHTML:function(l){if(this.attrAdded(l)){return b.Escape.html(g.get.apply(this,arguments));}return this.invoke("getAsHTML",l);},getAsURL:function(l){if(this.attrAdded(l)){return encodeURIComponent(g.get.apply(this,arguments));}return this.invoke("getAsURL",l);},getByClientId:function(l){return this._clientIdMap[l]||null;},getById:function(l){return this._idMap[l]||null;},invoke:function(m){var l=[this._items,m].concat(k(arguments,1,true));return k.invoke.apply(k,l);},load:function(m,n){var l=this;if(typeof m==="function"){n=m;m={};}m||(m={});this.sync("read",m,function(r,p){var q={options:m,response:p},o;if(r){q.error=r;q.src="load";l.fire(h,q);}else{if(!l._loadEvent){l._loadEvent=l.publish(i,{preventable:false});}o=q.parsed=l.parse(p);l.reset(o,m);l.fire(i,q);}n&&n.apply(null,arguments);});return this;},map:function(l,m){return k.map(this._items,l,m);},parse:function(l){if(typeof l==="string"){try{return b.JSON.parse(l)||[];}catch(m){this.fire(h,{error:m,response:l,src:"parse"});return null;}}return l||[];},remove:function(n,l){var m=n._isYUIModelList;if(m||j.isArray(n)){return k.map(m?n.toArray():n,function(o){return this._remove(o,l);},this);}else{return this._remove(n,l);}},reset:function(n,l){n||(n=[]);l||(l={});var m=b.merge({src:"reset"},l);if(n._isYUIModelList){n=n.toArray();}else{n=k.map(n,function(o){return o._isYUIModel?o:new this.model(o);},this);}m.models=n;if(l.silent){this._defResetFn(m);}else{if(this.comparator){n.sort(b.bind(this._sort,this));}this.fire(f,m);}return this;},some:function(q,p){var m=this._items.concat(),n,o,l;for(n=0,l=m.length;n<l;n++){o=m[n];if(q.call(p||o,o,n,this)){return true;}}return false;},sort:function(l){if(!this.comparator){return this;}var n=this._items.concat(),m;l||(l={});n.sort(b.bind(this._sort,this));m=b.merge(l,{models:n,src:"sort"});l.silent?this._defResetFn(m):this.fire(f,m);return this;},sync:function(){var l=k(arguments,0,true).pop();if(typeof l==="function"){l();}},toArray:function(){return this._items.concat();},toJSON:function(){return this.map(function(l){return l.toJSON();});},_add:function(m,l){var n,o;l||(l={});if(!m._isYUIModel){m=new this.model(m);}o=m.get("id");if(this._clientIdMap[m.get("clientId")]||(j.isValue(o)&&this._idMap[o])){this.fire(h,{error:"Model is already in the list.",model:m,src:"add"});return;}n=b.merge(l,{index:this._findIndex(m),model:m});l.silent?this._defAddFn(n):this.fire(c,n);return m;},_attachList:function(l){l.lists.push(this);l.addTarget(this);},_clear:function(){k.each(this._items,this._detachList,this);this._clientIdMap={};this._idMap={};this._items=[];},_compare:function(m,l){return m<l?-1:(m>l?1:0);},_detachList:function(m){var l=k.indexOf(m.lists,this);if(l>-1){m.lists.splice(l,1);m.removeTarget(this);}},_findIndex:function(o){var m=this._items,l=m.length,p=0,q,n,r;if(!this.comparator||!l){return l;}r=this.comparator(o);while(p<l){n=(p+l)>>1;q=m[n];if(this._compare(this.comparator(q),r)<0){p=n+1;}else{l=n;}}return p;},_remove:function(n,m){var l=this.indexOf(n),o;m||(m={});if(l===-1){this.fire(h,{error:"Model is not in the list.",model:n,src:"remove"});return;}o=b.merge(m,{index:l,model:n});m.silent?this._defRemoveFn(o):this.fire(d,o);return n;},_sort:function(m,l){return this._compare(this.comparator(m),this.comparator(l));},_afterIdChange:function(l){if(j.isValue(l.prevVal)){delete this._idMap[l.prevVal];}if(j.isValue(l.newVal)){this._idMap[l.newVal]=l.target;}},_defAddFn:function(m){var l=m.model,n=l.get("id");this._clientIdMap[l.get("clientId")]=l;if(j.isValue(n)){this._idMap[n]=l;}this._attachList(l);this._items.splice(m.index,0,l);},_defRemoveFn:function(m){var l=m.model,n=l.get("id");this._detachList(l);delete this._clientIdMap[l.get("clientId")];if(j.isValue(n)){delete this._idMap[n];}this._items.splice(m.index,1);},_defResetFn:function(l){if(l.src==="sort"){this._items=l.models.concat();return;}this._clear();if(l.models.length){this.add(l.models,{silent:true});}}},{NAME:"modelList"});b.augment(a,b.ArrayList);},"@VERSION@",{requires:["array-extras","array-invoke","arraylist","base-build","escape","json-parse","model"]});
+YUI.add("model-list",function(b){var g=b.Attribute.prototype,j=b.Lang,k=b.Array,c="add",e="create",h="error",i="load",d="remove",f="reset";function a(){a.superclass.constructor.apply(this,arguments);}b.ModelList=b.extend(a,b.Base,{model:b.Model,_isYUIModelList:true,initializer:function(m){m||(m={});var l=this.model=m.model||this.model;if(typeof l==="string"){this.model=b.Object.getValue(b,l.split("."));if(!this.model){b.error("ModelList: Model class not found: "+l);}}this.publish(c,{defaultFn:this._defAddFn});this.publish(f,{defaultFn:this._defResetFn});this.publish(d,{defaultFn:this._defRemoveFn});this.after("*:idChange",this._afterIdChange);this._clear();},destructor:function(){k.each(this._items,this._detachList,this);},add:function(n,l){var m=n._isYUIModelList;if(m||j.isArray(n)){return k.map(m?n.toArray():n,function(o){return this._add(o,l);},this);}else{return this._add(n,l);}},create:function(n,m,o){var l=this;if(typeof m==="function"){o=m;m={};}m||(m={});if(!n._isYUIModel){n=new this.model(n);}l.fire(e,b.merge(m,{model:n}));return n.save(m,function(p){if(!p){l.add(n,m);}o&&o.apply(null,arguments);});},each:function(q,p){var m=this._items.concat(),n,o,l;for(n=0,l=m.length;n<l;n++){o=m[n];q.call(p||o,o,n,this);}return this;},filter:function(o,s){var n=[],m=this._items,p,q,l,r;if(typeof o==="function"){s=o;o={};}for(p=0,l=m.length;p<l;++p){q=m[p];if(s.call(this,q,p,this)){n.push(q);}}if(o.asList){r=new b.ModelList({model:this.model});n.length&&r.add(n,{silent:true});return r;}else{return n;}},get:function(l){if(this.attrAdded(l)){return g.get.apply(this,arguments);}return this.invoke("get",l);},getAsHTML:function(l){if(this.attrAdded(l)){return b.Escape.html(g.get.apply(this,arguments));}return this.invoke("getAsHTML",l);},getAsURL:function(l){if(this.attrAdded(l)){return encodeURIComponent(g.get.apply(this,arguments));}return this.invoke("getAsURL",l);},getByClientId:function(l){return this._clientIdMap[l]||null;},getById:function(l){return this._idMap[l]||null;},invoke:function(m){var l=[this._items,m].concat(k(arguments,1,true));return k.invoke.apply(k,l);},load:function(m,n){var l=this;if(typeof m==="function"){n=m;m={};}m||(m={});this.sync("read",m,function(r,p){var q={options:m,response:p},o;if(r){q.error=r;q.src="load";l.fire(h,q);}else{if(!l._loadEvent){l._loadEvent=l.publish(i,{preventable:false});}o=q.parsed=l.parse(p);l.reset(o,m);l.fire(i,q);}n&&n.apply(null,arguments);});return this;},map:function(l,m){return k.map(this._items,l,m);},parse:function(l){if(typeof l==="string"){try{return b.JSON.parse(l)||[];}catch(m){this.fire(h,{error:m,response:l,src:"parse"});return null;}}return l||[];},remove:function(n,l){var m=n._isYUIModelList;if(m||j.isArray(n)){return k.map(m?n.toArray():n,function(o){return this._remove(o,l);},this);}else{return this._remove(n,l);}},reset:function(n,l){n||(n=[]);l||(l={});var m=b.merge({src:"reset"},l);if(n._isYUIModelList){n=n.toArray();}else{n=k.map(n,function(o){return o._isYUIModel?o:new this.model(o);},this);}m.models=n;if(l.silent){this._defResetFn(m);}else{if(this.comparator){n.sort(b.bind(this._sort,this));}this.fire(f,m);}return this;},some:function(q,p){var m=this._items.concat(),n,o,l;for(n=0,l=m.length;n<l;n++){o=m[n];if(q.call(p||o,o,n,this)){return true;}}return false;},sort:function(l){if(!this.comparator){return this;}var n=this._items.concat(),m;l||(l={});n.sort(b.bind(this._sort,this));m=b.merge(l,{models:n,src:"sort"});l.silent?this._defResetFn(m):this.fire(f,m);return this;},sync:function(){var l=k(arguments,0,true).pop();if(typeof l==="function"){l();}},toArray:function(){return this._items.concat();},toJSON:function(){return this.map(function(l){return l.toJSON();});},_add:function(m,l){var n,o;l||(l={});if(!m._isYUIModel){m=new this.model(m);}o=m.get("id");if(this._clientIdMap[m.get("clientId")]||(j.isValue(o)&&this._idMap[o])){this.fire(h,{error:"Model is already in the list.",model:m,src:"add"});return;}n=b.merge(l,{index:l.index||this._findIndex(m),model:m});l.silent?this._defAddFn(n):this.fire(c,n);return m;},_attachList:function(l){l.lists.push(this);l.addTarget(this);},_clear:function(){k.each(this._items,this._detachList,this);this._clientIdMap={};this._idMap={};this._items=[];},_compare:function(m,l){return m<l?-1:(m>l?1:0);},_detachList:function(m){var l=k.indexOf(m.lists,this);if(l>-1){m.lists.splice(l,1);m.removeTarget(this);}},_findIndex:function(o){var m=this._items,l=m.length,p=0,q,n,r;if(!this.comparator||!l){return l;}r=this.comparator(o);while(p<l){n=(p+l)>>1;q=m[n];if(this._compare(this.comparator(q),r)<0){p=n+1;}else{l=n;}}return p;},_remove:function(n,m){var l=this.indexOf(n),o;m||(m={});if(l===-1){this.fire(h,{error:"Model is not in the list.",model:n,src:"remove"});return;}o=b.merge(m,{index:l,model:n});m.silent?this._defRemoveFn(o):this.fire(d,o);return n;},_sort:function(m,l){return this._compare(this.comparator(m),this.comparator(l));},_afterIdChange:function(l){if(j.isValue(l.prevVal)){delete this._idMap[l.prevVal];}if(j.isValue(l.newVal)){this._idMap[l.newVal]=l.target;}},_defAddFn:function(m){var l=m.model,n=l.get("id");this._clientIdMap[l.get("clientId")]=l;if(j.isValue(n)){this._idMap[n]=l;}this._attachList(l);this._items.splice(m.index,0,l);},_defRemoveFn:function(m){var l=m.model,n=l.get("id");this._detachList(l);delete this._clientIdMap[l.get("clientId")];if(j.isValue(n)){delete this._idMap[n];}this._items.splice(m.index,1);},_defResetFn:function(l){if(l.src==="sort"){this._items=l.models.concat();return;}this._clear();if(l.models.length){this.add(l.models,{silent:true});}}},{NAME:"modelList"});b.augment(a,b.ArrayList);},"@VERSION@",{requires:["array-extras","array-invoke","arraylist","base-build","escape","json-parse","model"]});
View
2  build/model-list/model-list.js
@@ -904,7 +904,7 @@ Y.ModelList = Y.extend(ModelList, Y.Base, {
}
facade = Y.merge(options, {
- index: this._findIndex(model),
+ index: options.index || this._findIndex(model),
model: model
});
View
2  src/app/js/model-list.js
@@ -902,7 +902,7 @@ Y.ModelList = Y.extend(ModelList, Y.Base, {
}
facade = Y.merge(options, {
- index: this._findIndex(model),
+ index: options.index || this._findIndex(model),
@ericf Owner
ericf added a note

This won't account for an index of 0. This might be a better way:

'index' in options ? options.index : this._findIndex(model)
@rgrove
rgrove added a note

Agreed. @ghinch, I'm in the process of merging this in now, so I'll go ahead and fix this. Thanks for the patch!

@ghinch
ghinch added a note

Ah good point. Maybe better to use YUI? :)

Y.Object.owns(options, 'index') ? options.index : this._findIndex(model)
@rgrove
rgrove added a note

Nah, in is much faster here, and we don't need the hasOwnProperty() check.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
model: model
});
Something went wrong with that request. Please try again.