unique field in the model - extjs

i am looking for a solution to make one of the data field as unique field in the model in sencha, somehow i am not able to find documentation for it. Here is the code
Ext.define('handfree.model.CategoryM', {
extend: 'Ext.data.Model',
requires: [
'Ext.data.identifier.Uuid'
],
config: {
fields: [
{ name: 'id', type: 'int' },
{ name: 'name', type: 'string' }
],
idProperty: 'id',
identifier : 'uuid'
}
});
I need 'name' field to be unique. Thank you very much!

Assuming that you are manually popping records into a store, you can put a listener on your store, and when a record is added, unset it.
http://docs.sencha.com/touch/2-0/#!/api/Ext.data.Store-event-addrecords

Related

Using Ext.crete in Ext.define

Is it safe to use an Ext.create call in a Ext.define call ?
Example:
Ext.define('app.store.MyStore',{
extend: 'Ext.data.Store',
model: Ext.create('Ext.data.Model', {
idProperty: 'Id',
fields: [
{ name: 'Id', type: 'number' },
{ name: 'Name', type: 'number' }
]
}),
...
No, it is not. In fact with your code the model definition does not even get to the store. Try creating an instance of your store and check what fields the model of the store has:
const myStore = Ext.create('app.store.MyStore');
console.log(myStore.getModel().getFields());
The result will be an empty array. Now define your model and store like this:
Ext.define('app.store.MyModel', {
extend: 'Ext.data.Model',
idProperty: 'Id',
fields: [{
name: 'Id',
type: 'number'
}, {
name: 'Name',
type: 'number'
}]
});
Ext.define('app.store.MyStore', {
extend: 'Ext.data.Store',
model: 'app.store.MyModel'
});
Try the again to create a store and check for the model fields like above, now you will get an array with two elements, as expected.
Even if it were possible I would not recommend it. Chances are high that you will need this model definition somewhere else in you application.

Trying to use JSONStore in a combo

In our application we have a lot of name/value stores, and they are created at load time and put into a JSONStore like so :
Ext.create("Ext.data.JsonStore", {
data: data,
model: 'EM.model.controlUnit.CodeList',
storeId: "cl_" + tableId,
sorters: [{
sorterFn: aSorterFunction
}],
});
The model is pretty simple and looks like this :
Ext.define('EM.model.controlUnit.CodeList', {
extend: 'Ext.data.Model',
fields: [{
name: 'value', type: 'int'
}, {
name: 'label', type: 'string'
}, {
name: 'description', type: 'string'
}]
});
I thought stores were pretty interchangeable so I decided to use the store in the combo (There is no special combo store so I thought a JSONStore must be as good as a SimpleStore). I get the store like so :
var msDataStore = Ext.getStore("cl_t_cl_maritalstatus");
And use the store like so :
{
xtype: 'combo',
fieldLabel: 'Marital Status',
displayField: "label",
valueField: "value",
store: msDataStore
}
The combo is filled with the values from the store when I run the application, however, when I pop down the combo box, this error is thrown :
ext-debug-w-comments.js:9951 Uncaught
Ext.data.proxy.Server.buildUrl(): You are using a ServerProxy but have
not supplied it with a url.
I do not want any server proxy. These are simple locally stored name value collections.
Can JSONStores be used with combos?
If not. What is the best way to convert a JSONStore into something acceptable for the combo. I can chop, change, restructure the store object. But I just want to know if there is something simpler that I can do before going on some kind of long and pointless journey.
This problem is related 'proxy' property. Default proxy for JsonStore is 'ajax';
proxy: {
type : 'ajax',
reader: 'json',
writer: 'json'
}
You should override with 'memory' like that;
proxy: {
type: 'memory'
}
Your final store is;
Ext.create("Ext.data.JsonStore", {
data: data,
model: 'EM.model.controlUnit.CodeList',
storeId: "cl_" + tableId,
proxy: {
type: 'memory'
}
sorters: [{
sorterFn: aSorterFunction
}],
});
A JsonStore without URL is completely acceptable, but you have to make sure the combo does not trigger a load operation when clicking on the dropdown. This is done by adding to the combo definition the config option queryMode:'local':
{
xtype: 'combo',
fieldLabel: 'Marital Status',
displayField: "label",
valueField: "value",
queryMode: 'local',
store: msDataStore
}

How can I use WebSQL in Sencha Touch with Unique Id

this is my model:
Ext.define('myApp.model.Category', {
extend: 'Ext.data.Model',
requires: [
'Ext.data.Field'
],
config: {
fields: [
{
name: 'id',
type: 'int'
},
{
name: 'slug',
type:'string'
},
{
name: 'title',
type:'string'
},
{
name: 'post_count',
type:'int'
}
],
}});
and this is my store:
Ext.define('myApp.store.LocalCate',{
extend: 'Ext.data.Store',
requires: [
'Ext.data.proxy.Sql'
],
config: {
autoLoad:false,
model:'myApp.model.Category',
storeId:'LocalCate',
proxy:{
type: 'sql',
database: 'myApp',
table: 'Category'
},
}
});
now the problem is that :
when i use store.add({id:123,slug:'123',title:'123',post_count:123});
or store.add(Ext.create('myApp.model.Category'{id:123,slug:'123',title:'123',post_count:123});
they both doesn't work out;
It appears that when i delete the 'id:123' and only add the data
{slug:'123',title:'123',post_count:123}
it works out and when i do store.sync(), the WebSql DataBase show the data without 'id'.
I do know that if i change the id to something else just like 'cate_id',it will work.
However, I want this 'id' as the idproperty, and I don't want to change it's name.
What should i do? How do you use the websql in sencha touch? I find the same problem in LocalStorage.
answer it by myself.
the model will do this when created
// If it does not have an id in the data at this point, we use the one generated by the id strategy.
// This means that we will treat this record as a phantom record from now on
id = me.data[idProperty];
if(!id && id !==0){
me.data[idProperty]= me.internalId = me.id;
me.phantom =true;
and if i created a model with id, it's phantom is false that tell the sencha it has it data in database so that when i sync(),it won't be pushed to the database.
only if i created the model with phantom = true can i sync() it to the database.
here is the code for addrecords:
getNewRecords:function(){
returnthis.data.filterBy(function(item){
// only want phantom records that are valid
return item.phantom ===true&& item.isValid();
}).items;
},

Writing a Custom Reader in ExtJS

I am new to ExtJS and am playing around with it, I am receiving following JSON from the server as shown below
{"tid":1,"action":"HelloWorld","method":"getData",
"result": {"InstId":"1",
"value": [
{"country.stateId":"101", "country.stateName":"TA"},
{"country.stateId":"102", "country.stateName":"ABC"},
{"country.stateId":"103", "country.stateName":"DEF"}]}",
"type":"rpc"}
and I need to extract values form the above JSON and populate the combobox. To do this job I need to write a custom Reader to read and populate the in the combobox. Can anyone of you please help me in writing a custom reader?
Following are the various snippet
Ext.define('AM.store.TestStore', {
extend: 'Ext.data.Store',
alias : 'widget.testStore',
model: 'AM.model.TestModel',
autoload:true,
proxy: {
type: 'direct',
directFn: HelloWorld.getData,
extraParams:[ {'InstId': '1', 'boxid':'id'},
{'InstId': '1', 'name':'states'}
],
reader:{
type:'json',
root:'result'
}
});
Following is the view object
Ext.define('AM.view.combobox.TestView' ,{
extend: 'Ext.form.ComboBox',
alias : 'widget.TestView',
fieldLabel: 'States',
store:'TestStore',
renderTo: Ext.getBody(),
displayField: 'country.stateName',
valueField: 'country.stateId',
autoselect:false
});
Following is my Model Object
Ext.define('AM.model.TestModel', {
extend: 'Ext.data.Model',
alias : 'widget.TestModel',
fields: [ {name: 'country.stateId'}, {name: 'country.stateName'}]
);
Following is my controller
Ext.define('AM.controller.Funds', {
extend: 'Ext.app.Controller',
alias : 'widget.FundsController',
views: ['combobox.TestView'],
stores: ['TestStore'],
models:['TestModel']
);
Can anyone please help me to write a custom JSON Reader?
Thanks
Phani Kumar
You don't need to write custom reader for that. I think making couple small changes in your code would be enough:
First, in the proxy definition:
root: 'value'
useSimpleAccessors: true
then in the model for your data:
fields: [{
name: 'id', mapping: 'country.stateId' }, {
name: 'name', mapping: 'country.stateName'
}]
That should do it.

Sencha touch 2.0 many-to-many associations - how?

I've been having a hell of a time getting Sencha Touch 2.0 hasMany associations working, especially since it looks like their data models don't directly allow for many-to-many relationships. I've got two models - People and Roles (theere are a bunch more, but these are the two that matter in this example) , each has a many-to-many to the other.
I originally thought that I could do this with a hasMany in each of the models, but snce the data is stored in third-normal form in my db, I figure that I need a third, person-to-role model. Code is here:
Ext.define('SMToolkit.model.Person', {
extend: 'Ext.data.Model',
config: {
fields: [
'id',
'first_name',
'last_name',
'email',
'address',
'phone1',
'phone2',
'type',
'location'
],
hasMany: [
{
model: 'SMToolkit.model.Person_Role',
name: 'role'
}
],
proxy: {
type: 'rest',
url : 'index.php/api/persons'
}
}
});
Ext.define('SMToolkit.model.Role', {
extend: 'Ext.data.Model',
config: {
fields: [
'id',
'name',
'description',
'type',
'show_id'
],
hasMany: [
{
model: 'SMToolkit.model.Person_Role',
name: 'person'
},
{
model: 'SMToolkit.model.Scene_Role',
name: 'scene'
},
{
model: 'SMToolkit.model.Thing',
name: 'thing'
}
],
proxy: {
type: 'rest',
url : 'index.php/api/roles'
}
}
});
Ext.define('SMToolkit.model.Person_Role', {
extend: 'Ext.data.Model',
config: {
fields: [
'person_id',
'role_id'
],
associations: [
{
type: 'belongsTo',
model: 'SMToolkit.model.Person',
name: 'person'
},
{
type: 'belongsTo',
model: 'SMToolkit.model.Role',
name: 'role'
},
],
proxy: {
type: 'rest',
url : 'index.php/api/personsroles'
}
}
});
I've confirmed that the personsroles url above does in fact return a valid data set, so I know that there should be something in there...
When I look at a Role record, I can see fields for the associated stores, but even if I know for certain that there's an appropriate record in the Person_Role table in the db, the Persons array in the record is empty.
I'm getting the record like so:
onRoleSelect: function(list, index, node, record) {
var editButton = this.getEditButton();
if (!this.showRole) {
this.showRole = Ext.create('SMToolkit.view.role.Show');
}
person = record.person();
thing = record.thing();
scene = record.scene();
person.load();
thing.load();
scene.load();
// Bind the record onto the view
this.showRole.setRecord(record);
// Push the show show view into the navigation view
this.getRoleContainer().push(this.showRole);
},
What am I doing wrong? Why is there no association data?
Here's an alternative approach for handling complex model relations in Sencha.
I've not yet tested it but I think it will likely handle Many-Many relations as well.
Recursive M-M relations might cause you grief with the linkChildAssociations() function.
http://appointsolutions.com/2012/07/using-model-associations-in-sencha-touch-2-and-ext-js-4/

Resources