-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackbone-business.js
94 lines (69 loc) · 2.56 KB
/
backbone-business.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
(function (Backbone) {
"use strict";
var _businessRules;
var evaluateViewVisibility = function(options) {
var view = options.view;
var facts = options.facts;
var ViewClassName
, ViewClass
, _default
, rule
, found = false
;
//TODO: do I even need the ViewClass? What am I doing with it?
//TODO: super inefficient search
//search doesn't need to be called every time we call evaluateViewVisibility
//eval might be slow
//linear search might be slow
for (ViewClassName in _businessRules.viewVisibility) {
if (ViewClassName === '_default') {
//do nothing
//_default is the only non-viewClass name allowed
}
else {
ViewClass = eval(ViewClassName);
if (view.constructor === ViewClass) {
rule = _businessRules.viewVisibility[ViewClassName];
found = true;
break;
}
}
}
if (!found) {
return _businessRules.viewVisibility._default;
}
else {
return Backbone.BusinessRuleProcessor.processRule({rule:rule, facts : facts});
}
};
Backbone.Business = function (options) {
var ViewClass = options.viewClass;
var businessRules = options.businessRules;
var facts = options.facts; //TODO: im not sure that facts should be passed in here. need to make sure that they can change, be added to etc.
_businessRules = businessRules;
return {
//TODO: constructor vs. initialize?
constructor : function () {
if (!facts || !(facts instanceof Backbone.Model)) {
throw 'facts must be a backbone model';
}
//this.listenTo(facts,'change', function(){console.error('change')});
this.listenTo(facts,'change', (function() { return this.render.apply(this, arguments)}).bind(this) ); //rerender when the facts model changes
//TODO: totally needs to be optimized, but not so bad for now
//wrapped in a function to make it easier to spyOn this.render
ViewClass.prototype.constructor.apply(this, arguments);
},
render : function () {
if (evaluateViewVisibility({view : this,facts : facts})) {
return ViewClass.prototype.render.apply(this, arguments);
}
else {
this.$el.empty(); //TODO: pretty crude, but gets the job done for now
}
},
injectBuisnessRules : function(businessRules) {
_businessRules = businessRules;
}
};
};
})(Backbone);