How to add element to the beginning of the store - sencha-touch-2

The add method adds the menuPoint to the end of the store, how can I add it to the beginning instead?
Have gone through all methods in the store documentation, but cant find a method putting the element to the beginning of the store.
http://docs.sencha.com/touch/2-0/#!/api/Ext.data.Store
var store = Ext.getStore('AdultMenuStore');//adultmenulist.getStore();
for(var i=0; i < adultObj.children.length; i++){
var child = adultObj.children[i];
var menuPoint = Ext.create('Sencha.model.MenuPoint',
{
id: child.childId,
name: child.firstName,
icon_url: 'http://kidsparent.no/images/adultMenu/bkids-home-profile.png', xtype: 'childmenu'
});
store.add(menuPoint);
}

Just use insert method instead:
insert( Number index, Ext.data.Model[] records )

Related

how to use primaryKey in grid model in extjs4

I have a grid panel with one grid model. How can I set primary key to that grid store so that I can prevent duplicate values. My model is:
Ext.define('product',{
extend : 'Ext.data.Model',
fields: [{name: 'name',type: 'string'},
{name: 'column1', type: 'int'},
{name: 'column2', type: 'int'}]
associations: [{type: 'belongsTo',model: 'product',primaryKey: 'column1'}]
});
How can I use this primary key to prevent entry of same record twice?!
You can't 'set' a primary key on a model with Extjs. The concept itself does not exist in the library. The ID of the record needs to be unique, but there is really nothing that enforces it to be so. My question for you is, what do you even want done when a duplicate record is added? Do you want the values of the record to overwrite based on the key? Do you want an exception?
The best suggestion I can have for you is to use the 'add' event on the store and fill it with code something like this:
var myStore = Ext.create("Ext.data.Store", {
listeners: {
add: function(store, records){
var checkRecords = store.getRange();//get the records to check against
//clean out the records from the set that were just added
for(var i = 0; i < records.length; i++)
Ext.Array.remove(checkRecords, records[i]);
for(var i = 0; i < checkRecords.length; i++)
{
for( var j = 0; j < records.length; j++)
{
if(checkRecords[i].get("primary_key") == records[j].get("primary_key"))
throw "Duplicate primary key: " + records[j].get("primary_key");
}
}
}
}
});
That code will throw an exception but not stop the record for being added.
what about idProperty??
if you set an idProperty on your model it acts as a primary key.

How to empty the datastore in sencha?

