Search code examples
javascriptextjsextjs4rally

on commenting one function code works, otherwise it fails


In my application when I am trying to copy one object its getting copied but if I am defining another function call, the code which was executing properly gives error, don't know why. Any help on this is highly appreciated, and thanks in advance

If I comment this function that._createCustomStore(data); everything works good, if I uncomment this it gives me error as Uncaught ReferenceError: _newParent is not defined on this line copiedParent = _newParent;

below is my code

            _genericInnerCopy: function(_childObj) {
                copiedParent = {};
                that = this;
                model = that.model;
                var record = Ext.create(model, {
                    Name: _childObj.get('Name'),
                    //Parent: _newParent.get("_ref");,
                });
                record.save({
                    callback: function(result, operation) {
                        if(operation.wasSuccessful()) {
                            console.log("Done");
                            //that._copyChild();
                        } else {
                            console.log("error");
                        }
                    }
                })
                that._all_pis.push(record);
                copiedParent = _newParent;
                var store = Ext.create('Rally.data.custom.Store', {
                    data: that._all_pis,
                    listeners: {
                        load: function(store,data,success) {
                            that._updateAll(store, data, copiedParent);
                        },                      
                        scope: that
                    },     
                });
                //console.log("record values", that._all_pis);
            },  
            _updateAll: function(store,data, copiedParent) {
                that = this;
                Rally.data.BulkRecordUpdater.updateRecords({
                    records: data,
                    propertiesToUpdate: {
                        Parent: copiedParent.get("_ref")
                    },
                    success: function(readOnlyRecords){
                        //all updates finished, except for given read only records
                    },
                    scope: that
                });
                that._createCustomStore(data);
            },
            _createCustomStore: function(data) {
                me = this;
                Ext.create('Rally.data.custom.Store', {
                    data: data,
                    //model: 'PortfolioItem/' + _newParent.get('PortfolioItemTypeName'),
                    autoSync:true,
                    listeners: {
                        load: function(store,data,success) {
                            console.log("store value", store);
                            console.log("data value", data);
                            console.log("success value", success);
                        },
                        scope: me                   
                    },
                });             
            },
            onqModelRetrieved: function() {
                var that = this;
                that._type = 'PortfolioItem/' + that._type,
                Rally.data.ModelFactory.getModel({
                    type: that._type,
                    success: this.onModelRetrieved,
                    scope: this
                });     
            },              
            onModelRetrieved: function(model) {
                this.model = model;
                this.createFeature();
            },
            createFeature: function() {
                var record = Ext.create(this.model, {
                    Name: "(Copy of) " + this._newObj.get('Name'),
                });
                record.save({
                    callback: function(result, operation) {
                        if(operation.wasSuccessful()) {
                            _newParent = result
                            Ext.Msg.alert('created ' + result.get('PortfolioItemTypeName') + ':', result.get('Name'));
                        }
                        else{
                            console.log("error");
                        }
                    }
                });
            }       
        });

Solution

  • Try define it and use it like this:

    _newParent: null,
    _genericInnerCopy: function(_childObj) {
        copiedParent = {};
        that = this;
        model = that.model;
        var record = Ext.create(model, {
            Name: _childObj.get('Name')
            //Parent: _newParent.get("_ref");,
        });
        record.save({
            callback: function(result, operation) {
                if(operation.wasSuccessful()) {
                    console.log("Done");
                    //that._copyChild();
                } else {
                    console.log("error");
                }
            }
        })
        that._all_pis.push(record);
        copiedParent = that._newParent;
        var store = Ext.create('Rally.data.custom.Store', {
            data: that._all_pis,
            listeners: {
                load: function(store,data,success) {
                    that._updateAll(store, data, copiedParent);
                },                      
                scope: that
            }
        });
        //console.log("record values", that._all_pis);
    },  
    _updateAll: function(store,data, copiedParent) {
        that = this;
        Rally.data.BulkRecordUpdater.updateRecords({
            records: data,
            propertiesToUpdate: {
                Parent: copiedParent.get("_ref")
            },
            success: function(readOnlyRecords){
                //all updates finished, except for given read only records
            },
            scope: that
        });
        that._createCustomStore(data);
    },
    _createCustomStore: function(data) {
        me = this;
        Ext.create('Rally.data.custom.Store', {
            data: data,
            //model: 'PortfolioItem/' + _newParent.get('PortfolioItemTypeName'),
            autoSync:true,
            listeners: {
                load: function(store,data,success) {
                    console.log("store value", store);
                    console.log("data value", data);
                    console.log("success value", success);
                },
                scope: me                   
            }
        });             
    },
    onqModelRetrieved: function() {
        var that = this;
        that._type = 'PortfolioItem/' + that._type,
        Rally.data.ModelFactory.getModel({
            type: that._type,
            success: this.onModelRetrieved,
            scope: this
        });     
    },              
    onModelRetrieved: function(model) {
        this.model = model;
        this.createFeature();
    },
    createFeature: function () {
        var that = this;
        var record = Ext.create(this.model, {
            Name: "(Copy of) " + this._newObj.get('Name')
        });
        record.save({
            callback: function (result, operation) {
                if (operation.wasSuccessful()) {
                    that._newParent = result
                    Ext.Msg.alert('created ' + result.get('PortfolioItemTypeName') + ':', result.get('Name'));
                }
                else {
                    console.log("error");
                }
            }
        });
    }