Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Problem with Ext.Create

    Thank you for reporting this bug. We will make it our priority to review this report.
  1. #1
    Sencha User
    Join Date
    Nov 2012
    Posts
    118

    Default Problem with Ext.Create

    Code:
    Ext.define('User', {
        extend: 'Ext.data.Model',
        fields: [
            {name: 'name',  type: 'string'},
            {name: 'age',   type: 'int', convert: null}
        
        ]
     
       
    });
    console.log("before user1 defined . It has to print undefined"+user1);
    var user1 = {name : "abc" , age : 30}
    console.log("user1 iniitalized ");
    console.log(user1)
    var user2 = Ext.create("User",user1);
    console.log("User Object created with help of Ext.Create and stored in user2 variable");
    console.log(user2);
    console.log("user1 valus after user object has been created " );
    console.log(user1);
    user1.name = "Frank";
    console.log("User name has been modified , user2 is ");
    console.log(user2);
    console.log("and user1 is changed to  " );
    console.log(user1);

    If u look at the output , you will notice before using Ext.Create() user1 was having properties name and age , but once I used Ext.Create() , id property is being added to user1 object.

    And now Ext JS refers user2 and user1 as same variable pointing to same memory location. It means , I cannot use user1 to assign it to other variable and operate on it.

    Kindly guide.

    Thanks

  2. #2
    Ext JS Premium Member
    Join Date
    Aug 2011
    Location
    Greenville, SC
    Posts
    181

    Default

    Hello,


    I found this Ext bug the other day, but didn't report it. It should really be under the Ext Bugs forum. Probably the scariest part about this issue is the changes are not reflected in the models getChanges function. The issue is when passing in data using Ext.create for a model the original data object is referenced:


    Code:
    var data = {
    	"name": "abc",
    	"age": 30
    };
    
    
    var record = Ext.create('User', data);

    The original object is used instead of cloning the object and setting value. This worked prior to Ext 5.


    I've translated and modified your example a little to show the issues in a fiddle.


    https://fiddle.sencha.com/#fiddle/150g




    As far as I know, this is unique to models. The really strange part about how this code works is if you use the use record.set instead of passing in the data on create, the record does not use the original object. If you modify the fiddle where the record is created with the following code, you will see the record working as expected.


    Code:
    // Values of the data var before create
    console.log("data values - name: " + data.name + ", age: " + data.age);
    record = Ext.create('User');
    record.set(data);

    There are two ways I've tested fixing this:


    1.) Created an override of the Ext.data.Model constructor and do a clone:
    Code:
    // This may or may not impact performance
    constructor: function(data) {
    	var me = this;
    
    
    	data = Ext.clone(data);
    
    
    	me.callParent([data]);
    },

    2.) Do not pass data into the Ext.create. Create the record then set the value.


    Code:
    record = Ext.create('User');
    record.set(data);

    I hope this helps, but I'd also like to see this issue solved. It has been a real headache.


    Bryan

  3. #3
    Sencha User
    Join Date
    Nov 2012
    Posts
    118

    Default

    Thanks for Reply,

    Well , I cant override the constructor. But I have used object literal in following way to avoid that situation

    var tempObj = {}
    var objLiteral = { name : "abc"}
    var user = Ext.create("UserModel",Ext.apply(tempObj,objLiteral));

    In this case objLiteral won't get changed thought I need to create tempObj variable. But I can pass ObjLiteral anywhere without any worry.

  4. #4
    Ext JS Premium Member
    Join Date
    Aug 2011
    Location
    Greenville, SC
    Posts
    181

    Default

    This won't work for you?

    Code:
    Ext.define('OVERRIDE.data.Model',{
    	override: 'Ext.data.Model',
    	constructor: function(data) {
    		var me = this;
    
    
    		data = Ext.clone(data);
    
    
    		me.callParent([data]);
    	}
    });

  5. #5
    Ext JS Premium Member
    Join Date
    Aug 2011
    Location
    Greenville, SC
    Posts
    181

    Default

    I have file this issue with Sencha Support.

  6. #6
    Ext JS Premium Member
    Join Date
    Aug 2011
    Location
    Greenville, SC
    Posts
    181

    Default

    Here is the bug listed for this issue: EXTJS-20328

  7. #7
    Sencha User
    Join Date
    Nov 2012
    Posts
    118

    Default

    Hey , Can you provide link of this bug listed ?

  8. #8
    Ext JS Premium Member
    Join Date
    Aug 2011
    Location
    Greenville, SC
    Posts
    181

    Default

    Unfortunately, I can't. Sencha does not expose their ticketing system. All I can provide is a bug number which they gave me.

  9. #9
    Sencha User joel.watson's Avatar
    Join Date
    Nov 2014
    Posts
    3,120

    Default

    I've moved this thread to the bugs forum and have linked the issue above.

    Thanks
    Joel

  10. #10
    Ext JS Premium Member
    Join Date
    Aug 2011
    Location
    Greenville, SC
    Posts
    181

    Default

    Thank you Joel...

Page 1 of 2 12 LastLast

Similar Threads

  1. Create TreeGrid Problem
    By marvic in forum Sencha GXT Q&A
    Replies: 2
    Last Post: 16 Jun 2013, 9:22 AM
  2. Migration from st 1.x to st 2 problem (Ext.create)
    By Piggygoose in forum Sencha Touch 2.x: Q&A
    Replies: 3
    Last Post: 11 May 2012, 5:43 AM
  3. [NOREPRO] Problem with Ext.create() and arrays
    By aflx in forum Sencha Touch 2.x: Bugs
    Replies: 1
    Last Post: 13 Nov 2011, 6:45 AM
  4. Problem with Ext.ClassManager.create
    By MrBoolean in forum Ext: Discussion
    Replies: 1
    Last Post: 1 Apr 2011, 11:01 AM
  5. Problem with record.create
    By tubamanu in forum Sencha Touch 1.x: Discussion
    Replies: 0
    Last Post: 18 Dec 2010, 4:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •