2 var CRM
= (window
.CRM
) ? (window
.CRM
) : (window
.CRM
= {});
3 if (!CRM
.Backbone
) CRM
.Backbone
= {};
6 * Backbone.sync provider which uses CRM.api() for I/O.
7 * To support CRUD operations, model classes must be defined with a "crmEntityName" property.
8 * To load collections using API queries, set the "crmCriteria" property or override the
9 * method "toCrmCriteria".
11 * @param method Accepts normal Backbone.sync methods; also accepts "crm-replace"
14 * @see tests/qunit/crm-backbone
16 CRM
.Backbone
.sync = function(method
, model
, options
) {
17 var isCollection
= _
.isArray(model
.models
);
21 success: function(data
) {
23 options
.success(_
.toArray(data
.values
));
25 error: function(data
) {
26 // CRM.api displays errors by default, but Backbone.sync
27 // protocol requires us to override "error". This restores
28 // the default behavior.
29 $().crmError(data
.error_message
, ts('Error'));
35 CRM
.api(model
.crmEntityName
, 'get', model
.toCrmCriteria(), apiOptions
);
37 // replace all entities matching "x.crmCriteria" with new entities in "x.models"
39 var params
= this.toCrmCriteria();
41 params
.values
= this.toJSON();
42 CRM
.api(model
.crmEntityName
, 'replace', params
, apiOptions
);
45 apiOptions
.error({is_error
: 1, error_message
: "CRM.Backbone.sync(" + method
+ ") not implemented for collections"});
49 // callback options to pass to CRM.api
51 success: function(data
) {
53 var values
= _
.toArray(data
['values']);
54 if (data
.count
== 1) {
55 options
.success(values
[0]);
58 data
.error_message
= ts("Expected exactly one response");
59 apiOptions
.error(data
);
62 error: function(data
) {
63 // CRM.api displays errors by default, but Backbone.sync
64 // protocol requires us to override "error". This restores
65 // the default behavior.
66 $().crmError(data
.error_message
, ts('Error'));
71 case 'create': // pass-through
73 var params
= model
.toJSON();
74 params
.options
|| (params
.options
= {});
75 params
.options
.reload
= 1;
76 if (!model
._isDuplicate
) {
77 CRM
.api(model
.crmEntityName
, 'create', params
, apiOptions
);
79 CRM
.api(model
.crmEntityName
, 'duplicate', params
, apiOptions
);
84 var apiAction
= (method
== 'delete') ? 'delete' : 'get';
85 var params
= model
.toCrmCriteria();
87 apiOptions
.error({is_error
: 1, error_message
: 'Missing ID for ' + model
.crmEntityName
});
90 CRM
.api(model
.crmEntityName
, apiAction
, params
, apiOptions
);
93 apiOptions
.error({is_error
: 1, error_message
: "CRM.Backbone.sync(" + method
+ ") not implemented for models"});
99 * Connect a "model" class to CiviCRM's APIv3
103 * var ContactModel = Backbone.Model.extend({});
104 * CRM.Backbone.extendModel(ContactModel, "Contact");
107 * c = new ContactModel({id: 3});
111 * @param Class ModelClass
112 * @param string crmEntityName APIv3 entity name, such as "Contact" or "CustomField"
113 * @see tests/qunit/crm-backbone
115 CRM
.Backbone
.extendModel = function(ModelClass
, crmEntityName
) {
116 // Defaults - if specified in ModelClass, preserve
117 _
.defaults(ModelClass
.prototype, {
118 crmEntityName
: crmEntityName
,
119 toCrmCriteria: function() {
120 return (this.get('id')) ? {id
: this.get('id')} : {};
122 duplicate: function() {
123 var newModel
= new ModelClass(this.toJSON());
124 newModel
._isDuplicate
= true;
125 if (newModel
.setModified
) newModel
.setModified();
126 newModel
.listenTo(newModel
, 'sync', function(){
127 // may get called on subsequent resaves -- don't care!
128 delete newModel
._isDuplicate
;
133 // Overrides - if specified in ModelClass, replace
134 _
.extend(ModelClass
.prototype, {
135 sync
: CRM
.Backbone
.sync
140 * Configure a model class to track whether a model has unsaved changes.
143 * - setModified() - flag the model as modified/dirty
144 * - isSaved() - return true if there have been no changes to the data since the last fetch or save
146 * - saved(object model, bool is_saved) - triggered whenever isSaved() value would change
148 * Note: You should not directly call isSaved() within the context of the success/error/sync callback;
149 * I haven't found a way to make isSaved() behave correctly within these callbacks without patching
150 * Backbone. Instead, attach an event listener to the 'saved' event.
154 CRM
.Backbone
.trackSaved = function(ModelClass
) {
155 // Retain references to some of the original class's functions
156 var Parent
= _
.pick(ModelClass
.prototype, 'initialize', 'save', 'fetch');
159 var onSyncSuccess = function() {
160 this._modified
= false;
161 if (this._oldModified
.length
> 0) {
162 this._oldModified
.pop();
164 this.trigger('saved', this, this.isSaved());
166 var onSaveError = function() {
167 if (this._oldModified
.length
> 0) {
168 this._modified
= this._oldModified
.pop();
169 this.trigger('saved', this, this.isSaved());
173 // Defaults - if specified in ModelClass, preserve
174 _
.defaults(ModelClass
.prototype, {
175 isSaved: function() {
176 var result
= !this.isNew() && !this.isModified();
179 isModified: function() {
180 return this._modified
;
182 _saved_onchange: function(model
, options
) {
183 if (options
.parse
) return;
184 // console.log('change', model.changedAttributes(), model.previousAttributes());
187 setModified: function() {
188 var oldModified
= this._modified
;
189 this._modified
= true;
191 this.trigger('saved', this, this.isSaved());
196 // Overrides - if specified in ModelClass, replace
197 _
.extend(ModelClass
.prototype, {
198 initialize: function(options
) {
199 this._modified
= false;
200 this._oldModified
= [];
201 this.listenTo(this, 'change', this._saved_onchange
);
202 this.listenTo(this, 'error', onSaveError
);
203 this.listenTo(this, 'sync', onSyncSuccess
);
204 if (Parent
.initialize
) {
205 return Parent
.initialize
.apply(this, arguments
);
209 // we'll assume success
210 this._oldModified
.push(this._modified
);
211 return Parent
.save
.apply(this, arguments
);
214 this._oldModified
.push(this._modified
);
215 return Parent
.fetch
.apply(this, arguments
);
221 * Configure a model class to support client-side soft deletion.
222 * One can call "model.setDeleted(BOOLEAN)" to flag an entity for
223 * deletion (or not) -- however, deletion will be deferred until save()
227 * setSoftDeleted(boolean) - flag the model as deleted (or not-deleted)
228 * isSoftDeleted() - determine whether model has been soft-deleted
230 * softDelete(model, is_deleted) -- change value of is_deleted
234 CRM
.Backbone
.trackSoftDelete = function(ModelClass
) {
235 // Retain references to some of the original class's functions
236 var Parent
= _
.pick(ModelClass
.prototype, 'save');
238 // Defaults - if specified in ModelClass, preserve
239 _
.defaults(ModelClass
.prototype, {
240 is_soft_deleted
: false,
241 setSoftDeleted: function(is_deleted
) {
242 if (this.is_soft_deleted
!= is_deleted
) {
243 this.is_soft_deleted
= is_deleted
;
244 this.trigger('softDelete', this, is_deleted
);
245 if (this.setModified
) this.setModified(); // FIXME: ugly interaction, trackSoftDelete-trackSaved
248 isSoftDeleted: function() {
249 return this.is_soft_deleted
;
253 // Overrides - if specified in ModelClass, replace
254 _
.extend(ModelClass
.prototype, {
255 save: function(attributes
, options
) {
256 if (this.isSoftDeleted()) {
257 return this.destroy(options
);
259 return Parent
.save
.apply(this, arguments
);
266 * Connect a "collection" class to CiviCRM's APIv3
268 * Note: the collection supports a special property, crmCriteria, which is an array of
269 * query options to send to the API.
273 * var ContactModel = Backbone.Model.extend({});
274 * CRM.Backbone.extendModel(ContactModel, "Contact");
275 * var ContactCollection = Backbone.Collection.extend({
276 * model: ContactModel
278 * CRM.Backbone.extendCollection(ContactCollection);
280 * // Use class (with passive criteria)
281 * var c = new ContactCollection([], {
282 * crmCriteria: {contact_type: 'Organization'}
285 * c.get(123).set('property', 'value');
286 * c.get(456).setDeleted(true);
289 * // Use class (with active criteria)
290 * var criteriaModel = new SomeModel({
291 * contact_type: 'Organization'
293 * var c = new ContactCollection([], {
294 * crmCriteriaModel: criteriaModel
297 * c.get(123).set('property', 'value');
298 * c.get(456).setDeleted(true);
303 * @param Class CollectionClass
304 * @see tests/qunit/crm-backbone
306 CRM
.Backbone
.extendCollection = function(CollectionClass
) {
307 var origInit
= CollectionClass
.prototype.initialize
;
308 // Defaults - if specified in CollectionClass, preserve
309 _
.defaults(CollectionClass
.prototype, {
310 crmEntityName
: CollectionClass
.prototype.model
.prototype.crmEntityName
,
311 toCrmCriteria: function() {
312 return (this.crmCriteria
) ? _
.extend({}, this.crmCriteria
) : {};
316 * Get an object which represents this collection's criteria
317 * as a live model. Any changes to the model will be applied
318 * to the collection, and the collection will be refreshed.
320 * @param criteriaModelClass
322 setCriteriaModel: function(criteriaModel
) {
323 var collection
= this;
324 this.crmCriteria
= criteriaModel
.toJSON();
325 this.listenTo(criteriaModel
, 'change', function() {
326 collection
.crmCriteria
= criteriaModel
.toJSON();
327 collection
.debouncedFetch();
331 debouncedFetch
: _
.debounce(function() {
332 this.fetch({reset
: true});
336 * Reconcile the server's collection with the client's collection.
337 * New/modified items from the client will be saved/updated on the
338 * server. Deleted items from the client will be deleted on the
341 * @param Object options - accepts "success" and "error" callbacks
343 save: function(options
) {
344 options
|| (options
= {});
345 var collection
= this;
346 var success
= options
.success
;
347 options
.success = function(resp
) {
348 // Ensure attributes are restored during synchronous saves.
349 collection
.reset(resp
, options
);
350 if (success
) success(collection
, resp
, options
);
351 // collection.trigger('sync', collection, resp, options);
353 wrapError(collection
, options
);
355 return this.sync('crm-replace', this, options
)
358 // Overrides - if specified in CollectionClass, replace
359 _
.extend(CollectionClass
.prototype, {
360 sync
: CRM
.Backbone
.sync
,
361 initialize: function(models
, options
) {
362 options
|| (options
= {});
363 if (options
.crmCriteriaModel
) {
364 this.setCriteriaModel(options
.crmCriteriaModel
);
365 } else if (options
.crmCriteria
) {
366 this.crmCriteria
= options
.crmCriteria
;
369 return origInit
.apply(this, arguments
);
374 // filter models list, excluding any soft-deleted items
375 this.each(function(model
) {
376 // if model doesn't track soft-deletes
377 // or if model tracks soft-deletes and wasn't soft-deleted
378 if (!model
.isSoftDeleted
|| !model
.isSoftDeleted()) {
379 result
.push(model
.toJSON());
388 * Find a single record, or create a new record.
390 * @param Object options:
391 * - CollectionClass: class
392 * - crmCriteria: Object values to search/default on
393 * - defaults: Object values to put on newly created model (if needed)
394 * - success: function(model)
395 * - error: function(collection, error)
397 CRM
.Backbone
.findCreate = function(options
) {
398 options
|| (options
= {});
399 var collection
= new options
.CollectionClass([], {
400 crmCriteria
: options
.crmCriteria
403 success: function(collection
) {
404 if (collection
.length
== 0) {
405 var attrs
= _
.extend({}, collection
.crmCriteria
, options
.defaults
|| {});
406 var model
= collection
._prepareModel(attrs
, options
);
407 options
.success(model
);
408 } else if (collection
.length
== 1) {
409 options
.success(collection
.first());
411 options
.error(collection
, {
413 error_message
: 'Too many matches'
417 error: function(collection
, errorData
) {
419 options
.error(collection
, errorData
);
426 CRM
.Backbone
.Model
= Backbone
.Model
.extend({
428 * Return JSON version of model -- but only include fields that are
429 * listed in the 'schema'.
433 toStrictJSON: function() {
434 var schema
= this.schema
;
435 var result
= this.toJSON();
436 _
.each(result
, function(value
, key
) {
443 setRel: function(key
, value
, options
) {
444 this.rels
= this.rels
|| {};
445 if (this.rels
[key
] != value
) {
446 this.rels
[key
] = value
;
447 this.trigger("rel:" + key
, value
);
450 getRel: function(key
) {
451 return this.rels
? this.rels
[key
] : null;
455 CRM
.Backbone
.Collection
= Backbone
.Collection
.extend({
457 * Store 'key' on this.rel and automatically copy it to
462 * @param initialModels
464 initializeCopyToChildrenRelation: function(key
, value
, initialModels
) {
465 this.setRel(key
, value
, {silent
: true});
466 this.on('reset', this._copyToChildren
, this);
467 this.on('add', this._copyToChild
, this);
469 _copyToChildren: function() {
470 var collection
= this;
471 collection
.each(function(model
) {
472 collection
._copyToChild(model
);
475 _copyToChild: function(model
) {
476 _
.each(this.rels
, function(relValue
, relKey
) {
477 model
.setRel(relKey
, relValue
, {silent
: true});
480 setRel: function(key
, value
, options
) {
481 this.rels
= this.rels
|| {};
482 if (this.rels
[key
] != value
) {
483 this.rels
[key
] = value
;
484 this.trigger("rel:" + key
, value
);
487 getRel: function(key
) {
488 return this.rels
? this.rels
[key
] : null;
493 CRM.Backbone.Form = Backbone.Form.extend({
494 validate: function() {
495 // Add support for form-level validators
496 var errors = Backbone.Form.prototype.validate.apply(this, []) || {};
498 if (this.validators) {
499 _.each(this.validators, function(validator) {
500 var modelErrors = validator(this.getValue());
502 // The following if() has been copied-pasted from the parent's
503 // handling of model-validators. They are similar in that the errors are
504 // probably keyed by field names... but not necessarily, so we use _others
507 var isDictionary = _.isObject(modelErrors) && !_.isArray(modelErrors);
509 //If errors are not in object form then just store on the error object
511 errors._others = errors._others || [];
512 errors._others.push(modelErrors);
515 //Merge programmatic errors (requires model.validate() to return an object e.g. { fieldKey: 'error' })
517 _.each(modelErrors, function(val, key) {
518 //Set error on field if there isn't one already
519 if (self.fields[key] && !errors[key]) {
520 self.fields[key].setError(val);
525 //Otherwise add to '_others' key
526 errors._others = errors._others || [];
529 errors._others.push(tmpErr);
537 return _.isEmpty(errors) ? null : errors;
542 // Wrap an optional error callback with a fallback error event.
543 var wrapError = function (model
, options
) {
544 var error
= options
.error
;
545 options
.error = function(resp
) {
546 if (error
) error(model
, resp
, options
);
547 model
.trigger('error', model
, resp
, options
);