PDA

View Full Version : Retrieving an object and updating an object property in an object array



DaveC426913
22 Dec 2010, 2:24 PM
Say I've got an array of objects:



arrDepts = [];

objDept ={};
objDept.id='1'
objDept.title='Bakery'
arrDepts.push(objDept);

objDept ={};
objDept.id='2'
objDept.title='Dairy'
arrDepts.push(objDept);


So, I've got
arrDepts[0] {id:'1',title:'Bakery'}
arrDepts[1] {id:'2',title:'Dairy'}

1] I want to efficiently (without manually looping) update the title ('Meat') of an object, given its id (1).
so that
arrDepts[0] {id:'1',title:'Meat'}
arrDepts[1] {id:'2',title:'Dairy'}

2] Is there an efficient way (without manually looping) of simply retrieving an object ({id:'2',title:'Dairy'}) given its id (2)?

3] Or - If I'm making my data objects persistent to use them over and over - even though they're fairly simple - are there Sencha objects & methods that would save me time and effort? Is that what Store is for?

evant
22 Dec 2010, 3:08 PM
Use a store, it does all of that.

RWaters
22 Dec 2010, 11:18 PM
Also perhaps take a look at Ext.util.MixedCollection if you don't need all the complexity of a store/reader/proxy.

DaveC426913
23 Dec 2010, 6:48 AM
Hm. OK. The API docs are pretty basic on stores, and it's a big topic.

For example, the store has a config property url:

I'm not sure how I reconcile that with my myriad API AJAX calls:


getList: function(objCaller,arrStatus){
Ext.Ajax.request({
url: CONCIERGE.Config.baseURL + "/v1/shoppinglist/get",
method: 'POST',
headers: {'Srn-Auth-Token': CONCIERGE.Config.SrnAuthToken},
params: {
'api_key': CONCIERGE.Config.apikey,
'status': arrStatus
},
...

deleteItem: function(objCaller, itemUID){
Ext.Ajax.request({
url: CONCIERGE.Config.baseURL + "/v1/shoppinglist/delete",
method: 'POST',
headers: {'Srn-Auth-Token': CONCIERGE.Config.SrnAuthToken},
params: {
'api_key': CONCIERGE.Config.apikey,
'entry_id': itemUID,
},
...


Man, this is blowing up in my face. Implementing this is going to set me back by a week.

evant
23 Dec 2010, 12:58 PM
Surely then, if it's going to take that long, you should just write a simple loop to find the object. It'd be around 3 lines?

DaveC426913
23 Dec 2010, 1:03 PM
Yeah. I was torn between doing it properly with provided objects, and just patching it for now.

I came across this complication while in the middle of getting other functionality going, so I think what I'll do is patch it to hold while I continue to get everything else working the way I like, then at a logical break, I'll come back and implement this better.

One of the downsides to Agile Methodology is that it promotes false starts that require refactoring once the app gets fleshed out.