aui-archive2 /auiplugin/src/main/resources/experimental/js/atlassian/restfultable/restfultable.entrymodel.js

Language Javascript Lines 221
MD5 Hash ad29aed951eaed9d26d0d296e330488b
Repository https://bitbucket.org/shaffenden/aui-archive2.git View Raw File View Project SPDX
| Open JSFiddle
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
(function ($) {

    /**
     * A class provided to fill some gaps with the out of the box Backbone.Model class. Most notiably the inability
     * to send ONLY modified attributes back to the server.
     *
     * @class EntryModel
     * @namespace AJS.RestfulTable
     */
    AJS.RestfulTable.EntryModel = Backbone.Model.extend({

        /**
         * Overrides default save handler to only save (send to server) attributes that have changed.
         * Also provides some default error handling.
         *
         * @override
         * @param attributes
         * @param options
         */
        save: function (attributes, options) {


            options = options || {};

            var instance = this,
                Model,
                syncModel,
                error = options.error, // we override, so store original
                success = options.success;


            // override error handler to provide some defaults
            options.error = function (model, xhr) {

                var data = $.parseJSON(xhr.responseText || xhr.data);

                instance._serverErrorHandler(xhr);

                // call original error handler
                if (error) {
                    error.call(instance, instance, data, xhr);
                }
            };

            // if it is a new model, we don't have to worry about updating only changed attributes because they are all new
            if (this.isNew()) {

                // call super
                Backbone.Model.prototype.save.call(this, attributes, options);

            // only go to server if something has changed
            } else if (attributes) {

                // create temporary model
                Model = Backbone.Model.extend({
                    url: this.url()
                });

                syncModel = new Model({
                    id: this.id
                });

                options.success = function (model, xhr) {

                    // update original model with saved attributes
                    instance.clear().set(model.toJSON());

                    // call original success handler
                    if (success) {
                        success.call(instance, instance, xhr);
                    }
                };

                // update temporary model with the changed attributes
                syncModel.save(attributes, options);
            }
        },

        /**
         * Destroys the model on the server. We need to override the default method as it does not support sending of
         * query paramaters.
         *
         * @override
         * @param options
         * ... {function} success - Server success callback
         * ... {function} error - Server error callback
         * ... {object} data
         *
         * @return AJS.RestfulTable.EntryModel
         */
        destroy: function (options) {

            options = options || {};

            var instance = this,
                url = this.url(),
                data;

            if (options.data) {
                data = $.param(options.data);
            }

            if (data !== "") {
                // we need to add to the url as the data param does not work for jQuery DELETE requests
                url = url + "?" + data;
            }

            $.ajax({
                url: url,
                type: "DELETE",
                dataType: "json",
                success: function (data) {
                    if(instance.collection){
                        instance.collection.remove(instance);
                    }
                    if (options.success) {
                        options.success.call(instance, data);
                    }
                },
                error: function (xhr) {
                    instance._serverErrorHandler(xhr);
                    if (options.error) {
                        options.error.call(instance, xhr);
                    }
                }
            });

            return this;
        },


        /**
         * A more complex lookup for changed attributes then default backbone one.
         *
         * @param attributes
         */
        changedAttributes: function (attributes) {

            var changed = {},
                current = this.toJSON();

            $.each(attributes, function (name, value) {

                if (!current[name]) {
                    if (typeof value === "string") {
                        if ($.trim(value) !== "") {
                            changed[name] = value;
                        }
                    } else if ($.isArray(value)) {
                        if (value.length !== 0) {
                            changed[name] = value;
                        }
                    } else {
                        changed[name] = value;
                    }
                } else if (current[name] && current[name] !== value) {

                    if (typeof value === "object") {
                        if (!_.isEqual(value, current[name])) {
                            changed[name] = value;
                        }
                    } else {
                        changed[name] = value;
                    }
                }
            });

            if (!_.isEmpty(changed)) {
                this.addExpand(changed);
                return changed;
            }
        },

        /**
         * Useful point to override if you always want to add an expand to your rest calls.
         *
         * @param changed attributes that have already changed
         */
        addExpand: function (changed){},

        /**
         * Throws a server error event unless user input validation error (status 400)
         *
         * @param xhr
         */
        _serverErrorHandler: function (xhr) {
            var data;
            if (xhr.status !== 400) {
                data = $.parseJSON(xhr.responseText || xhr.data);
                AJS.triggerEvtForInst(AJS.RestfulTable.Events.SERVER_ERROR, this, [data, xhr]);
            }
        },

        /**
         * Fetches values, with some generic error handling
         *
         * @override
         * @param options
         */
        fetch: function (options) {

            options = options || {};

            var instance = this,
                error = options.error;

            this.clear(); // clear the model, so we do not merge the old with the new

            options.error = function (model, xhr) {
                instance._serverErrorHandler(xhr);
                if (error) {
                    error.apply(this, arguments);
                }
            };

            // call super
            Backbone.Model.prototype.fetch.call(this, options);
        }
    });

})(AJS.$);
Back to Top