~tim-alwaysreformed/reformedchurcheslocator/rcl-backbone-test_for_directory_type

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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
var CouchAppObject = require('CouchAppObject').CouchAppObject,
	config = require('config'),
	db = config.db, // db should be a jquery.couch.js database object
	// Get jQuery from global namespace
	$ = window.$
if (config.env=='server'){
	// Load jQuery
	var $ = require('jquery')
}

// ---------------------------------------------------------------------
// Define model's sub-objects that 
//1) have the CouchAppObject base object as their prototype
//2) allow you to document your schema
//3) provide easy (attribute) access to related objects (via one-to-many and many-to-many relations)
//4) allow you to define custom views in one place
//5) auto-create (and auto-load on attribute access) default views and relationship views
//6) auto-persist data to CouchDB when the object changes, and auto-load data from the database 
//	when the object's underlying documents change in CouchDB
//7) allow you to add convenience methods to your model objects

var Cong = CouchAppObject.Type.sub()
$.extend(true, Cong, {
    // Cong object using the prototype method of inheritance
    type : 'congregation', // Required. Name the document type (else you won't have any relations!)
    groups: {
    	many_to_many:['congregation','cgroup'] // Declare many_to_many relation by listing the two 
											   // doc types involved in the relation.  The backref on
											   // the other type must be explicitly created.  Note
											   // that the order of the type names is not significant,
											   // because default view names are created by sorting 
											   // type names alphabetically.
    },
    name : '',
    meeting_address1 : '',
    meeting_address2:'',
    meeting_city:'',
    meeting_state:'',
    meeting_zip:'',
    meeting_country:'',
    lat:'',
    lng:'',
    mailing_address1:'',
    mailing_address2:'',
    mailing_city:'',
    mailing_state:'',
    mailing_zip:'',
    mailing_country:'',
    phone:'',
    fax:'',
    email:'',
    website:'',
    service_info:'',
    people:{many_to_many:['congregation','person']},
    date_founded:'', // date
    number_of_members:'', // integer
    range_of_number_of_members:'', // textual range, like '20-30' members, where estimates are 
    							   // 	permitted/preferred or the only available data
    organized:'', // boolean, defines whether this is a mission work or an organized congregation
    source:'', // Foreign key:  Which source this cong's data came from
    source_cong_id:'', // The ID of this cong in the source's database
    
    // Relations and other views in this format:
    //	view_name: { map:function(doc){}, reduce:function(keys, values){} }, ...
    views : {
    }
    // Optionally write other convenience functions here
})

var CGroup = CouchAppObject.Type.sub()
$.extend(true, CGroup, {
	// CGroup object using the prototype method of inheritance
	type : 'cgroup', // Required. Name the document type (else you won't have any relations!)
	name:'',
	abbreviation:'',
	people:{many_to_many:['cgroup','person']},
	website:'',
	congs:{many_to_many:['congregation','cgroup']},
    roles:{many_to_many:['person','role']},
	directories:{many_to_one:'directory', backref:'cgroup'},
	views:{
		
	}
})
var Directory = CouchAppObject.Type.sub()
$.extend(true, Directory, {
    // Directory object using the prototype method of inheritance
    type : 'directory', // Required. Name the document type (else you won't have any relations!)
    // Declare one_to_many relation by declaring the foreign key's doc type, and the backref.
    //	The backref on the other type must be explicitly created.
    cgroup:{one_to_many:'cgroup', backref:'directories'},
    get_url_contents:'', // true or false
    pagetype:'', // html or rss
    url:'', // url of directory's main page
    url_html:'', // HTML of directory's main page
    state_url:'', // URL of state page
    state_url_html:'', // HTML of state page
    state_page_values:[] // list of select box options for this directory's states
})
var Person = CouchAppObject.Type.sub()
$.extend(true, Person, {
    // Person object using the prototype method of inheritance
    type : 'person', // Required. Name the document type (else you won't have any relations!)
    prefix:'',
    firstname:'',
    lastname:'',
    suffix:'',
    phone:'',
    email:'',
    congs:{many_to_many:['congregation','person']},
    groups:{many_to_many:['cgroup','person']},
    roles:{many_to_many:['person','role']},
    office:{many_to_many:['person','office']}
})
var Office = CouchAppObject.Type.sub()
$.extend(true, Office, {
    // Office object using the prototype method of inheritance
    type : 'office', // Required. Name the document type (else you won't have any relations!)
    name:'',
    people:{many_to_many:['office','person']}
})
var Role = CouchAppObject.Type.sub()
$.extend(true, Role, {
    // Role object using the prototype method of inheritance
    type : 'role', // Required. Name the document type (else you won't have any relations!)
    name:'',
    people:{many_to_many:['role','person']}
})


//---------------------------------------------------------------------

// TODO: Consider making this model object have a prototype that has a function which feeds changes 
//  into its changes handlers.  Divide the changes handlers into two sections:  client and server-side
//  code.
var model = CouchAppObject.Model.sub()
$.extend(true, model, {
	// Declare model's types
	// TODO: Can this be automated?  Just iterate through this.properties and filter for the objects
	//	who are an instanceof("Type", Type) or if (obj instanceof Type){}
	types:{
	    Cong:Cong,
	    Person:Person,
	    CGroup:CGroup,
	    Directory:Directory,
	    Office:Office,
	    Role:Role
	},
	//Define model's migrations
	migration_version : 1,
    migrations : {
        1 : {
            upgrade : function() {

            },
            downgrade : function() {

            }
        }
    },
    // Define model's changes handlers
    changes_handlers:{
        client:[
                
        ],
        server:[
                
        ]
    }
})

exports.model = model