-
30 Jul 2008 3:52 AM #1
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
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 ?Code:foo = Ext.data.Record.create([ {name:"bar", type:"int", defaultValue:1} ]) x = new foo(); console.log(x);
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
-
30 Jul 2008 4:02 AM #2
The defaultValue is used by a DataReader. The generated constructor just puts the data object into the data property.
Search the forum: http://www.google.com/coop/cse?cx=01...%3Az7of1ufqccu
Read the docs too: http://extjs.com/deploy/dev/docs/
Scope: http://extjs.com/forum/showthread.ph...642#post257642
-
30 Jul 2008 4:43 AM #3
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
-
30 Jul 2008 4:49 AM #4
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});Search the forum: http://www.google.com/coop/cse?cx=01...%3Az7of1ufqccu
Read the docs too: http://extjs.com/deploy/dev/docs/
Scope: http://extjs.com/forum/showthread.ph...642#post257642
-
30 Jul 2008 5:07 AM #5
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
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.Code:User = Ext.data.Record.create([ {name:"name", type:"string", defaultValue:1} {name:'datecreated', type:'date', defaultValue:new Date()} ]) x = new User({ name: "Chris" });
-
30 Jul 2008 5:08 AM #6
OK, you can extend foo then to preprocess any passed in (or not) value before handing it up to the superclass constructor.
Search the forum: http://www.google.com/coop/cse?cx=01...%3Az7of1ufqccu
Read the docs too: http://extjs.com/deploy/dev/docs/
Scope: http://extjs.com/forum/showthread.ph...642#post257642
-
30 Jul 2008 5:40 AM #7
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)
-
30 Jul 2008 5:55 AM #8
take a look at the tutorials section, preconfigured classes. there's a stickied thread in the help section as well for additional discussion.
MJ
API Search || Ext 3: docs-demo-upgrade guide || User Extension Repository
Frequently Asked Questions: FAQs
Tutorial: Grid (php/mysql/json) , Application Design and Structure || Extensions: MetaGrid, MessageWindow
-
30 Jul 2008 7:20 AM #9
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"
I'm hoping a slight nudge in the right direction will help the penny drop.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()} ]);
thanks, Chris
-
30 Jul 2008 12:51 PM #10
Do not extend to configure. IMNSHO, it's bad practice. Why not just write a factory method?Code:Ext.override(foo, { constructor: function(data) { } });Search the forum: http://www.google.com/coop/cse?cx=01...%3Az7of1ufqccu
Read the docs too: http://extjs.com/deploy/dev/docs/
Scope: http://extjs.com/forum/showthread.ph...642#post257642


Reply With Quote