-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackbone.viewevents.js
129 lines (116 loc) · 3.85 KB
/
backbone.viewevents.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
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
// Generated by CoffeeScript 1.6.2
var __slice = [].slice,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
return define(['backbone', 'underscore'], function(Backbone, _) {
return root.Backbone.ViewEvents = factory(Backbone, _);
});
} else if (typeof require === 'function' && ((typeof module !== "undefined" && module !== null ? module.exports : void 0) != null)) {
return module.exports = factory(require('backbone'), require('underscore'));
} else {
return root.Backbone.ViewEvents = factory(root.Backbone, root._);
}
})(this, function(Backbone, _arg) {
var View, ViewEvents, eventSplitter, extend, mangleEventName, uniqueId, _ref;
extend = _arg.extend, uniqueId = _arg.uniqueId;
eventSplitter = /\s+/;
mangleEventName = function(name, context) {
var ctxNs, names;
ctxNs = context != null ? (!context._ctxId ? context._ctxId = uniqueId('ctxId') : void 0, '.' + context._ctxId) : '';
name = name.trim();
if (eventSplitter.test(name)) {
name.split(eventSplitter);
names = (function() {
var _i, _len, _results;
_results = [];
for (_i = 0, _len = names.length; _i < _len; _i++) {
name = names[_i];
_results.push("viewevent:" + name + ".viewevent" + ctxNs);
}
return _results;
})();
return names.join(" ");
} else {
return "viewevent:" + name + ".viewevent" + ctxNs;
}
};
ViewEvents = {
on: function(name, callback, context) {
var c, n,
_this = this;
if (typeof name === 'object') {
for (n in name) {
c = name[n];
this.on(n, c, callback);
}
} else {
this.$el.on(mangleEventName(name, context), function() {
var args, e;
e = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
return callback.apply(context, args);
});
}
return this;
},
once: function(name, callback, context) {
var c, n,
_this = this;
if (typeof name === 'object') {
for (n in name) {
c = name[n];
this.once(n, c, callback);
}
} else {
this.$el.one(mangleEventName(name, context), function() {
var args, e;
e = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
return callback.apply(context, args);
});
}
return this;
},
off: function(name, callback, context) {
var c, n;
if (!name && !callback) {
if ((context != null ? context._ctxId : void 0) != null) {
this.$el.off('.' + context._ctxId);
} else {
this.$el.off('.viewevent');
}
} else if (typeof name === 'object') {
for (n in name) {
c = name[n];
this.off(n, c, callback);
}
} else {
this.$el.off(mangleEventName(name, context), callback);
}
return this;
},
trigger: function() {
var args, name;
name = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
args.push({
view: this,
type: name
});
this.$el.trigger(mangleEventName(name), args);
return this;
}
};
View = (function(_super) {
__extends(View, _super);
function View() {
_ref = View.__super__.constructor.apply(this, arguments);
return _ref;
}
return View;
})(Backbone.View);
extend(View.prototype, ViewEvents);
return {
View: View,
ViewEvents: ViewEvents
};
});