1. #1
    Sencha User
    Join Date
    Sep 2010
    Posts
    64
    Vote Rating
    3
    plleeuwm is on a distinguished road

      2  

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

  2. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    St. Louis, MO
    Posts
    34,107
    Vote Rating
    453
    mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of

      0  

    Default


    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.

  3. #3
    Sencha User
    Join Date
    Sep 2010
    Posts
    64
    Vote Rating
    3
    plleeuwm is on a distinguished road

      0  

    Default


    Quote Originally Posted by mitchellsimoens View Post
    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');
    
        }
    });
    But this logic already exists in store why would we want to duplicate it?

    Code:
    model.set('foo', 'mitch');
    model.set('foo', 'mitch');
    Would fire datachanged twice. The stores update event logic already handles this.

    ***
    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.

  4. #4
    Sencha User
    Join Date
    Sep 2010
    Posts
    64
    Vote Rating
    3
    plleeuwm is on a distinguished road

      0  

    Default


    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.

    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']);
            }
        }
    });
    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.

    But I can do something like :

    Code:
    Ext.define('RecordForm', 
        loadRecord: function(rec) {
        var ret = this.callParent(arguments);
        rec.relayStoreEvents();
        return ret;
        }    
    }
    In another part of the code I can publish the update events on the rec through the RecordForm.

  5. #5
    Sencha - Support Team slemmon's Avatar
    Join Date
    Mar 2009
    Location
    Boise, ID
    Posts
    2,917
    Vote Rating
    80
    slemmon is a glorious beacon of light slemmon is a glorious beacon of light slemmon is a glorious beacon of light slemmon is a glorious beacon of light slemmon is a glorious beacon of light

      0  

    Default


    +1