Alexander
Alexander

Reputation: 20234

Sencha Touch: clone record

I want to clone a Sencha Touch 2.3 Model instance (also called "record"). The clone should have the same id as the old one, but it should not be attached to any store. In ExtJS4, I would do the following and it would work:

var newRecord = record.copy();

This does not work in Sencha Touch. As per ST2 documentation, the new record gets a newly generated id on copy(). So I tried:

var newRecord = record.copy(record.getId());

So now I should have a new copy with the same id.

newRecord.getId()==record.getId(); // returns true

I then modify the record:

newRecord.set("myCount",newRecord.get("myCount")+1);

Guess what happens next? I check that the records are really different:

newRecord.get("myCount")!=record.get("myCount"); // returns false

So, when I modified the "copy", I also modified the original, which means that no copy was created.

What am I doing wrong, where did I misread the Sencha Touch docs?

Upvotes: 0

Views: 1676

Answers (1)

kumar
kumar

Reputation: 1854

record.copy(id) will return a reference to the existing record if you pass the same id as record.

var newRecord = record.copy() will create another instance of the record so it will have a different id but its not attached to the current store. You can verify that as shown :

record.stores  //Returns store reference in array
newRecord.stores //Returns [] indicating no store refeerences

You can copy the record and set the id of newRecord to id of original record if you want like this.

var newRecord = record.copy()
newRecord .set('id',record.getId()) //newRecord.getId() returns same as record.getId()

Upvotes: 2

Related Questions