Child pages
  • fluid.model.transformWithRules

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

Include Page
sneak peek warning
sneak peek warning

Note

This page is still a rough draft.

{div:class=} h1.
Wiki Markup
Div
classapi-page

fluid.model.transformWithRules(model,

rules)

{section} {column:width=70%} Transforms a model based on a specified expansion rules objects. {code:javascript|borderStyle=none|bgColor=white}

Section
Column
width70%

Transforms a model based on a specified expansion rules objects.

Code Block
javascript
javascript
bgColorwhite
borderStylenone
fluid.model.transformWithRules(model, rules);
{code} *File

File name:

* {{

ModelTransformations.js

}} h2. Parameters {span:class=

Parameters

Span
classborderless-table

} |*{{model}}*|

model

(Object)

the

model

to

transform | |*{{rules}}*|

transform

rules

(Object)

a

rules

object

containing

instructions

on

how

to

transform

the

model

(see

below

for

more

information)

| |*{{options}}*|

options

(Object)

a

set

of

rules

governing

the

transformations.

At

present

this

may

contain

the

values:

{{


isomorphic:

true

}}

indicating

that

the

output

model

is

to

be

governed

by

the

same

schema

found

in

the

input

model,

or

{{flatSchema}} holding a flat schema object which consists of a hash of EL path specifications with wildcards, to the values


flatSchema holding a flat schema object which consists of a hash of EL path specifications with wildcards, to the values "array"/"object"

defining

the

schema

to

be

used

to

construct

missing

trunk

values.

| {span} h2. Return Value {span:class=borderless-table} |*Object*| the transformed model | {span} {column} {column:width=5%} {column} {column} h3. See Also * [Model Transformation] {column} {section} ---- h3. Rules Rules objects take the form of: {code:javascript}

Return Value

Span
classborderless-table

Object

the transformed model

Column
width5%
 
Column

See Also


Rules

Rules objects take the form of:

Code Block
javascript
javascript
{
    "target.path": "value.el.path" 
}
{code} or {code:javascript}

or

Code Block
javascript
javascript
{
    "target.path": {
        
expander
transform: {
            type: "
expander
transform.function.path",
             ...
        }
    }
}
{code} h3. Transformation Expanders The Framework currently provides the following expanders that can be used as part of a model transformation. * {{fluid.model.transform.value}} * {{fluid.model.transform.arrayValue}} * {{fluid.model.transform.firstValue}} * {{fluid.model.transform.merge}} Each of these is described below. h4. {{fluid.model.transform.value}} This extracts and/or the value of a given path, and can be used for the following purposes: *To rename a property:* {section} {column} Start: {code:javascript}

Transformation Expanders

The Framework currently provides the following expanders that can be used as part of a model transformation.

  • fluid.transforms.value
  • fluid.transforms.arrayValue
  • fluid.transforms.firstValue
  • fluid.transforms.merge

Each of these is described below.

fluid.transforms.value

This extracts and/or the value of a given path, and can be used for the following purposes:

To rename a property:

Section
Column

Start:

Code Block
javascript
javascript
var source = {
    cat: "meow",
    ...
}
{code} {column} {column} \\ \\ *>>*{column} {column} Rule to rename
Column



>>

Column

Rule to rename "cat"

to

"feline":

{code:javascript}

Code Block
javascript
javascript
var rules = {
    feline: { 
        
expander
transform: {
            type: "fluid.
model
transforms.
transform.
value",
            // specify only the path to transform
            inputPath: "cat"
        }
    },
    ....
}
{code} {column} {column} \\ \\ *>>*{column} {column} Result: {code:javascript}
Column



>>

Column

Result:

Code Block
javascript
javascript
{
    feline: "meow",
    ...
}
{code} {column} {section} *To set a default value:* {section} {column} Start: {code:javascript}

To set a default value:

Section
Column

Start:

Code Block
javascript
javascript
var source = {
    gerbil: undefined,
    // or if "gerbil" doesn't exist
    ...
}
{code} {column} {column} \\ \\ *>>*{column} {column} Rule to set default value of
Column



>>

Column

Rule to set default value of "gerbil":

{code:javascript}

