1. #51
    Sencha User
    Join Date
    Apr 2010
    Posts
    85
    Vote Rating
    12
    Answers
    3
    HriBB will become famous soon enough

      0  

    Default

    I've had a similar problem with parent who hasMany children. I solved it by using grid cellediting plugin, which works great for editing parent data, and for associated children I created a custom editor which extends Ext.form.field.Picker and handles get/set manually. And I also use a custom template to render children in grid. In your case you could extend Ext.form.field.Combobox which also extends Ext.form.field.Picker and override get/set functions. But for that you'll need to get your hands dirty and dig into the source code. Hint:

    Code:
    Ext.util.Observable.capture(observable, function() {
        console.log('capture', arguments);
    });
    Anyway, you'll have to handle this manually. But it would be cool if you could handle association editing through config.

  2. #52
    Sencha User
    Join Date
    Jul 2012
    Posts
    25
    Vote Rating
    3
    Answers
    1
    delebash is on a distinguished road

      0  

    Default

    Thanks, for the hint, I guess I will go digging, yeah so much fun....

  3. #53
    Sencha User
    Join Date
    Jul 2010
    Posts
    55
    Vote Rating
    -1
    Answers
    1
    hdave is an unknown quantity at this point

      0  

    Default

    Anyone here have a working version of this writer for ExtJS 5?

  4. #54
    Sencha Premium Member
    Join Date
    Jun 2012
    Posts
    34
    Vote Rating
    2
    Answers
    1
    bartyhoho is on a distinguished road

      0  

    Default

    Quote Originally Posted by hdave View Post
    Anyone here have a working version of this writer for ExtJS 5?
    This is probably too late for you, but for anyone else needing help and finding this post in the future, here's what I used for both Ext 5.1.0 and 6.0.1:

    Code:
    /**
     * Override for the JsonWriter so that associations are sent to the server along
     * with "main" model properties.
     *
     * Thanks to: http://moduscreate.com/writing-ext-js-overrides/
     * https://www.sencha.com/forum/showthread.php?148250-Saving-Model-with-Nested-Data -
     * swelbow's comment at the bottom
     *
     *
     * @class Overrides.Json
     * @overrides Ext.data.writer.Json
     */
    Ext.define('Overrides.JsonWriter', {
        override: 'Ext.data.writer.Json',
        
        getRecordData: function(record, operation) {
            console.log("using JsonWriter override!!!!!!");
            record.data = this.callParent(arguments);
            Ext.apply(record.data, record.getAssociatedData());
            
            /*
             * The associated data is ignored by the writer's dateFormat
             * config. So do that manually.
             */
            this.applyDateFormat(record.data);
            
            return record.data;
        },
        
        /**
         * Custom function to loop over data, see if something is a date, and if it is a date
         * apply the date format to it.
         * 
         * @param object        JS Object to inspect and possibly format
         * @return object        JS Object, either untouched or a formatted date
         */
        applyDateFormat: function(object) {
            for (var property in object) {
                if (object.hasOwnProperty(property) && Array.isArray(object[property])) {
                    
                    //we have an array. do this again
                    this.applyDateFormat(object[property]);
                    
                } else if (object.hasOwnProperty(property) &&
                        object[property] != null &&
                        typeof object[property] == 'object' &&
                        !(object[property] instanceof Date)) {
                    
                    //we have a non-Date object. do this again so we can check the fields
                    this.applyDateFormat(object[property]);
                    
                } else if (object.hasOwnProperty(property) && (object[property] instanceof Date)) {
                    
                    //we have a Date object. format it
                    var dateFormat = (this._dateFormat != null) ? this._dateFormat : 'time';
                    object[property] = Ext.Date.format(object[property], dateFormat);
                    
                }
            }
            
            return object;
         }
    });

  5. #55
    Sencha Premium Member
    Join Date
    May 2010
    Location
    Guatemala, Central America
    Posts
    1,469
    Vote Rating
    302
    Answers
    12
    ssamayoa is on a distinguished road

      0  

    Default

    Thanks! Will try later.
    UI: Sencha Architect 3.x / ExtJS 4 & 5
    Server side: JEE / EJB 3.x / CDI / JPA 2.x/ JAX-RS / JasperReports
    Application Server: Glassfish / WildFly
    Databases: Oracle / DB2 / MySQL / Firebird

    If you like my answer please vote!

  6. #56
    Sencha User
    Join Date
    Jan 2016
    Posts
    1
    Vote Rating
    0
    friendly.marmot is on a distinguished road

      0  

    Default

    In ExtJS 5.1.1:

    Ext.define('PCVM.VirtualMachineSummary', {
    extend: 'Ext.data.Model',
    fields: [
    { name: 'idfield', type: 'int' },
    { name: 'someflag', type: 'boolean' }
    ],
    idProperty: 'idfield',
    requires: [ 'AnotherModel ],
    hasMany: [
    { name: 'anotherModel', model: 'AnotherModel', autoLoad: true }
    ],
    proxy: {
    type: 'ajax',
    url : '/path/to/post/hdlr',
    reader: {
    type: 'json'
    },
    writer: {
    type: 'json',
    allDataOptions: {
    associated: true,
    persist: true
    }
    }
    }

    var model = Ext.create('ModelName');
    model.flush = function() {
    //force a flush of the entire model, rather than changes-only
    this.phantom = true;
    this.save();
    };
    model.on_load_success = function(record, operation) {
    console.log("model succeded: '" + record + "'");
    };
    model.on_load_failure = function(record, operation) {
    console.log("model failed: '" + record + "'");
    };

    The hasMany-"name" should match the name of the json element containing the associated data.

    this.phantom=true will force the entire model to be saved, rather than only changes.
    writer:allDataOptions:associated:true will force associated data to be saved as well.