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

39 by Tim Black
Fix require() errors in couchapp, continue developing all aspects of CouchAppObject
1
var CouchAppObject = require('CouchAppObject').CouchAppObject,
2
	config = require('config'),
3
	db = config.db, // db should be a jquery.couch.js database object
4
	// Get jQuery from global namespace
5
	$ = window.$
6
if (config.env=='server'){
7
	// Load jQuery
34.1.7 by Tim Black
Split CouchObject into separate modules
8
	var $ = require('jquery')
9
}
32 by Tim Black
Added initial version of model code, including CouchObject ORM
10
34.1.9 by Tim Black
Continue developing CouchAppObject.Base, fill in fields in model objects
11
// ---------------------------------------------------------------------
32 by Tim Black
Added initial version of model code, including CouchObject ORM
12
// Define model's sub-objects that 
40 by Tim Black
CouchAppObject: Improve save() and db changes monitoring functions
13
//1) have the CouchAppObject base object as their prototype
14
//2) allow you to document your schema
15
//3) provide easy (attribute) access to related objects (via one-to-many and many-to-many relations)
16
//4) allow you to define custom views in one place
17
//5) auto-create (and auto-load on attribute access) default views and relationship views
18
//6) auto-persist data to CouchDB when the object changes, and auto-load data from the database 
19
//	when the object's underlying documents change in CouchDB
20
//7) allow you to add convenience methods to your model objects
34.1.6 by Tim Black
Make CouchObject able to run on server under Node, begin splitting CouchObject into modules
21
34.1.9 by Tim Black
Continue developing CouchAppObject.Base, fill in fields in model objects
22
var Cong = CouchAppObject.Type.sub()
32 by Tim Black
Added initial version of model code, including CouchObject ORM
23
$.extend(true, Cong, {
24
    // Cong object using the prototype method of inheritance
34.1.6 by Tim Black
Make CouchObject able to run on server under Node, begin splitting CouchObject into modules
25
    type : 'congregation', // Required. Name the document type (else you won't have any relations!)
34.1.9 by Tim Black
Continue developing CouchAppObject.Base, fill in fields in model objects
26
    groups: {
27
    	many_to_many:['congregation','cgroup'] // Declare many_to_many relation by listing the two 
28
											   // doc types involved in the relation.  The backref on
29
											   // the other type must be explicitly created.  Note
38 by Tim Black
Migrate code into couchapp
30
											   // that the order of the type names is not significant,
31
											   // because default view names are created by sorting 
32
											   // type names alphabetically.
34.1.9 by Tim Black
Continue developing CouchAppObject.Base, fill in fields in model objects
33
    },
34
    name : '',
35
    meeting_address1 : '',
36
    meeting_address2:'',
37
    meeting_city:'',
38
    meeting_state:'',
39
    meeting_zip:'',
40
    meeting_country:'',
41
    lat:'',
42
    lng:'',
43
    mailing_address1:'',
44
    mailing_address2:'',
45
    mailing_city:'',
46
    mailing_state:'',
47
    mailing_zip:'',
48
    mailing_country:'',
49
    phone:'',
50
    fax:'',
51
    email:'',
52
    website:'',
53
    service_info:'',
54
    people:{many_to_many:['congregation','person']},
55
    date_founded:'', // date
56
    number_of_members:'', // integer
57
    range_of_number_of_members:'', // textual range, like '20-30' members, where estimates are 
58
    							   // 	permitted/preferred or the only available data
59
    organized:'', // boolean, defines whether this is a mission work or an organized congregation
60
    source:'', // Foreign key:  Which source this cong's data came from
61
    source_cong_id:'', // The ID of this cong in the source's database
62
    
63
    // Relations and other views in this format:
64
    //	view_name: { map:function(doc){}, reduce:function(keys, values){} }, ...
32 by Tim Black
Added initial version of model code, including CouchObject ORM
65
    views : {
34.1.6 by Tim Black
Make CouchObject able to run on server under Node, begin splitting CouchObject into modules
66
    }
34.1.9 by Tim Black
Continue developing CouchAppObject.Base, fill in fields in model objects
67
    // Optionally write other convenience functions here
68
})
69
70
var CGroup = CouchAppObject.Type.sub()
71
$.extend(true, CGroup, {
72
	// CGroup object using the prototype method of inheritance
73
	type : 'cgroup', // Required. Name the document type (else you won't have any relations!)
74
	name:'',
75
	abbreviation:'',
76
	people:{many_to_many:['cgroup','person']},
77
	website:'',
38 by Tim Black
Migrate code into couchapp
78
	congs:{many_to_many:['congregation','cgroup']},
79
    roles:{many_to_many:['person','role']},
39 by Tim Black
Fix require() errors in couchapp, continue developing all aspects of CouchAppObject
80
	directories:{many_to_one:'directory', backref:'cgroup'},
81
	views:{
82
		
83
	}
38 by Tim Black
Migrate code into couchapp
84
})
85
var Directory = CouchAppObject.Type.sub()
86
$.extend(true, Directory, {
87
    // Directory object using the prototype method of inheritance
88
    type : 'directory', // Required. Name the document type (else you won't have any relations!)
89
    // Declare one_to_many relation by declaring the foreign key's doc type, and the backref.
90
    //	The backref on the other type must be explicitly created.
91
    cgroup:{one_to_many:'cgroup', backref:'directories'},
92
    get_url_contents:'', // true or false
93
    pagetype:'', // html or rss
94
    url:'', // url of directory's main page
95
    url_html:'', // HTML of directory's main page
96
    state_url:'', // URL of state page
97
    state_url_html:'', // HTML of state page
98
    state_page_values:[] // list of select box options for this directory's states
34.1.9 by Tim Black
Continue developing CouchAppObject.Base, fill in fields in model objects
99
})
100
var Person = CouchAppObject.Type.sub()
101
$.extend(true, Person, {
102
    // Person object using the prototype method of inheritance
103
    type : 'person', // Required. Name the document type (else you won't have any relations!)
104
    prefix:'',
105
    firstname:'',
106
    lastname:'',
107
    suffix:'',
108
    phone:'',
109
    email:'',
38 by Tim Black
Migrate code into couchapp
110
    congs:{many_to_many:['congregation','person']},
111
    groups:{many_to_many:['cgroup','person']},
34.1.9 by Tim Black
Continue developing CouchAppObject.Base, fill in fields in model objects
112
    roles:{many_to_many:['person','role']},
113
    office:{many_to_many:['person','office']}
114
})
115
var Office = CouchAppObject.Type.sub()
116
$.extend(true, Office, {
117
    // Office object using the prototype method of inheritance
118
    type : 'office', // Required. Name the document type (else you won't have any relations!)
119
    name:'',
120
    people:{many_to_many:['office','person']}
121
})
122
var Role = CouchAppObject.Type.sub()
123
$.extend(true, Role, {
124
    // Role object using the prototype method of inheritance
125
    type : 'role', // Required. Name the document type (else you won't have any relations!)
126
    name:'',
127
    people:{many_to_many:['role','person']}
128
})
129
130
131
//---------------------------------------------------------------------
34.1.6 by Tim Black
Make CouchObject able to run on server under Node, begin splitting CouchObject into modules
132
133
// TODO: Consider making this model object have a prototype that has a function which feeds changes 
134
//  into its changes handlers.  Divide the changes handlers into two sections:  client and server-side
135
//  code.
34.1.9 by Tim Black
Continue developing CouchAppObject.Base, fill in fields in model objects
136
var model = CouchAppObject.Model.sub()
34.1.6 by Tim Black
Make CouchObject able to run on server under Node, begin splitting CouchObject into modules
137
$.extend(true, model, {
138
	// Declare model's types
39 by Tim Black
Fix require() errors in couchapp, continue developing all aspects of CouchAppObject
139
	// TODO: Can this be automated?  Just iterate through this.properties and filter for the objects
140
	//	who are an instanceof("Type", Type) or if (obj instanceof Type){}
34.1.6 by Tim Black
Make CouchObject able to run on server under Node, begin splitting CouchObject into modules
141
	types:{
39 by Tim Black
Fix require() errors in couchapp, continue developing all aspects of CouchAppObject
142
	    Cong:Cong,
143
	    Person:Person,
144
	    CGroup:CGroup,
145
	    Directory:Directory,
146
	    Office:Office,
147
	    Role:Role
34.1.6 by Tim Black
Make CouchObject able to run on server under Node, begin splitting CouchObject into modules
148
	},
149
	//Define model's migrations
150
	migration_version : 1,
151
    migrations : {
152
        1 : {
153
            upgrade : function() {
154
155
            },
156
            downgrade : function() {
157
158
            }
159
        }
32 by Tim Black
Added initial version of model code, including CouchObject ORM
160
    },
34.1.6 by Tim Black
Make CouchObject able to run on server under Node, begin splitting CouchObject into modules
161
    // Define model's changes handlers
162
    changes_handlers:{
34.1.8 by Tim Black
Outline CouchObject migrations and changes_handlers iteration
163
        client:[
164
                
165
        ],
166
        server:[
167
                
168
        ]
34.1.6 by Tim Black
Make CouchObject able to run on server under Node, begin splitting CouchObject into modules
169
    }
32 by Tim Black
Added initial version of model code, including CouchObject ORM
170
})
171
34.1.7 by Tim Black
Split CouchObject into separate modules
172
exports.model = model