This repository has been archived by the owner on Apr 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
65 lines (62 loc) · 2.1 KB
/
index.js
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
(function (root, factory) {
// AMD
if (typeof define === 'function' && define.amd) {
define(['backbone', 'mustache'], factory);
// CommonJS
} else if (typeof module === 'object' && module.exports && require) {
module.exports = factory(require('backbone'), require('mustache'));
// Globals
} else {
/* eslint-disable no-param-reassign */
root.Auth = factory(root.Backbone, root.Mustache);
/* eslint-enable no-param-reassign */
}
})(this, function (Backbone, Mustache) {
'use strict';
/**
* Authorization Backbone Model
*/
return Backbone.Model.extend({
// Method called when authorization model is initialized with new constructor
initialize: function () {
var self = this;
// define default mustache render at initialization
var defaultRender = Mustache.render;
// Monkey-patch Mustache render to include on all views the
// authorizations (_auth_)
/* eslint-disable no-param-reassign */
Mustache.render = function (template, view) {
if (!view) {
return defaultRender.apply(this, [template, {
_auth_: self.attributes
}]);
}
view._auth_ = self.attributes;
return defaultRender.apply(this, arguments);
};
/* eslint-enable no-param-reassign */
},
/**
* Overides default set of attributes to allow for setting an array of
* authorizations (strings) that will be parsed to an object of
* authorizations as key and true for value
*/
set: function () {
var args = Array.prototype.slice.call(arguments);
// checks if attributes are an array
if (args[0] instanceof Array) {
// clean up old authorizations
this.clear();
var data = {};
// build object from array
args[0].forEach(function (key) {
data[key] = true;
});
// overwrite attributes
args[0] = data;
}
// applies new arguments to default set function
Backbone.Model.prototype.set.apply(this, args);
}
});
});