awa.mvc.Model Class
/var/www/azaleahealth.com/branches/docs/source_repo/awa/resources/apps/js/awa/mvc/Model.js:34
Abstract version of a model to use.
Please use this model instead of directly extending the backbone model. This is will help minimize the amount of reused code needed to handle various factors (i.e. ahis session id, multiple backend protocols)
See the following for parent class definition: http://backbonejs.org/#Model See awa.mvc.ModelAttributeTypes and awa.mvc.ModelAttributeTypes.ModelAttributeType for examples and descriptions of possible attributes
An example Model definition
define(
['awa'],
function(awa){
var ExampleModel = awa.mvc.Model.extend({
//this function returns the base url for the model's concepts.
//depending on the api type, this url is expanded based on the server action
urlRoot: function(){
return awa.url.BAREBONES + "V1/examples";
},
//specifies which ajax strategy to use. can also be 'api'
api: 'barebones',
//This is used to build the url and to determine if the model isNew
idAttribute: 'exp_id',
//This is used by grids and the isDisabled function
disabledAttribute: 'exa_disabled',
//This defines the initial values of certain properties when initializing a new model
defaults:{
exa_string: 'default string'
},
//Gets preferred display name of property
//ie. PatientModel => pat_fname + ' ' + pat_lname
//ie. InsuranceModel => ins_mnemonic || ins_name
displayName: function(){
return this.get('exa_name');
},
//Defined properties in all models.
//awa.mvc.ModelAttributeTypes
//awa.mvc.ModelAttributeTypes.ModelAttributeType
attributeProps:{
exa_string: {
name: "Example String",
rules:{
require: true,
date: true
}
},
exa_date: {
name: "Example Date (8 digit VARCHAR or DATE)",
type: 'date',
rules:{
require: true
}
},
exa_phone: {
name: "Example Phone (10~20 character VARCHAR)",
type: 'phone',
rules:{
usPhoneFormat: true
}
},
exa_ts: {
name: "Example Timestamp (Local to client, 12 digit VARCHAR)",
type: 'timestamp',
rules:{
require: true
}
},
exa_datetime: {
name: "Example Timestamp (ALWAYS EST, DATETIME)",
type: 'mysql_timestamp',
rules:{
require: true
}
},
exp_foreign_key_id: {
name: "Example foreign key property",
type: 'listValue',
//collection can be the collection or a function that returns a collection
collection: someCollection
},
exp_boolean: {
name: "Example Boolean (like disabled)",
type: 'boolean'
}
}
});
return ExampleModel;
});
//events
update
lock
Similar to normal classes, each attribute should be defined in the attributeProps. This allows naming, rules, formatting, etc. to be centralized into one area. i.e. the above property exp_string has the name "Example String". This will be used (by default) in grids, model displays, and edit views. See awa.mvc.ModelAttributeTypes for more details.
Validation also takes place in the model (for the most part). Each attribute has it's own set of rules. See awa.mvc.ModelRules for existing rules and how they apply.
Item Index
Methods
- _validateChildCollection
- add
- addAttributeProp static
- addAttributeProp static
- addRules
- createCollection static
- destroy
- difference
- displayName
- enable
- fetch static
- fetchAll static
- fillAttributeProp static
- get
- getAPI static
- getAttributeProp
- getAttributeProps static
- getCollection static
- getDisplay
- getDisplayMethod
- getFilters static
- getIDAttribute static
- getModelDescription static
- getModelName static
- getModelName static
- getName
- getScope
- getSearchCollection static
- getTip
- getType
- getURLRoot static
- hasExport static
- isDisabled
- isIncomplete
- isNotDone
- isValid
- objectAccess
- objectFilter
- removeRules
- restore
- set
- setRules
- store
- subAction
- sync
- toDisplayJSON
- toJSON
- toString
- url
- validate
Properties
Events
Methods
_validateChildCollection
-
key -
collection -
errors
CAUTION errors param will be modified
Parameters:
-
keyStringAttribute key
-
collectionawa.mvc.CollectionCollection to check
-
errorsObjectObject of errors
Returns:
Indicates this method found errors in the collection
add
-
attribute -
value -
[id] -
[options]
The add method is used primarily for arrays.
Parameters:
-
attributeStringattribute name
-
valueObjectvalue to be added to the array
-
[id]Integer optionalthe specific index
-
[options]Object | String optionalrefer to Backbone.js
Returns:
[description]
addAttributeProp
-
args
Allows for the addition of
Parameters:
-
argsObjectdesc
Returns:
Collection
addAttributeProp
-
attr -
prop
Allows for the addition of
Parameters:
-
attrStringdesc
-
propObjectdesc
Returns:
not available
addRules
-
key -
rules
appends rules to the model
Parameters:
-
keyObject | Stringdesc
-
rulesObject | Stringdesc
Returns:
[description]
createCollection
-
initialCollection -
options
Generates a new collection from the given model
Parameters:
-
initialCollectionArrayinitial collection to start with.
-
optionsObjectParameter bag
Returns:
prototype chain - Collection
destroy
-
options
Stolen from backbone and completely overridden. Look in method for change
Parameters:
-
optionsObjectconfiguration object with various properties
Returns:
[description]
difference
-
obj
Parameters:
-
objObjectconfiguration object with various properties
Returns:
[description]
displayName
()
type
Override to return the name of the model (i.e. for a patient, it would be their last name, comma, space, and then first name). By default, this returns attribute name, id, then cid.
Returns:
[description]
enable
-
[options]
If the model has a disabledAttribute, this method will automatically re-enable by setting the property to 0
Parameters:
-
[options]Object optionalconfiguration object with various properties
Returns:
[description]
fetch
-
id -
options
Global repository
Parameters:
-
idString | Integerdesc
-
optionsObjectdesc
Returns:
returns resolve promise
fetchAll
-
options
Global repository
Parameters:
-
optionsObjectdesc
Returns:
Collection
fillAttributeProp
-
attr -
prop
adds attribute prop data but does not override existing key values
Parameters:
-
attrStringdesc
-
propObjectdesc
Returns:
not available
get
-
attr
Get the current value of an attribute from the model Overwritten to create collections and sub models only when needed.
Parameters:
-
attrStringdesc
Returns:
[description]
getAPI
()
String
static
Returns the api for the given model class
Returns:
api Api for model. directs sync functionality
getAttributeProp
-
attr -
prop
Gets the attribute property for a specified attribute.
Parameters:
-
attrStringAttribute key (i.e pat_fname)
-
propStringProperty key (i.e displayMethod)
Returns:
[description]
getAttributeProps
-
attr -
prop
Parameters:
-
attrObjectAttribute key
-
propObjectProperty
Returns:
prop desc
getCollection
-
options
Global repository. This can be affected by uses outside of your own.
Parameters:
-
optionsStringdesc
Returns:
prototype chain - Collection
getDisplay
-
attr -
options
Like get, but this uses the displayMethod.
Parameters:
-
attrStringdesc
-
optionsObjectdesc
Returns:
[description]
getDisplayMethod
-
attr
returns the displayMethod for a given attribute
Parameters:
-
attrStringdesc
Returns:
[description]
getFilters
-
options
get filters collection for model collection
Parameters:
-
optionsObjectdesc
Returns:
Collection
getIDAttribute
()
String
static
Return id attribute key
Returns:
ID attribute of model
getModelDescription
()
String
static
Returns a description of the model. You can set this in the model with property modelDescription i.e. Patient Class would be "Labels that provide a means to better organize patients"
Returns:
desc of model
getModelName
()
String
static
Returns plural name of model class. by default it appends an 's' to the name. override in cases where needed. (i.e. diagnosis => diagnoses) i.e. Patient model would be Patients
Returns:
desc of name
getModelName
()
String
static
Returns name of model class. you can set this in the model i.e. Patient model would be Patient
Returns:
desc
getName
-
attr -
fullName
Gets the name of an attribute. This can be set by an attribute's attributeProp name. An attribute property can have a getName function to define how to name nested portions NOTE: The containing object of a property should decide the properties name. i.e. if a model has a "period" attribute prop, that should be named in the model, not the period attributeProps returns the attribute if not set.
Parameters:
-
attrStringAttribute key desired. If nothing is provided, the model's displayName is returned.
-
fullNameBooleanIf true returns the full name for nested properties from the context of the root model. i.e. "Encounter Primary Insurance Period Start" v. "Start"
Returns:
attribute name
getScope
-
options
Id this is a nested model inside of a collection or another model, this will provide the extended id to grab it from the root model.
Parameters:
-
optionsObjectdesc
-
[cssSafe]Boolean optionalreplaces attribute separator with dashes for css selection if true
-
[cid]Boolean optionalby default uses cid. if set to false, will use ID instead
-
Returns:
[description]
getSearchCollection
-
options
Collection used for searching in this model. columns and filters should be provided in the collection for grids.
Parameters:
-
optionsStringdesc
Returns:
prototype chain - Collection
getTip
-
attr
Gets the tip for an attribute. This can be set by an attribute's attributeProp tip. returns the attribute if not set.
Parameters:
-
attrStringdesc
Returns:
[description]
getType
-
attr
Gets the attribute type (i.e. string, collection, date), the default type is string
Parameters:
-
attrStringdesc
Returns:
[description]
getURLRoot
()
String
static
Returns the url root for the given model class
Returns:
urlRoot for model
hasExport
()
Boolean
static
Returns:
desc
isDisabled
()
Boolean
Tells whether or not an individual has disabled status.
Returns:
desc
isIncomplete
()
Boolean
Tells whether or not an individual has incomplete status. Override this method if you want to use it in a model.
Returns:
desc
isNotDone
()
Boolean
Tells whether or not an item was not done. Primarily for medical events (i.e. procedures, care plans, prescriptions)
Returns:
desc
isValid
-
Refer
A convenience function for returning boolean values instead of objects
Parameters:
-
ReferObjectto validate() for parameter list and notes.
Returns:
[description]
objectAccess
-
obj -
property
Parameters:
-
objObjectObject to retrieve nested property from
-
propertyStringProperty string to access
Returns:
returns the nested property or undefined
objectFilter
-
object -
iterator
this does something....
Parameters:
-
objectObjectdesc
-
iteratorFunctiondesc
Returns:
[description]
removeRules
-
key -
rules
Replaces all rules for an individual key in a model
Parameters:
-
keyObject | Stringdesc
-
rulesObject | Stringdesc
Returns:
[description]
restore
-
options
The restore method rewinds back to the last created store point.
Parameters:
-
optionsObjectdesc
Returns:
[description]
set
-
attributes -
[options]
The set method does everything outlined in Backbone.js library. It has been overridden to handle building inferred attributes that are outlined in the child class. Sometimes you may want another attribute to update another set in a model. In this case, you may override the set method. If you do, be sure to call super on set so that these processes are handled.
Parameters:
-
attributesObject | Stringrefer to Backbone.js
-
[options]Object | String optionalrefer to Backbone.js
Returns:
[description]
setRules
-
key -
rules
set rules to the model for an attribute
Parameters:
-
keyObject | Stringdesc
-
rulesObject | Stringdesc
Returns:
[description]
store
()
type
The store method sets a store point for the model in a stack. Use restore to rewind the model to previous store points. FIFO
Returns:
[description]
subAction
-
options
Call a subaction of a model. (i.e. if you have a document that has a copy action) The url should follow the pattern of resource/:id/:action (documents/2/copy)
var PatientModel = awa.mvc.Model.extend({ urlRoot: awa.url.BAREBONES + 'V1/patients', idAttribute: 'pat_id' });
var model = new PatientModel({pat_id: 75});
//the following will make a GET request to //[barebones_root]V1/patients/75/sync
model.subAction({ type: 'GET', action: 'sync', success: function(){
}
});
Parameters:
-
optionsObjectconfiguration object with various properties
-
actionStringname of action
-
typeStringhttp method to use (POST, GET, DELETE, etc)
-
successFunctiondesc
-
errorFunctiondesc
-
dataObjectdesc
-
Returns:
Avoid using this as it is subject to change.
sync
-
method -
model -
options
Overridden from Backbone.Model.fetch to handle not so restful backend.
Parameters:
-
methodObjectconfiguration object with various properties
-
modelObjectdesc
-
optionsObjectdesc
Returns:
result of api_sync
toDisplayJSON
()
type
Uses the displayRules/Methods that are set in either the displayRules property or the attributeProps to generate a javascript object (w/o getters and setters) that represents the model's data in a displayable format.
Returns:
[description]
toJSON
-
[options]
Overwritten to provide more control on the resulting json output
Parameters:
-
[options]Object optionalOptions object
-
[skipKeys]Array optionalSet of keys to skip.
-
[sync]String optionalIf calling for syncing purposes. see awa.mvc.Model.SYNC
-
Returns:
[description]
toString
()
type
Returns:
[description]
url
()
String
stolen from backbone and altered to not throw exception in case of no urlRoot
Returns:
URL for model instance
validate
-
attributes -
[options]
The validate method does everything outlined in Backbone.js library. It has been overridden to handle the rules set in the attributeProps and the rules property. In some cases, you may want to do extensive validating on attributes that dont fit the model rules functions (i.e. making sure a date of birth is not in the future). Be sure to call the super version of validate to handle the other rules. !!!PLEASE NOTE!!! This method will evaluate all rules if you don't pass in attributes but if you do, then it only validates the ones you pass in. Keep this in mind when overriding this method. passing in a small set of attributes will cause all of the unset variables to be ignored.
Parameters:
-
attributesObject | Stringrefer to Backbone.js
-
[options]Object | String optionalrefer to Backbone.js
Returns:
[description]
Properties
__attributePropsProcessed
Boolean
just leave it alone.
Default: false
_stores
Array
Used by the model object to set and access store points.
Default: empty
api
String
Specifies what backend protocol to use to communicate with the server. 'api' will use the old standard of adding the method in the URL. 'barebones' will accomodate barebones
Default: api
className
String
Specifies what backend protocol to use to communicate with the server. 'api' will use the old standard of adding the method in the URL. 'barebones' will accomodate barebones
Default: Model
disabledAttribute
String
Specifies the disabled attribute in the model
Default: undefined
displayRules
Object keys: attribute/field names,
value: name of the function outline in awa.func
that will be called to format the data for output.
Rule set for validation. Used by Form validators.
Default: undefined
inferred
Object keys: attribute/field names, value: Objects
that specify the rules to follow. Used in conjuction with model rule functions
Any attributes that are built with primary data goes here. i.e. a patients full name is derived from the combination of it's first, middle, and last name and the suffix
Default: undefined
name
String
Override to return the name of the model (i.e. for the patient model, it would be Patient).
Default: undefined
pluralName
String
Override to return the plural name of the model. You should only use this if the plural does not just append a 's' (i.e. for the Diagnosis model, it would be Diagnoses).
Default: undefined
rules
Object keys: attribute/field names, value: Objects
that specify the rules to follow. Used in conjuction with jquery forms.
Rule set for validation. Used by Form validators.
Default: undefined
syncBlackList
Array
Lists the attributes that will not be synced back. doesn't prevent anything from being sent if empty or undefined. Given priority of over syncWhiteList
Default: undefined
syncWhiteList
Array
Lists the attributes that can be synced back. sends back all attributes if the list is empty or undefined
Default: undefined
Events
rules
Called when rules change on an individual model