Version 1
[yaffs-website] / web / core / modules / quickedit / js / models / BaseModel.js
1 /**
2  * @file
3  * A Backbone Model subclass that enforces validation when calling set().
4  */
5
6 (function (Drupal, Backbone) {
7
8   'use strict';
9
10   Drupal.quickedit.BaseModel = Backbone.Model.extend(/** @lends Drupal.quickedit.BaseModel# */{
11
12     /**
13      * @constructs
14      *
15      * @augments Backbone.Model
16      *
17      * @param {object} options
18      *   Options for the base model-
19      *
20      * @return {Drupal.quickedit.BaseModel}
21      *   A quickedit base model.
22      */
23     initialize: function (options) {
24       this.__initialized = true;
25       return Backbone.Model.prototype.initialize.call(this, options);
26     },
27
28     /**
29      * Set a value on the model
30      *
31      * @param {object|string} key
32      *   The key to set a value for.
33      * @param {*} val
34      *   The value to set.
35      * @param {object} [options]
36      *   Options for the model.
37      *
38      * @return {*}
39      *   The result of `Backbone.Model.prototype.set` with the specified
40      *   parameters.
41      */
42     set: function (key, val, options) {
43       if (this.__initialized) {
44         // Deal with both the "key", value and {key:value}-style arguments.
45         if (typeof key === 'object') {
46           key.validate = true;
47         }
48         else {
49           if (!options) {
50             options = {};
51           }
52           options.validate = true;
53         }
54       }
55       return Backbone.Model.prototype.set.call(this, key, val, options);
56     }
57
58   });
59
60 }(Drupal, Backbone));