I am new to sencha touch. I have a datastore and i want to empty the datastore when the back button is clicked. Is there any method like refresh used to empty the datastore? Can anyone show me a bit of code on it?
This is not working in my case:
{
text: 'Back',
ui: 'back',
width: 50,
handler: function() {
var records = iPolis.journalListStore.getRange();
iPolis.journalListStore.remove(records);
iPolis.Viewport.setActiveItem('journalPanel', {
type: 'slide',
direction:'right'
});
}
}
you can use store.getRange(); to get all records of an Ext.data.Store and then pass those records to store.remove(); function to remove it form store
var records = store.getRange();
store.remove(records);
Ok, i found a solution to my question, i will post it here maby it will help someone.
the problem is that the store was not loaded so i have to getRange after the store load, here is the code :
StoreTrajectoire.on('load',function(store,records,opts){
var points = [];
points =StoreTrajectoire.getRange();
}
this will return an array of objects, to read the array data :
for(var i = 0; i < store.getCount(); i++ ) {
var record = store.getAt(i);
var data = record.get('your attribue')
points.push(p);
console.log(points[i]);
}
wish that can help you.

Statically defining data in store on store creation

I want to create a simple store that can be reused containing only years.
Ext.define('Workshop.store.YearsStore',
{
extend: 'Ext.data.Store',
fields: ['id', 'type'],
constructor: function(config)
{
var years = [];
for(var n=1972;n<=Ext.Date.format(new Date(), 'Y');n++)
{
years.push({id: n, type: n});
}
config.data = years;
this.initConfig(config);
return this;
}
});
This does not work, how can I define a static set of data on store creation?
Few things to correct:
You do not need to call initConfig. initConfig is intended to add getters & setters in the properties contained in the config properties in the class. In this case, you will just need to call callParent. It should take care the rest for you.
You forgot to take care of the case when config is not defined. config could be null, and your line config.data will raise config is undefined in some cases.
Use native (new Date()).getFullYear() seems to be better?
Below is the modified code, and link to demo.
Ext.define('Workshop.store.YearsStore', {
extend: 'Ext.data.Store',
fields: ['id', 'type'],
startYear: 1972,
endYear: (new Date()).getFullYear(),
constructor: function(cfg) {
var me = this;
//We init the configurations first (to copy startYear and endYear)
Ext.apply(me, cfg || {});
me.data = [];
//Then we push data
for(var n = me.startYear ; n <= me.endYear ; n++) {
me.data.push({id: n, type: n});
}
//Then finally we callparent to init this store.
me.callParent([cfg]);
}
});

Problem with list search in sencha

I am using search option in the list . There is provision to add new items to the list. The problem is that when I add a new item to the list, the list is getting updated , but I cannot search that item through the search field. But after refreshing the browser, we can. But it is not possible to refresh browser each time....... Is there any solution for this problem?
Here is the code I am using to search the list.
xtype: 'searchfield',
placeHolder: 'Search',
name: 'searchfield',
id:'subListSearch',
listeners : {
scope: this,
'focus': function() {
Ext.getCmp('xbtn').show();
},
keyup: function(field) {
var value = field.getValue();
if (!value) {
Store.filterBy(function() {
return true;
});
} else {
var searches = value.split(' '),
regexps = [],
i;
for (i = 0; i < searches.length; i++) {
if (!searches[i]) return;
regexps.push(new RegExp(searches[i], 'i'));
};
Store.filterBy(function(record) {
var matched = [];
for (i = 0; i < regexps.length; i++) {
var search = regexps[i];
if (record.get('Name').match(search)) matched.push(true);
else matched.push(false);
};
if (regexps.length > 1 && matched.indexOf(false) != -1) {
return false;
} else {
return matched[0];
}
});
}
}
}
There is also some other problems. I using some provision to filter the list. But when I uses the search option, it is searching through the entire list, not the filtered list.why?
Thanks
Arun A G
Thanks for responding to my question .
The problem is fixed with bindStore() method. Earlier I was doing load() method to render the new data into the store. But we can not search the last entered item with this method. After binding the Changed store into the list with bindStore() method, the issue was solved.

Dojo populate combo box widget dynamically

Could someone please explain to me why this simple straight forward code isnt working,
var serviceStore = new dojo.data.ItemFileWriteStore({
data: {identifier: "serviceCode",items:[]}
});
//jsonObj is a json object that I obtain from the server via AJAX
for(var i = 0; i<jsonObj.length;i++){
serviceStore.newItem({serviceCode: jsonObj[i]});
}
var serviceFilterSelect = dojo.byId('serviceSelect');
serviceFilterSelect.store = serviceStore;
There is no error at all displayed but my combobox with the id "serviceSelect" doesn't display any options, the combo is declared in the html section of my code,
<input dojoType = "dijit.form.ComboBox" id="serviceSelect"></input>
Any pointers towards the right direction will be much appreciated.
First of all you should use dijit.byId to get dojo widget instead of dojo.byId.
Also every item in jsonObj should contains field "name". This field will be displayed in combobox. E.g:
dojo.require("dojo.data.ItemFileWriteStore");
dojo.require("dijit.form.ComboBox");
var storeData = {
identifier: 'serviceCode',
items: []
}
var jsonObj = [{
serviceCode: 'sc1',
name: 'serviceCode1'
},
{
serviceCode: 'sc2',
name: 'serviceCode2'
}]
dojo.addOnLoad(function () {
var serviceStore = new dojo.data.ItemFileWriteStore({ data: storeData });
for (var i = 0; i < jsonObj.length; i++) {
serviceStore.newItem(jsonObj[i]);
}
var serviceFilterSelect = dijit.byId('serviceSelect');
serviceFilterSelect.attr('store', serviceStore);
});
And HTML:
<select dojotype="dijit.form.ComboBox" id="serviceSelect" ></select>
It seems that it works.
I can't tell from the code you posted, but if you're having trouble getting the DOM nodes, they may not had a chance to get loaded.
You can try wrapping what you have above with a dojo.ready(function(){ ... });.
Have you put items in your store? I can't tell from the sample that you posted.
var serviceStore = new dojo.data.ItemFileWriteStore({
data: {
identifier: "serviceCode"
,items: [
{serviceCode:'ec', name:'Ecuador'}
,{serviceCode:'eg', name:'Egypt'}
,{serviceCode:'sv', name:'El Salvador'}
]
}
});
For dojo >= 1.6:
dojo.byId('serviceSelect').store=serviceStore;
For dojo < 1.6:
dojo.byId('serviceSelect').attr("store",serviceStore);