-
4 Mar 2013 1:30 PM #1
Are there ever going to be useful events on Ext.data.Model ?
Are there ever going to be useful events on Ext.data.Model ?
Currently in the 4.2 beta and the lower the Ext.data.Model methods of afterEdit, afterReject, afterCommit just call methods on the actual store object(s) if there is one linked. If you have a model without a store you are SOL, the only event that gets fired from Model is of all things 'idchanged'. Are there any plans to put these events into models?
-
6 Mar 2013 7:56 AM #2Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,714
- Vote Rating
- 438
You could easily add events, for example a datachanged when you hit set (disclaimer: needs to be furthered to handle an object)
Code:Ext.define('Override.data.Model', { override : 'Ext.data.Model', set : function(fieldName, newValue) { var oldValue = this.get(fieldName), ret = this.callParent([fieldName, newValue]); this.fireEvent('datachanged', this, fieldName, newValue, oldValue); return ret; } }); Ext.define('MyModel', { extend : 'Ext.data.Model', fields : ['foo'] }); Ext.application({ name : 'Test', launch : function() { var model = new MyModel({ foo : 'bar' }); model.on('datachanged', function(record, fieldName, newValue, oldValue) { console.log('data changed', fieldName, newValue, oldValue); }); model.set('foo', 'mitch'); } });Mitchell Simoens @SenchaMitch
Sencha Inc, Senior Forum Manager
________________
http://www.JSONPLint.com - Source to lint your JSONP!
Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
https://github.com/mitchellsimoens
Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/
Need more help with your app? Hire Sencha Services services@sencha.com
Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is almost in print!
When posting code, please use BBCode's CODE tags.
-
6 Mar 2013 11:15 AM #3
But this logic already exists in store why would we want to duplicate it?
Would fire datachanged twice. The stores update event logic already handles this.Code:model.set('foo', 'mitch'); model.set('foo', 'mitch');
***
Yes I know we can have overrides that can do anything to the code but I think that this should be handled at the framework level. If you guys have no intention adding the events we can add them ourselves which is fine with us. I'd be worried about if we publish these events in our code developers rely on these events but then in 4.2 you guys publish something very similar. That would start us down the path of a complicated deprecation and refactoring process.
-
6 Mar 2013 11:51 AM #4
Here is my proposed solution to the problem if there there will be no work done to improve the model event system. There probably is some bugs in it but I whipped it in less than 30 mins. It is a complete hack of a bad design imo.
I have designed a record based form with has it's saving, reading and writing and other logic go through the model instead of the Form submit actions. My problem is that there are no useful update events on the model itself without a store.Code:Ext.override(Ext.data.Model, { relayStoreEvents: function(events) { if (!this._eventsStore) { this._eventsStore = new Ext.data.Store({ model: this.self }); this.join(this._eventsStore); this.relayEvents(this._eventsStore, events || ['update']); } } });
But I can do something like :
In another part of the code I can publish the update events on the rec through the RecordForm.Code:Ext.define('RecordForm', loadRecord: function(rec) { var ret = this.callParent(arguments); rec.relayStoreEvents(); return ret; } }
-
6 Mar 2013 2:12 PM #5


Reply With Quote