简体   繁体   中英

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?

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM