var self = this;
this.model.save({
success: function() {
self.callMethod();
},
});
this.model.save({
success: () => this.callMethod(),
});
Arrow Functions
define([
'app',
'dependency1',
'dependency2', // Notice not used
'template1',
], function (app, dependency1, dependency2, template1) {
//...
dependency1.callMethod();
app.vent.trigger('myEvent');
// render template1
//...
return new MyController();
});
import app from 'app';
import dependency1 from 'dependency1';
import dependency2 from 'dependency2', // Throws an eslint error now
import template1 from 'template1';
//...
dependency1.callMethod();
app.vent.trigger('myEvent');
// render template1
//...
export default new MyController();
Import/Export
const MyController = Marionette.Object.extend({
show: function (region) {
const model = new MyModel();
const view = new MyView({ model: model });
region.show(view);
},
});
const MyController = Marionette.Object.extend({
show(region) {
const model = new MyModel();
const view = new MyView({ model });
region.show(view);
},
});
Object Shorthand Notation
Classes
class MyController extends Marionette.Object {
show(region) {
const model = new MyModel();
const view = new MyView({ model });
region.show(view);
}
}
const MyController = Marionette.Object.extend({
show: function (region) {
const model = new MyModel();
const view = new MyView({ model: model });
region.show(view);
},
});
Other Resources
// MyModel.js
const MyModel = // ...
export default MyModel;
// MyView.js
const MyView = // ...
export default MyView;
// MyController.js
const MyController = //...
export default MyController;
// Direct access to all exports
// MyFeature.js
const MyModel = // ...
const MyView = // ...
const MyController = //...
return new MyController();
// No direct access to MyModel or MyView
Testability
Customization
// core/MyModel.js
const MyModel = Backbone.Model.extend({
parse(res) {
return res.data;
},
});
export default MyModel;
// custom/MyModel.js
import MyModel from 'MyModel';
const MyCustomModel = MyModel.extend({
parse(res) {
return res;
},
});
export default MyCustomModel;
More about how to do this later
How to extend a baseline component
// source/Agent.Shared.Core/content/scripts/app/core/BaseModule.js
class BaseModule {
show(region) {
const view = new MyView();
region.show(view);
}
}
export default BaseModule;
// source/custom/content/scripts/app/core/BaseModule.js
import BaseModule from 'override/core/BaseModule';
class CustomModule extends BaseModule {
show(region) {
// Don't have to call this
super.show(region);
// do more here
}
}
export default CustomModule;
How to replace a baseline component
// source/Agent.Shared.Core/content/scripts/app/core/BaseModule.js
class BaseModule {
show(region) {
const view = new MyView();
region.show(view);
}
}
export default BaseModule;
// source/custom/content/scripts/app/core/BaseModule.js
class CustomModule {
show(region) {
// do what you want
}
}
export default CustomModule;
How to extend a baseline view
// source/Agent.Shared.Core/content/scripts/app/core/BaseView.js
const BaseView = Marionette.ItemView.extend({
template: baseTemplate,
events: {
'click .element': 'execute',
},
onRender() {
// stuff
},
execute(e) {
// stuff
},
});
export default BaseView;
// source/custom/content/scripts/app/core/BaseView.js
import BaseView from 'override/core/BaseView';
const CustomView = BaseView.extend({
events: _.extend({}, BaseView.prototype.events, {
'click .another-element': 'executeElse',
},
onRender() {
BaseView.prototype.onRender.apply(this, arguments);
// stuff
},
executeElse(e) {
// stuff
},
});
export default CustomView;
Common themes in new code
showTabs() {
const tabSettings = this.generateTabs();
this.tabComponent.show(region, tabSettings.flatten());
}
generateTabs() {
const tabs = new OrderedMap();
tabs.add('tabOne', { /* settings */ });
tabs.add('tabTwo', { /* settings */ });
return tabs;
}
generateTabs() {
const tabs = Base.prototype.generateTabs.apply(this, arguments);
tabs.add('tabThree', { /* settings */ });
tabs.remove('tabOne');
tabs.insert('anotherTab', { /* settings */ }, 1);
return tabs;
}