0

I am developing a javascript menu (using jQuery).

As an example, the below is the structure of the items object I would pass to my custom function which then creates the menu.

var items = {
    0: {
        name: 'file',
        submenu: {
            0: {
                name: 'open',
                hasSubitems: true,
                subitems: {
                    0: {
                        name: 'file',
                        hasSubitems: false,
                        callback: function() { alert('you opened a file!'); }
                    },
                    1: {
                        name: 'project'
                        hasSubitems: false,
                        callback: function() { alert('you opened a project!'); }
                    }
                }
            },
            1: {
                name: 'exit',
                hasSubitems: false,
                callback: function() { alert('you logged out!'); }
            }
        }
    },
    1: {
        name: 'edit',
        submenu: {
           ...
        }
    }
}

I would like to be able to store the above into a database, including their prospective callbacks. I intend to create an interface to build the menus dynamically and such.

I realize I could create a JSON string and store it that way, but then what about the callback functions? I have been recommended against using eval several times, so in what other ways might people safely store this information in a database so it can be called upon later?

6
  • 2
    Even though we don't know what you need that for, but it is still a terribly bad idea. Commented May 22, 2012 at 4:18
  • I thought I explained what it is for. I wanted to store this menu into a database so it could be amended dynamically. Commented May 22, 2012 at 4:19
  • it doesn't explain why you don't want follow the common practice: store data in database, and js in .js files Commented May 22, 2012 at 4:22
  • Use json2.js and using JSON.stringify(<javscrpt array>) store it in db as string. Commented May 22, 2012 at 4:23
  • @PriyankPatel - that won't work with objects that include callback functions, since those are not valid JSON values. Commented May 22, 2012 at 4:28

1 Answer 1

1

JSON is language neutral (despite the name), and as such it only supports primitive values. Callback functions are language specific (javascript in this case), and so they cannot be included in anything that needs to be interpreted as pure JSON.

I recommend pulling your callback code out of here, and instead include necessary parameters that you would need to distinguish each one. Then, run this JSON object through a JS template engine (like Handlebars.js) with helper functions rather than your callbacks. These can be invoked with the parameters you used to replace your callback functions in your (now valid) JSON object.

Sign up to request clarification or add additional context in comments.

5 Comments

So basically, if I wanted the click callback to open a dialog, I would store 'dialog' in a value on the DB somewhere and associate that value with the right type of callback, correct? I had envisioned something like this as a solution, but have been curious about storing the entire object as JSON first.
Yes, you would associate the value with some logic in your helper function. Unfortunately, you can't store the object as JSON, so if JSON is indeed a storage requirement, best bet to start looking at alternatives (such as my suggestion).
No, it doesn't need to be stored in JSON. It just seemed like the best idea at the time. I'll have a look at this all. I have never used a javascript template engine before. Why do you recommend it for a task such as this?
facebook does something like this callback: { 'alert': 'user sees this' } and then the language parses the callback automatically with for example a switch statement.
I recommend them because they provide a clean separation of concerns (a valued design pattern for maintainable and robust code). You store your data as pure data and you delegate the rendering of that data with the appropriate view code (in this case, JavaScript). If you one day decided to render the menu with, say, Flash, that would be an easy option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.