In these situations you may invent your own type. For example:
type NameRec[<JavaScript>]() =
[<DefaultValue>]
[<Name>]
val mutable Name : string
[<DefaultValue>]
[<Name>]
val mutable Type : string
and then create an ArrayStore as:
Ext.data.ArrayStore (
Ext.data.ArrayStoreConfiguration (
Fields = [|
NameRec(Name = "company")
NameRec(Name = "price", Type = "float")
|]
)
)
Having to introduce a lot of these types may be inconvenient however.
There are a few possible ways to mimic the ability to create objects
with arbitrary fields. For example by defining a couple of helpers:
[<JavaScript>]
let (:=) x y : (string * obj) = (x, y)
[<Inline "void($r[$k]=$v)">]
let Set (r: obj) (k: string) (v: obj) : unit = ()
[<JavaScript>]
let (!) (fields: seq<(string * obj)>) =
let r = obj ()
for (k,v) in fields do
Set r k v
r
you can write the example as:
Ext.data.ArrayStore (
Ext.data.ArrayStoreConfiguration (
Fields = [|
NameRec(Name = "company")
NameRec(Name = "price", Type = "float")
|]
)
)
without defining any additional types.
I tested these examples for our ExtJs 4 binding (commnig soon) so not
sure it is compatible with the ExtJs 3 API.
Cheers,
Joel
I think Joel meant:
> Ext.data.ArrayStore (
> Ext.data.ArrayStoreConfiguration (
> Fields = [|
> !["name" := "company"]
> !["name" := "price"; "type" := "float"]
> |]
> )
> )
Trying to provide static types for Ext JS interfaces is a continuing
battle we seem to be losing. They don't seem to make it easy.
Fortunately if you can write something in JS, you can always have it
in WebSharper too, if you sacrifice some type safety.
Thanks,
Anton
--
Kind Regards,
Anton Tayanovskyy
WebSharper™ - type-safe JavaScript in F#
http://intellifactory.com
Correct, thanks for spotting my copy-paste error :)
Joel
type Data =
{
Id : string
Name : string
}
// Extend JsonStore, since missing fields configuration.
type StoreConfig[<JavaScript>]() =
inherit ExtJs.Ext.data.JsonStoreConfiguration()
[<DefaultValue>]
[<Name "fields">]
val mutable Fields : obj []
[<JavaScript>]
let GridSample () =
let store =
Ext.data.JsonStore(
StoreConfig(
Data = [|
{ Id = "1" ; Name = "A" }
{ Id = "2" ; Name = "B"}
{ Id = "3" ; Name = "C"}
{ Id = "4" ; Name = "D"}
{ Id = "5" ; Name = "E"}
|],
Fields = [|
!["name" := "Id"]
!["name" := "Name"; "type" := "string"]
|]
)
)
Ext.grid.GridPanel (
Ext.grid.GridPanelConfiguration (
Store = store,
ColModel =
Ext.grid.ColumnModel (
Ext.grid.ColumnModelConfiguration (
Columns = [|
// Column 1
Ext.grid.ColumnConfiguration (
Header = "Id",
Sortable = true,
DataIndex = "Id"
)
// Column 2
Ext.grid.ColumnConfiguration (
Header = "Id",
Sortable = true,
DataIndex = "Name"
)
|]
)
),
Sm =
Ext.grid.RowSelectionModel (
Ext.grid.RowSelectionModelConfiguration(
SingleSelect = true
)
),
Height = 200.,
Width = 200.
)
)
Cheers,
Joel