ractivejs - Getting Ractive data by, say, "id", rather than by the object index -


say ractive data looks this:

items: [   { id: 16, name: "thingy" },   { id: 23, name: "other thingy"} ] 

i know can first item:

ractive.get('items.0') 

but how (or delete, or update, matter) item who's id 23?

mostly javascript issue, put methods on ractive instance or on prototype generally. assuming array not large , using find , findindex, like:

ractive.prototype.getindexbyid = function(keypath, id){     this.get(keypath).findindex(function(each){         return each.id === id;     }); }  ractive.prototype.getbyid = function(keypath, id){     return this.get(keypath).find(function(each){         return each.id === id;     }); }  ractive.prototype.delete = function(keypath, id){     return this.splice(keypath, this.getindexbyid(id), 1); }  ractive.prototype.update = function(keypath, id, data){     return this.set(keypath + '.' + this.getindexbyid(id), data); } 

but if you're trying handle item action occurred, should use context:

{{#items:i}} <li on-click='selected'>{{name}}</li> <!-- or --> <li on-click='selected(this, i)'>{{name}}</li> {{/items}} 

in code

new ractive({     ...     selected: function(item, index){         // in lieu of passing in, can access via this.event:         var item = this.event.context // current array member         var index = this.event.index.i // current index     },     oninit: function(){         this.on('selected', function(){             // same method above         }     } 

Comments

Popular posts from this blog

tcpdump - How to check if server received packet (acknowledged) -