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