User Data

UserData in MightyEditor is a way to store specific data of an object. It works similarly to HTML5 localstorage, where you assign key->value pairs to an object.

html5_tutorial_mightyeditor_userdata

Accessing and changing the values is done by using the object.getData().userData.key method:

"use strict";
window.UserData.state.menu = {
    create: function() {

        this.text = mt.create("Text");
        this.count = mt.create("count");

        this.addClick = mt.create("add");
        this.addClick.inputEnabled = true;

        this.result = mt.create("result");
        this.result.inputEnabled = true;

    },

    update: function() {
        //calls functions on mouseclick
        this.addClick.events.onInputDown.add(this.addCount, this);
        this.result.events.onInputDown.add(this.showResult, this);

    },

    //adds 1 to the value for 'click' key
    addCount: function() {
        this.count.getData().userData.click++;
    },
    //gets the stored data and resets it
    showResult: function() {
        var newCount = this.count.getData().userData.click;
        this.count.setText(newCount);
        this.count.getData().userData.click = 0;
    }
};

Full sample available here.

Leave a Reply

Your email address will not be published. Required fields are marked *