0

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?!

John Conde
  • 217,595
  • 99
  • 455
  • 496
hsnGunda
  • 347
  • 1
  • 5
  • 20

2 Answers2

0

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.

Reimius
  • 5,694
  • 5
  • 24
  • 42
  • thanks 4 ur response. Is there any way to set perticular field as primary Key in grid store. What i exactly want is if the same record added twice the 1st record should get replaced. and 1 more question , u said there is no p_key in model, then wats the use of assosciation – hsnGunda Aug 13 '12 at 14:57
  • You seem to have a database mindset, and Javascript is not a database and it's purpose is not the same as a database's. The use of association in Javascript is not for relation of data to be stored and constrained for long periods of time, but instead the mindset is how to best relate and manipulate the way data is displayed to your users. So think of it this way, databases are for storing data, and javascript is tool for your window to see that data. – Reimius Aug 13 '12 at 15:04
  • Saying this: "Is there any way to set perticular field as primary Key in grid store" just does not make sense looking at it from a javascript perspective, so the answer is no. You have to manually define your interpretation of what a primary key is with your own javascript code since Extjs does not provide a 'primary key' functionality, and even if they did, it may not be the same interpretation of how you have set up your database to handle primary key scenarios. – Reimius Aug 13 '12 at 15:06
  • thanks again!! do u have any example to manually define a primary key in javascript?? – hsnGunda Aug 14 '12 at 04:26
  • You can't define a "primary key." My code above will make your store act like a field is a primary key, that is the best example I can provide for you – Reimius Aug 14 '12 at 15:29
  • The unique id generation system in ExtJS creates a unique id on the 'client-side'. This has no bearing on server-side, however it does create a unique id. so it's not a matter of 'if' you can force a unique id on the 'client', rather a matter of whether that unique id is also echo'd on the server. but that's a whole different answer. – Dawesi Dec 03 '13 at 14:58
  • I like how 'unique id' and 'primary key' are the same thing for you... here's a SO question with an answer really basic explanation of the difference: http://stackoverflow.com/questions/3844899/difference-between-key-primary-key-unique-key-and-index-in-mysql – Reimius Dec 03 '13 at 16:54
-3

what about idProperty??

if you set an idProperty on your model it acts as a primary key.

Dawesi
  • 568
  • 3
  • 9