0

I have a list that is bound to a store. I have a input text field (think chat like UX), and when the user clicks on a button, I do:

var newMessageData = {
    text: textMessage
};
var message = Ext.create('MyApp.model.Message', newMessageData);
messagesStore.add(message); // At this point, the message shows up in my list
message.save(); // On a successful save, an identical message shows up again in this same list

How can I implement this, so the message only shows up once at first (immediately after the user types in something) and then the record itself just syncs with the server in the background.

Help is much appreciated! Thanks!

Edit: My model definition is pretty simple with simple fields and a few converted fields + a custom idProperty:

idProperty: 'objectId',
{
    name: 'objectId',
    persist: false
}
John Doe
  • 1,005
  • 2
  • 8
  • 23

2 Answers2

0

You should be able to use messagesStore.sync() which:

Synchronizes the Store with its Proxy. This asks the Proxy to batch together any new, updated and deleted records in the store, updating the Store's internal representation of the records as each operation completes.

http://docs.sencha.com/touch/2.4.0/#!/api/Ext.data.Store-method-sync

pherris
  • 17,195
  • 8
  • 42
  • 58
  • Yeah, I tried that as well. When I do messagesStore.add(), the list updates with the added record. And, when I do sync(), it adds another one. Thoughts? – John Doe Aug 27 '14 at 04:00
  • If it helps at all, messagesStore is an associated store – so basically, someRecord.messages() – John Doe Aug 27 '14 at 04:04
  • Does the store contain two records after sync() or is the view out of sync? – pherris Aug 27 '14 at 04:28
  • The store actually has 2 records after sync – both records are non-dirty. – John Doe Aug 27 '14 at 04:50
  • Shall we do down the path of why the store's sync method isnt doing what we think it should do, or should we just write some code to fix the problem? – pherris Aug 27 '14 at 13:20
  • If the latter is hack, then I'd like to figure out the root cause of the problem and treat it at its source. Any help is much appreciated though! Thanks! – John Doe Aug 27 '14 at 15:30
  • could you please update your question with the full model definition? specifically, are you configuring an idProperty and is the id's type 'auto'? http://docs.sencha.com/touch/2.4.0/#!/api/Ext.data.Model-cfg-idProperty – pherris Aug 27 '14 at 16:36
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/60123/discussion-between-john-doe-and-pherris). – John Doe Aug 27 '14 at 21:32
0

Okay, so what was missing was that I had to add the keys "primaryKey" to my hasMany and belongsTo relationships. In my case, I had idProperty: 'myCustomId', but that alone didn't work for associated models. I had to add primaryKey: 'myCustomId' to my hasMany and belongsTo relationships.

John Doe
  • 1,005
  • 2
  • 8
  • 23