Define Base#toString() as a mean to render Base objects as object literals, and use it for Key.modifier and onFrame() event objects, through Base.merge() conversion.

This commit is contained in:
Jürg Lehni 2011-07-07 14:10:02 +02:00
parent baf7136ef7
commit 2851d8e403
3 changed files with 26 additions and 22 deletions

View file

@ -14,14 +14,17 @@
* All rights reserved.
*/
// Extend Base with utility functions used across the library. Also set
// this.Base on the injection scope, since bootstrap.js ommits that.
/**
* @name Base
* @class
* @private
*/
// Extend Base with utility functions used across the library. Also set
// this.Base on the injection scope, since bootstrap.js ommits that.
this.Base = Base.inject(/** @lends Base# */{
// Have generics versions of #clone() and #toString():
generics: true,
/**
* General purpose clone function that delegates cloning to the constructor
* that receives the object to be cloned as the first argument.
@ -32,6 +35,16 @@ this.Base = Base.inject(/** @lends Base# */{
return new this.constructor(this);
},
/**
* Renders base objects to strings in object literal notation.
*/
toString: function() {
return '{ ' + Base.each(this, function(value, key) {
this.push(key + ': ' + (typeof value === 'number'
? Base.formatNumber(value) : value));
}, []).join(', ') + ' }';
},
statics: /** @lends Base */{
/**
* Reads arguments of the type of the class on which it is called on
@ -102,12 +115,15 @@ this.Base = Base.inject(/** @lends Base# */{
}
},
/**
* Merge all passed hash objects into a newly creted Base object.
*/
merge: function() {
return Base.each(arguments, function(hash) {
Base.each(hash, function(value, key) {
this[key] = value;
}, this);
}, {}, true); // Pass true for asArray.
}, new Base(), true); // Pass true for asArray, as arguments is none
},
/**
@ -142,16 +158,6 @@ this.Base = Base.inject(/** @lends Base# */{
*/
formatNumber: function(num) {
return (Math.round(num * 100000) / 100000).toString();
},
/**
* Utility function for rendering objects to strings, in object literal
* notation.
*/
formatObject: function(obj) {
return '{ ' + Base.each(obj, function(value, key) {
this.push(key + ': ' + value);
}, []).join(', ') + ' }';
}
}
});

View file

@ -43,17 +43,14 @@ var Key = this.Key = new function() {
91: 'command'
},
modifiers = {
// Use Base.merge to convert into a Base object, for #toString()
modifiers = Base.merge({
shift: false,
control: false,
option: false,
command: false,
capsLock: false,
toString: function() {
return Base.formatObject(this);
}
},
capsLock: false
}),
// Since only keypress gets proper keyCodes that are actually representing
// characters, we need to perform a little trickery here to use these codes

View file

@ -357,11 +357,12 @@ var View = this.View = Base.extend(/** @lends View# */{
}
var now = Date.now() / 1000,
delta = before ? now - before : 0;
that._onFrame({
// Use Base.merge to convert into a Base object, for #toString()
that._onFrame(Base.merge({
delta: delta, // Time elapsed since last redraw in seconds
time: time += delta, // Time since first call of frame() in seconds
count: count++
});
}));
before = now;
// Automatically draw view on each frame.
that.draw(true);