Custom Data
Edicy.CustomData
allows binding user defined additional data to site, page or article object in edit mode. Bound data is later available in template on the bound object. To access it you need to add edicy-tools.js to your template.
Example:
{% if page.data.lastEdited %}
Last data change was on {{ page.data.lastEdited }}
{% endif %}
{% editorjsblock %}
<script src='{{ site.static_asset_host }}/libs/edicy-tools/latest/edicy-tools.js'></script>
<script>
var pageData = new Edicy.CustomData({
type: 'page',
id: '{{ page.id }}'
});
pageData.set({
"foo": "bar",
"lastEdited": (new Date()).toString()
}, {
success: function(data) {
console.log("Saved data:", data);
}
}
);
</script>
{% endeditorjsblock %}
Initiation parameters
-
type - defines the object that is changed. Value can be either "site", "page", or "article".
-
id - the ID of page or article that the object is bound to. Site does not need an ID.
-
success - (optional) function to call on success of a method. Returns object in arguments.
-
error - (optional) function to call on failing of a method. Returns error data in arguments.
Methods
set
For setting one or multiple keys to objects data. Data can be anything in JavaScript that can have a JSON string representation. Passing null as a value will delete the key.
pageData.set("foo", "bar");
pageData.set({
"foo": ["foo", "bar"],
"foobar": true,
"bar": { obj1: "foobar" },
"delete_it": null
},
{
success: function(data) {}
}
);
reset
Substitutes the whole data parameter object. All keys not passed will be deleted.
pageData.reset({
"foobar": true,
"foo": "bar"
},
{
success: function(data) {}
}
);
get
Returns the whole data object.
// Returns all data
pageData.get({
success: function(data) {},
error: function(data) {}
});
remove
Removes a key.
pageData.remove("bgcolor", {
success: function() {}
});