Skip to content

Commit ae073ed

Browse files
committed
v5 Migration
DO NOT MERGE This PR serves as a migration guide for moving to https://github.com/marionettejs/marionette with the exception of Radio integration. But it should make relevent breaking changes more obvious
1 parent bf8a5ab commit ae073ed

22 files changed

+12021
-971
lines changed

lib/backbone.marionette.esm.js

Lines changed: 876 additions & 340 deletions
Large diffs are not rendered by default.

lib/backbone.marionette.js

Lines changed: 909 additions & 373 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/backbone.marionette.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/backbone.marionette.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/backbone.marionette.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 9508 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@
5757
},
5858
"github": "https://github.com/marionettejs/backbone.marionette",
5959
"dependencies": {
60-
"backbone.radio": "^2.0.0"
60+
"backbone.radio": "^2.0.0",
61+
"yarn": "^1.22.10"
6162
},
6263
"peerDependencies": {
6364
"backbone": "^1.3.3",

src/behavior.js

Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@
66
// Behaviors allow you to blackbox View specific interactions
77
// into portable logical chunks, keeping your views simple and your code DRY.
88

9-
import _ from 'underscore';
9+
import { extend as _extend, uniqueId, result } from 'underscore';
1010
import extend from './utils/extend';
11-
import getNamespacedEventName from './utils/get-namespaced-event-name';
1211
import CommonMixin from './mixins/common';
1312
import DelegateEntityEventsMixin from './mixins/delegate-entity-events';
14-
import TriggersMixin from './mixins/triggers';
1513
import UIMixin from './mixins/ui';
14+
import ViewEventsMixin from './mixins/view-events';
1615

1716
const ClassOptions = [
1817
'collectionEvents',
@@ -29,8 +28,12 @@ const Behavior = function(options, view) {
2928
// to the view.
3029
this.view = view;
3130

31+
3232
this._setOptions(options, ClassOptions);
33-
this.cid = _.uniqueId(this.cidPrefix);
33+
this.cid = uniqueId(this.cidPrefix);
34+
35+
this._initViewEvents();
36+
this.setElement();
3437

3538
// Construct an internal UI hash using the behaviors UI
3639
// hash combined and overridden by the view UI hash.
@@ -39,7 +42,7 @@ const Behavior = function(options, view) {
3942
// This order will help the reuse and share of a behavior
4043
// between multiple views, while letting a view override
4144
// a selector under an UI key.
42-
this.ui = _.extend({}, _.result(this, 'ui'), _.result(view, 'ui'));
45+
this.ui = _extend({}, result(this, 'ui'), result(view, 'ui'));
4346

4447
// Proxy view triggers
4548
this.listenTo(view, 'all', this.triggerMethod);
@@ -52,12 +55,9 @@ Behavior.extend = extend;
5255
// Behavior Methods
5356
// --------------
5457

55-
_.extend(Behavior.prototype, CommonMixin, DelegateEntityEventsMixin, TriggersMixin, UIMixin, {
58+
_extend(Behavior.prototype, CommonMixin, DelegateEntityEventsMixin, UIMixin, ViewEventsMixin, {
5659
cidPrefix: 'mnb',
5760

58-
// This is a noop method intended to be overridden
59-
initialize() {},
60-
6161
// proxy behavior $ method to the view
6262
// this is useful for doing jquery DOM lookups
6363
// scoped to behaviors view.
@@ -67,6 +67,8 @@ _.extend(Behavior.prototype, CommonMixin, DelegateEntityEventsMixin, TriggersMix
6767

6868
// Stops the behavior from listening to events.
6969
destroy() {
70+
this._undelegateViewEvents();
71+
7072
this.stopListening();
7173

7274
this.view._removeBehavior(this);
@@ -76,10 +78,13 @@ _.extend(Behavior.prototype, CommonMixin, DelegateEntityEventsMixin, TriggersMix
7678
return this;
7779
},
7880

79-
proxyViewProperties() {
80-
this.$el = this.view.$el;
81+
setElement() {
82+
this._undelegateViewEvents();
83+
8184
this.el = this.view.el;
8285

86+
this._delegateViewEvents(this.view);
87+
8388
return this;
8489
},
8590

@@ -110,36 +115,6 @@ _.extend(Behavior.prototype, CommonMixin, DelegateEntityEventsMixin, TriggersMix
110115
this._undelegateEntityEvents(this.view.model, this.view.collection);
111116

112117
return this;
113-
},
114-
115-
_getEvents() {
116-
if (!this.events) { return; }
117-
118-
// Normalize behavior events hash to allow
119-
// a user to use the @ui. syntax.
120-
const behaviorEvents = this.normalizeUIKeys(_.result(this, 'events'));
121-
122-
// binds the handler to the behavior and builds a unique eventName
123-
return _.reduce(behaviorEvents, (events, behaviorHandler, key) => {
124-
if (!_.isFunction(behaviorHandler)) {
125-
behaviorHandler = this[behaviorHandler];
126-
}
127-
if (!behaviorHandler) { return events; }
128-
key = getNamespacedEventName(key, this.cid);
129-
events[key] = behaviorHandler.bind(this);
130-
return events;
131-
}, {});
132-
},
133-
134-
// Internal method to build all trigger handlers for a given behavior
135-
_getTriggers() {
136-
if (!this.triggers) { return; }
137-
138-
// Normalize behavior triggers hash to allow
139-
// a user to use the @ui. syntax.
140-
const behaviorTriggers = this.normalizeUIKeys(_.result(this, 'triggers'));
141-
142-
return this._getViewTriggers(this.view, behaviorTriggers);
143118
}
144119
});
145120

0 commit comments

Comments
 (0)