1 // ==========================================================================
  2 // Project:   The M-Project - Mobile HTML5 Application Framework
  3 // Copyright: (c) 2010 M-Way Solutions GmbH. All rights reserved.
  4 //            (c) 2011 panacoda GmbH. All rights reserved.
  5 // Creator:   Dominik
  6 // Date:      26.10.2010
  7 // License:   Dual licensed under the MIT or GPL Version 2 licenses.
  8 //            http://github.com/mwaylabs/The-M-Project/blob/master/MIT-LICENSE
  9 //            http://github.com/mwaylabs/The-M-Project/blob/master/GPL-LICENSE
 10 // ==========================================================================
 11 
 12 m_require('core/foundation/m.js');
 13 
 14 /**
 15  * @class
 16  *
 17  * Base class of all objects.
 18  */
 19 
 20 /**
 21  * @constructor
 22  */
 23 M.Object =
 24 /** @scope M.Object.prototype */ {
 25 
 26     /**
 27      * The type of this object.
 28      *
 29      * @type String
 30      */
 31     type: 'M.Object',
 32 
 33     /**
 34      * Creates an object based on a passed prototype.
 35      *
 36      * @param {Object} proto The prototype of the new object.
 37      */
 38     create: function(proto) {
 39         var f = function(){};
 40         f.prototype = proto;
 41         return new f();
 42     },
 43 
 44     /**
 45      * Includes passed properties into a given object.
 46      *
 47      * @param {Object} properties The properties to be included into the given object.
 48      */
 49     include: function(properties) {
 50         for(var prop in properties) {
 51             this[prop] = properties[prop];
 52         }
 53     },
 54 
 55     /**
 56      * Creates a new class and extends it with all functions of the defined super class
 57      * The function takes multiple input arguments. Each argument serves as additional
 58      * super classes - see mixins.
 59      *
 60      * @param {Object} properties The properties to be included into the given object.
 61      */
 62     extend: function(properties){
 63         /* create the new object */
 64         var obj = M.Object.create(this);
 65 
 66         /* assign the properties passed with the arguments array */
 67         obj.include(properties);
 68 
 69         /* return the new object */
 70         return obj;
 71     },
 72 
 73     /**
 74      * Binds a method to its caller, so it is always executed within the right scope.
 75      *
 76      * @param {Object} caller The scope of the method that should be bound.
 77      * @param {Object} method The method to be bound.
 78      * @param {Object} arg One or more arguments. If more, then apply is used instead of call.
 79      */
 80     bindToCaller: function(caller, method, arg) {
 81         return function() {
 82             if(_.isArray(arg)) {
 83                 return method.apply(caller, arg);
 84             }
 85             return method.call(caller, arg);
 86         }
 87     },
 88 
 89     /**
 90      * Returns the class property behind the given key.
 91      *
 92      * @param {String} key The key of the property to be returned.
 93      */
 94     get: function(key) {
 95         return this[key];
 96     },
 97 
 98     /**
 99      * Returns the class property behind the given key.
100      *
101      * @param {String} key The key of the property to be changed.
102      * @param {Object|String} value The value to be set.
103      */
104     set: function(key, value) {
105         this[key] = value;
106     },
107 
108     /**
109      * This method will remove an object from the DOM and then delete it. 
110      */
111     destroy: function() {
112         if(this.id && $('#' + this.id)) {
113             M.EventDispatcher.unregisterEvents(this);
114             M.ViewManager.unregister(this);
115             $('#' + this.id).remove();
116         }
117         delete this;
118     }
119 
120 };