1 |
/* |
2 |
* This handles the following attribute properties |
3 |
* * init with function value in non-lazy initialization |
4 |
* * required attributes in initializaion |
5 |
* * handles for auto-decoration |
6 |
* * predicate for attribute availability checks |
7 |
*/ |
8 |
|
9 |
|
10 |
Class("Joose.Attribute", { |
11 |
after: { |
12 |
handleProps: function (classObject) { |
13 |
this.handleHandles(classObject); |
14 |
this.handlePredicate(classObject); |
15 |
} |
16 |
}, |
17 |
methods: { |
18 |
|
19 |
isPersistent: function () { |
20 |
var props = this.getProps() |
21 |
if(props.persistent == false) { |
22 |
return false |
23 |
} |
24 |
return true |
25 |
}, |
26 |
|
27 |
doInitialization: function (object, paras) { |
28 |
var name = this.initializerName(); |
29 |
var _name = this.getName(); |
30 |
var value; |
31 |
var set = false; |
32 |
if(typeof paras != "undefined" && typeof paras[name] != "undefined") { |
33 |
value = paras[name]; |
34 |
set = true; |
35 |
} else { |
36 |
var props = this.getProps(); |
37 |
|
38 |
if(props.required) { |
39 |
throw "Required initialization parameter missing: "+name + "(While initializing "+object+")" |
40 |
} |
41 |
|
42 |
var init = props.init; |
43 |
if(typeof init == "function" && !props.lazy) { |
44 |
// if init is not a function, we have put it in the prototype, so it is already here |
45 |
value = init.call(object) |
46 |
set = true |
47 |
} |
48 |
} |
49 |
if(set) { |
50 |
var setterName = this.setterName(); |
51 |
if(object.meta.can(setterName)) { // use setter if available |
52 |
object[setterName](value) |
53 |
} else { // direct attribute access |
54 |
object[_name] = value |
55 |
} |
56 |
} |
57 |
}, |
58 |
|
59 |
handleHandles: function (classObject) { |
60 |
var meta = classObject.meta; |
61 |
var name = this.getName(); |
62 |
var props = this.getProps(); |
63 |
|
64 |
var handles = props.handles; |
65 |
var isa = props.isa |
66 |
|
67 |
if(handles) { |
68 |
if(handles == "*") { |
69 |
if(!isa) { |
70 |
throw "I need an isa property in order to handle a class" |
71 |
} |
72 |
|
73 |
// receives the name and should return a closure |
74 |
var optionalHandlerMaker = props.handleWith; |
75 |
|
76 |
meta.decorate(isa, name, optionalHandlerMaker) |
77 |
} |
78 |
else { |
79 |
throw "Unsupported value for handles: "+handles |
80 |
} |
81 |
|
82 |
} |
83 |
}, |
84 |
|
85 |
handlePredicate: function (classObject) { |
86 |
var meta = classObject.meta; |
87 |
var name = this.getName(); |
88 |
var props = this.getProps(); |
89 |
|
90 |
var predicate = props.predicate; |
91 |
|
92 |
var getter = this.getterName(); |
93 |
|
94 |
if(predicate) { |
95 |
meta.addMethod(predicate, function () { |
96 |
var val = this[getter](); |
97 |
return val ? true : false |
98 |
}) |
99 |
} |
100 |
} |
101 |
} |
102 |
}) |