Search code examples
javascriptphpnode.jsdata-objects

node.js data model object syntax


I have a feeling this is a total noob question but I wasn't able to find any information so here goes.

I need to "translate" php data model objects to JavaScript data model objects for a node.js project. I am pretty new to node.js and I simply don't understand the syntax of the data model objects.

Here's the barebones template that was provided (user.js):

'use strict';

module.exports = function UserModel() {
    return {
        name: 'username'
    };
};

Here's part of the php model I am working off of:

class Model_User extends Base_Model {
    'name' => array(
        'first' => 'first_name',
        'last' => 'last_name',
    ),

    'friends' => array(
        'model' => 'User',
    ),
}

I have written ActionScript data model objects in the past and I thought JS would be mostly identical but in AS the data properties are declared as separate vars. Looking at the barebones template above, that doesn't seem to be the case for node.js JS data model objects.


Solution

  • What about:

    var model = {
        'name': [
            { 'first': 'first_name' },
            { 'last': 'last_name' }
        ],
        'friends': [
            { 'model': 'User' }
        ]
    };
    

    This is basically creating a model object. It contains two key -> value pairs name & friends, which both contain arrays of objects.

    Read this for more info on javascript objects: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects