@qooxdoo/framework
Version:
The JS Framework for Coders
153 lines (124 loc) • 4.33 kB
JavaScript
/* ************************************************************************
qooxdoo - the new era of web development
http://qooxdoo.org
Copyright:
2004-2011 1&1 Internet AG, Germany, http://www.1und1.de
License:
MIT: https://opensource.org/licenses/MIT
See the LICENSE file in the project's top-level directory for details.
Authors:
* Martin Wittemann (martinwittemann)
************************************************************************ */
/**
* This mixin offers the basic property features which include generic
* setter, getter and resetter.
*/
qx.Mixin.define("qx.core.MProperty",
{
members :
{
/**
* Sets multiple properties at once by using a property list or
* sets one property and its value by the first and second argument.
* As a fallback, if no generated property setter could be found, a
* handwritten setter will be searched and invoked if available.
*
* @param data {Map | String} a map of property values. The key is the name of the property.
* @param value {var?} the value, only used when <code>data</code> is a string.
* @return {Object} Returns this instance if <code>data</code> is a map
* or a non-generated setter is called; otherwise returns <code>value</code>.
* @throws {Error} if a property defined does not exist
*/
set : function(data, value)
{
var setter = qx.core.Property.$$method.set;
if (qx.Bootstrap.isString(data))
{
if (!this[setter[data]])
{
if (this["set" + qx.Bootstrap.firstUp(data)] != undefined) {
this["set" + qx.Bootstrap.firstUp(data)](value);
return this;
}
throw new Error("No such property: " + data);
}
return this[setter[data]](value);
}
else
{
for (var prop in data)
{
if (!this[setter[prop]])
{
if (this["set" + qx.Bootstrap.firstUp(prop)] != undefined) {
this["set" + qx.Bootstrap.firstUp(prop)](data[prop]);
continue;
}
throw new Error("No such property: " + prop);
}
this[setter[prop]](data[prop]);
}
return this;
}
},
/**
* Returns the value of the given property. If no generated getter could be
* found, a fallback tries to access a handwritten getter.
*
* @param prop {String} Name of the property.
* @return {var} The value of the value
* @throws {Error} if a property defined does not exist
*/
get : function(prop)
{
var getter = qx.core.Property.$$method.get;
if (!this[getter[prop]])
{
if (this["get" + qx.Bootstrap.firstUp(prop)] != undefined) {
return this["get" + qx.Bootstrap.firstUp(prop)]();
}
throw new Error("No such property: " + prop);
}
return this[getter[prop]]();
},
/**
* Resets the value of the given property. If no generated resetter could be
* found, a handwritten resetter will be invoked, if available.
*
* @param prop {String} Name of the property.
* @throws {Error} if a property defined does not exist
*/
reset : function(prop)
{
var resetter = qx.core.Property.$$method.reset;
if (!this[resetter[prop]])
{
if (this["reset" + qx.Bootstrap.firstUp(prop)] != undefined) {
this["reset" + qx.Bootstrap.firstUp(prop)]();
return;
}
throw new Error("No such property: " + prop);
}
this[resetter[prop]]();
},
/**
* Checks if the property is initialized, i.e. has a defined init value or
* has got a value by a setter method.
*
* @param prop {String} Name of the property
* @return {Boolean} If the property is initialized
* @throws {Error} If the property defined does not exist
*/
isPropertyInitialized : function(prop)
{
if (qx.core.Environment.get("qx.debug")) {
qx.core.Assert.assertString(prop);
if (!this["get" + qx.Bootstrap.firstUp(prop)]) {
throw new Error("No such property: " + prop);
}
}
return this["$$user_" + prop] !== undefined ||
this["$$init_" + prop] !== undefined;
}
}
});