Child pages
  • fluid.model.transformWithRules

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Wiki Markup
{include:sneak peek warning}
{note}This page is still a rough draft.{note}
{div:class=api-page}

h1. 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}
fluid.model.transformWithRules(model, rules);
{code}

*File name:* {{ModelTransformation.js}}

h2. Parameters

{span:class=borderless-table}
|*{{model}}*| (Object)  the model to transform |
|*{{rules}}*| (Object) a rules object containing instructions on how to transform the model (see below for more information) |
{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}
{
    "target.path": "value.el.path" || {
        expander: {
            type: "expander.function.path",
             ...
        }
    }
}
{code}

h3. Transformation Expanders

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

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}
var source = {
    cat: "meow",
    ...
}
{code}
{column}
{column}
\\
\\
*>>*{column}
{column}
Rule to rename "cat" to "feline":
{code:javascript}
var rules = {
    feline: { 
        expander: {
            type: "fluid.model.transform.value",
            // specify only a new path
            path: "cat"
        }
    },
    ....
}
{code}
{column}
{column}
\\
\\
*>>*{column}
{column}
Result:
{code:javascript}
{
    feline: "meow",
    ...
}
{code}
{column}
{section}

*To set a default value:*
{section}
{column}
Start:
{code:javascript}
var source = {
    gerbil: undefined,
    // or if "gerbil" doesn't exist
    ...
}
{code}
{column}
{column}
\\
\\
*>>*{column}
{column}
Rule to set default value of "gerbil":
{code:javascript}
var rules = {
    gerbil: { 
        expander: {
            type: "fluid.model.transform.value",
            // specify the path and the default value
            path: "gerbil",
            value: "squeek"
        }
    },
    ....
}
{code}
{column}
{column}
\\
\\
*>>*{column}
{column}
Result:
{code: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}
var source = {
    // no mention of kangaroos
    ...
}
{code}
{column}
{column}
\\
\\
*>>*{column}
{column}
Rule to set default value of "gerbil":
{code:javascript}
var rules = {
    gerbil: { 
        kangaroo: {
            type: "fluid.model.transform.value",
            // specify only a value
            value: "boingg"
        }
    },
    ....
}
{code}
{column}
{column}
\\
\\
*>>*{column}
{column}
Result:
{code:javascript}
{
    kangaroo: "boingg",
    ...
}
{code}
{column}
{section}

*To change the structure/nesting:*
{section}
{column}
Start:
{code:javascript}
var source = {
    goat: false,
    sheep: [
        "baaa",
        "wooooool"
    ],
    ...
}
{code}
{column}
{column}
\\
\\
*>>*{column}
{column}
Rule to set default value of "gerbil":
{code:javascript}
var rules = {
    "farm.goat": {                                          
        expander: {
            type: "fluid.model.transform.value",
            path: "goat"
        }
    },
    "farm.sheep": {
        expander: {
            type: "fluid.model.transform.value",
            path: "sheep"
        }
    } 
    ....
}
{code}
{column}
{column}
\\
\\
*>>*{column}
{column}
Result:
{code:javascript}
{
    farm: {
        goat: false,
        sheep: [
            "baaa",
            "wooooool"
        ]
    },
    ...
}
{code}
{column}
{section}
h4. {{fluid.model.transform.arrayValue}}
h4. {{fluid.model.transform.firstValue}}
h4. {{fluid.model.transform.merge}}


h3. Example
{code:javascript}

{code}
In this example, description here...

{div}