Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v5 Migration #3679

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
  • Loading branch information
paulfalgout committed Nov 13, 2020
commit e782b4a4fc48ee27e090fa108062cb25dd359a60
1,216 changes: 876 additions & 340 deletions lib/backbone.marionette.esm.js

Large diffs are not rendered by default.

57 changes: 16 additions & 41 deletions src/behavior.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
// Behaviors allow you to blackbox View specific interactions
// into portable logical chunks, keeping your views simple and your code DRY.

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

const ClassOptions = [
'collectionEvents',
Expand All @@ -29,8 +28,12 @@ const Behavior = function(options, view) {
// to the view.
this.view = view;


this._setOptions(options, ClassOptions);
this.cid = _.uniqueId(this.cidPrefix);
this.cid = uniqueId(this.cidPrefix);

this._initViewEvents();
this.setElement();

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

// Proxy view triggers
this.listenTo(view, 'all', this.triggerMethod);
Expand All @@ -52,12 +55,9 @@ Behavior.extend = extend;
// Behavior Methods
// --------------

_.extend(Behavior.prototype, CommonMixin, DelegateEntityEventsMixin, TriggersMixin, UIMixin, {
_extend(Behavior.prototype, CommonMixin, DelegateEntityEventsMixin, UIMixin, ViewEventsMixin, {
cidPrefix: 'mnb',

// This is a noop method intended to be overridden
initialize() {},

// proxy behavior $ method to the view
// this is useful for doing jquery DOM lookups
// scoped to behaviors view.
Expand All @@ -67,6 +67,8 @@ _.extend(Behavior.prototype, CommonMixin, DelegateEntityEventsMixin, TriggersMix

// Stops the behavior from listening to events.
destroy() {
this._undelegateViewEvents();

this.stopListening();

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

proxyViewProperties() {
this.$el = this.view.$el;
setElement() {
this._undelegateViewEvents();

this.el = this.view.el;

this._delegateViewEvents(this.view);

return this;
},

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

return this;
},

_getEvents() {
if (!this.events) { return; }

// Normalize behavior events hash to allow
// a user to use the @ui. syntax.
const behaviorEvents = this.normalizeUIKeys(_.result(this, 'events'));

// binds the handler to the behavior and builds a unique eventName
return _.reduce(behaviorEvents, (events, behaviorHandler, key) => {
if (!_.isFunction(behaviorHandler)) {
behaviorHandler = this[behaviorHandler];
}
if (!behaviorHandler) { return events; }
key = getNamespacedEventName(key, this.cid);
events[key] = behaviorHandler.bind(this);
return events;
}, {});
},

// Internal method to build all trigger handlers for a given behavior
_getTriggers() {
if (!this.triggers) { return; }

// Normalize behavior triggers hash to allow
// a user to use the @ui. syntax.
const behaviorTriggers = this.normalizeUIKeys(_.result(this, 'triggers'));

return this._getViewTriggers(this.view, behaviorTriggers);
}
});

Expand Down
Loading