Code Block
javascript
javascript
var rules = {
    gerbil: { 
        
expander
transform: {
            type: "fluid.
model.transform
transforms.value",
            // specify path and default value
            inputPath "gerbil",
            value: "squeek"
        }
    },
    ....
}
{code} {column} {column} \\ \\ *>>*{column} {column} Result: {code:javascript}
Column



>>

Column

Result:

Code Block
javascript
javascript
{
    gerbil: "squeek",
    ...
}
{code}

Note

that

if

"gerbil"

has

a

value

initially,

it

will

be

unaffected.

{column} {section} *To specify a literal value:* {section} {column} Start: {code:javascript}

To specify a literal value:

Section
Column

Start:

Code Block
javascript
javascript
var source = {
    // no mention of kangaroos
    ...
}
{code} {column} {column} \\ \\ *>>*{column} {column} Rule to set a value for
Column



>>

Column

Rule to set a value for "kangaroo":

{code:javascript}

Code Block
javascript
javascript
var rules = {
    kangaroo: { 
        
expander
transform: {
            type: "fluid.
model.transform
transforms.value",
            // specify only a value
            value: "boingg"
        }
    },
    ....
}
{code} {column} {column} \\ \\ *>>*{column} {column} Result: {code:javascript}
Column



>>

Column

Result:

Code Block
javascript
javascript
{
    kangaroo: "boingg",
    ...
}
{code} {column} {section} *To change the

To change the structure/nesting:

* {section} {column} Start: {code:javascript}

Section
Column

Start:

Code Block
javascript
javascript
var source = {
    goat: false,
    sheep: [
        "baaa",
        "wooooool"
    ],
    ...
}
{code} {column} {column} \\ \\ *>>*{column} {column} Rule to change the nesting: {code:javascript}
Column



>>

Column

Rule to change the nesting:

Code Block
javascript
javascript
var rules = {
    "farm.goat": {                                          
        
expander
transform: {
            type: "fluid.
model.transform
transforms.value",
            inputPath "goat"
        }
    },
    "farm.sheep": {
        
expander
transform: {
            type: "fluid.
model
transforms.
transform.
value",
            inputPath "sheep"
        }
    } 
    ....
}
{code} {column} {column} \\ \\ *>>*{column} {column} Result: {code:javascript}
Column



>>

Column

Result:

Code Block
javascript
javascript
{
    farm: {
        goat: false,
        sheep: [
            "baaa",
            "wooooool"
        ]
    },
    ...
}
{code} {column} {section} h4. {{fluid.model.transform.arrayValue}} The {{arrayValue}} expander will transform a value into an aray. If the value is already an array, the expander will have no effect. For example: {section} {column} Start: {code:javascript}

fluid.transforms.arrayValue

The arrayValue expander will transform a value into an aray. If the value is already an array, the expander will have no effect.

For example:

Section
Column

Start:

Code Block
javascript
javascript
var source = {
    cat: "meow",
    sheep: [
        "baaa",
        "wooooool"
    ],
    ...
}
{code} {column} {column} \\ \\ *>>*{column} {column} Rule to convert to arrays: {code:javascript}
Column



>>

Column

Rule to convert to arrays:

Code Block
javascript
javascript
var rules = {
    cat: {                                          
        
expander
transform: {
            type: "fluid.
model.transform
transforms.arrayValue",
            inputPath "cat"
        }
    },
    sheep: {
        
expander
transform: {
            type: "fluid.
model
transforms.
transform.
arrayValue",
            inputPath "sheep"
        }
    } 
    ....
}
{code} {column} {column} \\ \\ *>>*{column} {column} Result: {code:javascript}
Column



>>

Column

Result:

Code Block
javascript
javascript
{
    cat: ["meow"],
    sheep: [
        "baaa",
        "wooooool"
    ],
    ...
}
{code}

Note

that

the

value

of

{{

cat

}}

is

now

an

array,

but

the

value

of

{{

sheep

}}

is

unaffected.

{column} {section} h4. {{fluid.model.transform.firstValue}} h4. {{fluid.model.transform.merge}} h3. Example {code:javascript} {code} In this example, description here... {div}

fluid.transforms.firstValue

fluid.transforms.merge

Example

Code Block
javascript
javascript
 

In this example, description here...