dreamerslab / node.class

A port of John Resig Class Implementation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

extend() method not in child class

tarach opened this issue · comments

For some reason child class has no extend() method.

I wrote small test and it works like a charm:

var Class = require("node.class");

var Person = Class.extend({

  // constructor
  init: function ( name, height ){
    this.name   = name;
    this.height = height;
  },

  getName: function (){
    return this.name;
  },
  getHeight: function (){
    return this.height;
  }
});

var ben = new Person( 'ben', '176' );

console.log(Person)
console.log(Person.prototype)
console.log(ben.getHeight()); // 176

var Car = Class.extend({

  init : function ( speed ){
    this.speed = speed;
  }
});

var SportsCar = Car.extend({
        turbo: function() {
                console.log("Wrrrrrrum! " + this.speed);
        }
});

But on my Config class it does not give an extend method for some weird reason so I can't extend my JsonConfig class that is suppose to be abstract.

define(["observer/_subject", "node.class"], function(Subject, Class)
{
    var fs = require('fs');

    var file_path_validator = function(file_path)
    {
        var is_ok = true;
        try {
            fs.accessSync(file_path, fs.F_OK);
        } catch(err)
        {
            is_ok = false;
        }
        return is_ok;
    };

    return Class.extend({
        events: {
            /**
             * When config starts loading
             */
            onLoad: 1,
            /**
             * When json will be parsed and saved as data
             */
            onLoaded: 2,
            /**
             * When config contents change
             */
            onContentChange: 3
        },
        init: function(file_path)
        {
            this.error = null;
            this.file_path = null;
            this.file_path_validator = null;
            /**
             * Stores data as a string before saving the to file or before parsing them into data
             * @type {String}
             */
            this.Contents = null;
            /**
             * Config data
             * @type {*}
             */
            this.data = {};
            /**
             * Observer collection
             * @type Subject
             */
            this.Subject = null;

            this.setFilePath(file_path);
            this.setFilePathValidator(file_path_validator);
            this.setSubject(new Subject());
        },
        /**
         * Loads configuration file. Returns 1 on success -1 if file
         * didn't passed validation and -2 if error occurred while parsing its contents to JSON.
         * Calls 3 events on different occasions
         * @returns {*}
         */
        loadSync: function()
        {
            this.setError(null);

            var file_path = this.getFilePath(),
                Subject = this.getSubject(),
                validator = this.getFilePathValidator();

            Subject.notify(this.events.onLoad, this);
            // validate path

            if(validator(file_path))
            {
                this.setContents(fs.readFileSync(file_path, "utf8"));
                Subject.notify(this.events.onContentChange, this);
                try {
                    this.setData(JSON.parse(this.getContents()));
                } catch(err)
                {
                    this.setError(err);
                    return -2;
                }
                Subject.notify(this.events.onLoaded, this);

                return 1;
            } else
            {
                return -1;
            }
        },
        /**
         * Returns data from loaded configuration file or def value if it does not exists
         * @param id
         * @param def
         * @returns {*|boolean}
         */
        get: function(id, def)
        {
            return this.data[id] || (def || false);
        },
        getContents: function()
        {
            return this.Contents;
        },
        getData: function()
        {
            return this.data;
        },
        getError: function()
        {
            return this.error;
        },
        getFilePath: function()
        {
            return this.file_path;
        },
        getFilePathValidator: function()
        {
            return this.file_path_validator;
        },
        getSubject: function()
        {
            return this.Subject;
        },
        set: function(id, value)
        {
            this.data[id] = value;
        },
        setContents: function(Contents)
        {
            this.Contents = Contents;
        },
        setData: function(data)
        {
            this.data = data;
        },
        setError: function(error)
        {
            this.error = error;
        },
        setFilePath: function(file_path)
        {
            this.file_path = file_path;
        },
        setFilePathValidator: function(validator)
        {
            this.file_path_validator = validator;
        },
        setSubject: function(Subject)
        {
            this.Subject = Subject;
        }
    });
});
commented

@tarach Is the code frontend or backend? Your example confused me a bit since this define(["observer/_subject", "node.class"], function(Subject, Class) part looks like frontend code but this var fs = require('fs'); looks like backend code.

It's a node webkit so you could say it's both. It's loaded via requirejs which means it's executed in webkit context but require('fs') executes in node context.

commented

Hi @tarach I have no experience with node webkit but my guess is the loader messed up the library.