Section |
---|
Column |
---|
| Transforms a model based on a specified expansion rules objects. Code Block |
---|
| javascript |
---|
| javascript |
---|
bgColor | white |
---|
borderStyle | nonejavascript |
---|
| fluid.model.transformWithRules(model, rules);
|
File name: ModelTransformations.js Span |
---|
| model
| (Object) the model to transform | rules
| (Object) a rules object containing instructions on how to transform the model (see below for more information) | 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 "array"/"object" defining the schema to be used to construct missing trunk values. |
|
Span |
---|
| Object | the transformed model |
|
|
|
Rules objects take the form of: Code Block |
---|
| {
"target.path": "value.el.path"
}
|
or Code Block |
---|
| {
"target.path": {
transform: {
type: "transform.function.path",
...
}
}
}
|
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. 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 |
---|
| var source = {
cat: "meow",
...
}
|
|
Column |
---|
Rule to rename "cat" to "feline": Code Block |
---|
| var rules = {
feline: {
transform: {
type: "fluid.transforms.value",
// specify only the path to transform
inputPath: "cat"
}
},
....
}
|
|
Column |
---|
Result: Code Block |
---|
| {
feline: "meow",
...
}
|
|
|
To set a default value: Section |
---|
Column |
---|
Start: Code Block |
---|
| var source = {
gerbil: undefined,
// or if "gerbil" doesn't exist
...
}
|
|
Column |
---|
Rule to set default value of "gerbil": Code Block |
---|
| var rules = {
gerbil: {
transform: {
type: "fluid.transforms.value",
// specify path and default value
inputPath "gerbil",
value: "squeek"
}
},
....
}
|
|
Column |
---|
Result: Code Block |
---|
| {
gerbil: "squeek",
...
}
|
Note that if "gerbil" has a value initially, it will be unaffected. |
|
To specify a literal value: Section |
---|
Column |
---|
Start: Code Block |
---|
| var source = {
// no mention of kangaroos
...
}
|
|
Column |
---|
Rule to set a value for "kangaroo": Code Block |
---|
| var rules = {
kangaroo: {
transform: {
type: "fluid.transforms.value",
// specify only a value
value: "boingg"
}
},
....
}
|
|
Column |
---|
Result: Code Block |
---|
| {
kangaroo: "boingg",
...
}
|
|
|
To change the structure/nesting: Section |
---|
Column |
---|
Start: Code Block |
---|
| var source = {
goat: false,
sheep: [
"baaa",
"wooooool"
],
...
}
|
|
Column |
---|
Rule to change the nesting: Code Block |
---|
| var rules = {
"farm.goat": {
transform: {
type: "fluid.transforms.value",
inputPath "goat"
}
},
"farm.sheep": {
transform: {
type: "fluid.transforms.value",
inputPath "sheep"
}
}
....
}
|
|
Column |
---|
Result: Code Block |
---|
| {
farm: {
goat: false,
sheep: [
"baaa",
"wooooool"
]
},
...
}
|
|
|
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 |
---|
| var source = {
cat: "meow",
sheep: [
"baaa",
"wooooool"
],
...
}
|
|
Column |
---|
Rule to convert to arrays: Code Block |
---|
| var rules = {
cat: {
transform: {
type: "fluid.transforms.arrayValue",
inputPath "cat"
}
},
sheep: {
transform: {
type: "fluid.transforms.arrayValue",
inputPath "sheep"
}
}
....
}
|
|
Column |
---|
Result: Code Block |
---|
| {
cat: ["meow"],
sheep: [
"baaa",
"wooooool"
],
...
}
|
Note that the value of cat is now an array, but the value of sheep is unaffected. |
|
In this example, description here... |