1. #1
    Ext User
    Join Date
    Jul 2008
    Posts
    28
    Vote Rating
    0
    d1rty is on a distinguished road

      0  

    Default Record.create() and defaultValue

    Record.create() and defaultValue


    I'm defining a record type for my application, and i'd like to set the default values to be used when no data is passed to the constructor. however it doesn't seem to work as i would expect

    Code:
    foo = Ext.data.Record.create([
        {name:"bar", type:"int", defaultValue:1}
    ]) 
    x = new foo();
    console.log(x);
    this give me a record object with an empty data object. should the constructor not seek out fields with default values and set them in the data object ?

    the docs seems to suggest it should..

    defaultValue : Mixed
    (Optional) The default value passed to the Reader when the field does not exist in the data object (i.e. undefined). (defaults to "")

    Chris
    Attached Images

  2. #2
    Sencha - Ext JS Dev Team Animal's Avatar
    Join Date
    Mar 2007
    Location
    Notts/Redwood City
    Posts
    30,496
    Vote Rating
    44
    Animal has a spectacular aura about Animal has a spectacular aura about Animal has a spectacular aura about

      0  

    Default


    The defaultValue is used by a DataReader. The generated constructor just puts the data object into the data property.

  3. #3
    Ext User
    Join Date
    Jul 2008
    Posts
    28
    Vote Rating
    0
    d1rty is on a distinguished road

      0  

    Default


    ok, so if i wanted it to behave in the way i descibed. is it possible to override the constructor to make it set the default values? I understand about overriding methods (well a bit anyway) but i'm not sure about how (or if its possible in this scenario) to override a constructor

  4. #4
    Sencha - Ext JS Dev Team Animal's Avatar
    Join Date
    Mar 2007
    Location
    Notts/Redwood City
    Posts
    30,496
    Vote Rating
    44
    Animal has a spectacular aura about Animal has a spectacular aura about Animal has a spectacular aura about

      0  

    Default


    You could extend your foo class (Because that's what it is, see the "new" bit)

    But basically, just construct a new record with the values you require.

    Code:
    x = new foo({bar: 1});

  5. #5
    Ext User
    Join Date
    Jul 2008
    Posts
    28
    Vote Rating
    0
    d1rty is on a distinguished road

      0  

    Default


    yup, i realise that i can create an instance of foo with the values i want, but what i'm trying to achieve is not to have to pass in every field to the record whenever i want to create one. I'll expand on my exaple

    Code:
    User = Ext.data.Record.create([
        {name:"name", type:"string", defaultValue:1}
        {name:'datecreated', type:'date', defaultValue:new Date()}
    ])
    
    x = new User({
        name: "Chris"
    });
    i don't want to have to pass datacreate: new Date() to the constructor everytime, but i do want my user object returned with data.datecreated populated. This is a simple example, but in a real app there might be numerous fields that should be initialized like this.

  6. #6
    Sencha - Ext JS Dev Team Animal's Avatar
    Join Date
    Mar 2007
    Location
    Notts/Redwood City
    Posts
    30,496
    Vote Rating
    44
    Animal has a spectacular aura about Animal has a spectacular aura about Animal has a spectacular aura about

      0  

    Default


    OK, you can extend foo then to preprocess any passed in (or not) value before handing it up to the superclass constructor.

  7. #7
    Ext User
    Join Date
    Jul 2008
    Posts
    28
    Vote Rating
    0
    d1rty is on a distinguished road

      0  

    Default


    and that's where my knowledge runs out..
    can you point me in the right direction? (just how to extend foo is all i think i need)

  8. #8
    Ext User
    Join Date
    Jul 2007
    Location
    Florida
    Posts
    9,996
    Vote Rating
    5
    mjlecomte will become famous soon enough mjlecomte will become famous soon enough

      0  

    Default


    take a look at the tutorials section, preconfigured classes. there's a stickied thread in the help section as well for additional discussion.

  9. #9
    Ext User
    Join Date
    Jul 2008
    Posts
    28
    Vote Rating
    0
    d1rty is on a distinguished road

      0  

    Default


    I've read the tutorials (most of them), and i'm using preconfigured classes already in my application. I'm becoming more familiar with Ext.extend for creating them, and thats working well for me. This is how i would do it if i were extending a component, but Ext.data.Record extends Object directly, so theres no initComponent method to override.

    However i'm having a mental block to see how that applies in to this situation.

    I've tried to directly subclass Ext.data.Record, but the following code throws the error "test.Record.create is not a function"

    Code:
    test.Record = function(cfg) {
        Ext.data.Record.call(this, cfg);
    };
    
    test.User = test.Record.create([
        {name:"name", type:"string", defaultValue:1},
        {name:'datecreated', type:'date', defaultValue:new Date()}
    ]);
    I'm hoping a slight nudge in the right direction will help the penny drop.
    thanks, Chris

  10. #10
    Sencha - Ext JS Dev Team Animal's Avatar
    Join Date
    Mar 2007
    Location
    Notts/Redwood City
    Posts
    30,496
    Vote Rating
    44
    Animal has a spectacular aura about Animal has a spectacular aura about Animal has a spectacular aura about

      0  

    Default


    Code:
    Ext.override(foo, {
        constructor: function(data) {
        }
    });
    Do not extend to configure. IMNSHO, it's bad practice. Why not just write a factory method?