javascript - Dojo: Dealing with a priority list (ordered list) when using a Store -
ice cream! want make list of ice creams have tried , keep list ordered favourite ice creams @ top , least favourite @ bottom. (the list should ordered how delicious ice cream is.
if model in array can this:
var icecreamordredlist = [ {name: "coconut"}, {name: "mint chocolate chip"}, ... {name: "banana"} /* noone likes banana ice cream*/ } the order of array indicates coconut ice cream preferred banana.
if want display ice cream preferences in dijit or dgrid, need wrap array in store. memory store obvious choice. means doing this:
var icecreamstore = new memory([data: icecreamordredlist]); whenever try new ice cream want add data structure , allow of dijits/dgrids update. means should declare store instead:
var icecreamstore = new observable(new memory({data: icecreamordredlist})); this part works well.
one issue haven't been able resolve if asks me how answer it:
'root beer' ice cream favourite. position of 'root beer' in priority list?
i can find 'root beer' in store doing this:
var someicecream = icecreamstore.query({name: "root beer"}); but not allow me access position of 'root beer' in store.
i this:
for(var i=0; i<icecreamstore.data.length; i++){ if(icecreamstore.data[i].name == "root beer"){ return i; } } but seems 1) heavy 2) out of spirit of store api.
doing opposite tricky. answering question:
what nth favourite ice cream
requires along lines of:
return icecreamstore.data[n] this seems unstore like.
reordering elements in store challenging. there patterns dealing reordering? attach priority field onto ice cream. ie:
{name: "cotton candy", order: 33} but means when insert or delete ice cream need update order property on ice creams. yuck.
if ice cream names unique, set memory stores idproperty field "name". can stuff icecreamstore.index["root beer"] because dojo stores keep internal index on position id at. here example based on code:
var icecreamstore = new observable(new memory({data: icecreamordredlist, idproperty: "name"})); and particular index (which should same order), be:
var priority = icecreamstore.index["root beer"];
Comments
Post a Comment