Permalink
Browse files

- detect when a class (a function like: `func.meta.c == func`) has be…

…en provided as `init` value for the attribute and do not call it during initialization
  • Loading branch information...
SamuraiJack committed Aug 24, 2011
1 parent 0c06c6d commit 46afa17086ec7e3c48fb3de98d89deab2e3596a5
Showing with 45 additions and 11 deletions.
  1. +2 −1 Changes
  2. +3 −1 bench/010_basic.js
  3. +1 −1 bench/index.html
  4. +5 −0 lib/Joose.js
  5. +11 −5 lib/Joose/Managed/Attribute.js
  6. +4 −0 lib/Joose/Manual/Attributes.js
  7. +19 −3 t/052_advanced_attribute.t.js
View
@@ -4,7 +4,8 @@ Release history for Joose:
[ENHANCEMENTS]
- - none
+ - detect when a class (a function like: `func.meta.c == func`) has been provided as `init` value for the attribute
+ and do not call it during initialization
[API CHANGES]
View
@@ -1,6 +1,7 @@
StartBenchmark(
{
- title : 'Class creation time',
+ title : 'Simple class creation time',
+ id : 'class-creation-simple',
run : function () {
return Class({
@@ -21,6 +22,7 @@ StartBenchmark(
},
{
title : 'Class instantiation time',
+ id : 'class-instantiation-simple',
prepare : function () {
return {
View
@@ -1,4 +1,4 @@
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="../../ext-4.0.2a/resources/css/ext-all.css">
View
@@ -182,6 +182,11 @@ Joose.O = {
},
+ isClass : function (obj) {
+ return obj && obj.meta && obj.meta.c == obj
+ },
+
+
wantArray : function (obj) {
if (obj instanceof Array) return obj
@@ -50,7 +50,9 @@ Joose.Managed.Attribute = new Joose.Managed.Class('Joose.Managed.Attribute', {
override : {
computeValue : function () {
- if (!Joose.O.isFunction(this.init)) this.SUPER()
+ var init = this.init
+
+ if (Joose.O.isClass(init) || !Joose.O.isFunction(init)) this.SUPER()
},
@@ -179,18 +181,22 @@ Joose.Managed.Attribute = new Joose.Managed.Class('Joose.Managed.Attribute', {
if (config.hasOwnProperty(name)) {
value = config[name]
isSet = true
- } else
- if (Joose.O.isFunction(this.init)) {
+ } else {
+ var init = this.init
+
+ // simple function (not class) has been used as "init" value
+ if (Joose.O.isFunction(init) && !Joose.O.isClass(init)) {
- value = this.init.call(instance, config, name)
+ value = init.call(instance, config, name)
isSet = true
} else if (this.builder) {
value = instance[ this.builder.replace(/^this\./, '') ](config, name)
isSet = true
- }
+ }
+ }
if (isSet)
this.setRawValueTo(instance, value)
@@ -90,10 +90,13 @@ To specify the default value for the attribute use `init` property. In the examp
If you'll provide a function as the value for `init`, it will be called during initialization with the following signature:
`func(attributeName, config)`, where `config` is the 1st argument passed to constructor. Function will be called as class's method (in the instance's scope).
+**The exception** from this rule is the function which represent the constructor of some class. Such function **will not** be called during initialization.
+
Value, returned from function, will be used for initialization. **Note**, that if there is setter method, it won't be used during initialization - value will be just assigned to instance.
Any other values, provided as `init` value, will be applied to prototype of the class directly.
+
Also, to keep the attributes declaration section clean, you can specify the method to call, to receive the default value with `builder` option:
Class('User', {
@@ -119,6 +122,7 @@ Simplified form. Attribute helpers
Advanced attributes also allows a simplified form, when the attribute's properties object is provided as an atomic value (string or number for example).
In this case, this value is treated as value of `init` property, value of `is` remains undefined (no accessors will be created for this attribute).
+
The following examples are equivalent:
Class('User', {
@@ -81,8 +81,8 @@ StartTest(function (t) {
has : {
res : {
- is : 'rw',
- init : 'fromClass'
+ is : 'rw',
+ init : 'fromClass'
}
}
})
@@ -110,8 +110,24 @@ StartTest(function (t) {
t.ok(bar.regex1.test(/123abc123/), 'Attribute was correctly initialized with RegExp #1')
t.ok(bar.regex2.test(/123abc123/), 'Attribute was correctly initialized with RegExp #2')
- t.autoCheckGlobals = false
+
+ //==================================================================================================================================================================================
+ t.diag("Webkit & RegExp attributes")
+
+
+ Class('Baz', {
+ has : {
+ barClass : Bar
+ }
+ })
+
+ var baz = new Baz()
+
+ t.ok(baz.barClass == Bar, 'Correct attribute initialization with Class')
+
+
+ t.expectGlobals('Test', 'TestRole', 'TestClass', 'TestClass2', 'Bar', 'Baz')
t.done()
})

0 comments on commit 46afa17

Please sign in to comment.