wwl-js-backbone-extensions
Current Version | Master | Develop |
---|---|---|
Abstract model
Usage
'wwl-js-backbone-extensions'AbstractModel rootName: 'article' # default: 'data' # You can combine them: jsonOmitted: 'comments'# default: [] jsonPermitted: 'id''title''rating'# default: [] syncWithoutRoot: false # default: false binaryId: true # default: false # You need to implement this : -> @getOption'context'getSecureRandomsecureHexRandom128 # You need to implement this : @contextgetUrlHelpersprependUrlHosturl
context = yourcontextheremodel = context: context
Events
modelon 'before:save' #... modelon 'beforeSync' #... modelon 'remoteErrors:[...backbone collection events...]'#... modelon 'localErrors:[...backbone collection events...]'#... modelon 'errors:[...backbone collection events for both collections combined...]'#... modelon 'dirty:change' # ...
Dirty
Dirty requires the server to send updated_at
as part of the response. If updated_at
changes, the model will be unmarked as dirty.
modelisDirty # e.g. false modelset'name''test'modelisDirty # e.g. true modelsavemodelisDirty # e.g. false modelisDirty # e.g. false modelsetDirtymodelisDirty # e.g. true modelsetDirtyfalsemodelisDirty # e.g. false
Persistance
# If it's synced at least once modelisSynced # If it's currently syncing modelisSyncing # If it's a new model modelisNew # Save the model modelsave
Validations
# An errors collection for local errors (you need to maintain it by yourself) modelgetLocalErrors # An errors collection build by the errors-attribute from the server response. modelgetRemoteErrors # An array of the errors concatenated from `getLocalErrors()` and `getRemoteErrors()` modelgetErrors # If there are local and/or remote errors. # It calls (backbone feature) the validate function if defined. So you could run your # local validations there (http://backbonejs.org/#Model-isValid). modelisValid # If there are local errors modelisLocalValid # If there are remote errors modelisRemoteValid # You can run the is...Valid function with one attribute key too, to check just that one. modelisValid'title'modelisLocalValid'title'modelisRemoteValid'title'
Helpers
# Returns the models sync root name - "data" by default modelgetRootName # Returns the meta data sent by the server modelgetMeta # Unset all attributes except of the id. # You can pass { discardId: true } to unset id too. modelunsetAll
Abstract collection
'wwl-js-backbone-extensions'AbstractCollection rootName: 'article' # default: 'data' syncWithoutRoot: false # default: false model: Article url: '/articles' # You need to implement this : @contextgetUrlHelpersprependUrlHosturl
context = yourcontextherecollection = context: context
Persistance
# If it's synced at least once collectionisSynced # If it's currently syncing collectionisSyncing
Helpers
# If there is already the model for the passed id inside the collection, it will return it # without fetching it - except you're passing true for fetch. # Attributes will be set to the model result = collectiongetOrFetchidattributes = fetch = falseresultmodel # the model instance resultjqxhr # *optional* - the jqxhr request if available # Returns the model instance (builds it if necessary) model = collectiongetOrInitializeidattributes = options = # Returns the models sync root name - "data" by default collectiongetRootName # Returns the meta data sent by the server collectiongetMeta