MnObject
incorporates backbone conventions initialize
, cid
and extend
. MnObject
includes:
When instantiating a MnObject
there are several properties, if passed, that will be attached directly to the instance: channelName
, radioEvents
, radioRequests
import { MnObject } from 'backbone.marionette';
const myObject = new MnObject({ ... });
The cid
or client id is a unique identifier automatically assigned to MnObjects when they're first created and by default is prefixed with mno
. You can modify the prefix for MnObject
s you extend
by setting the cidPrefix
.
import { MnObject } from 'backbone.marionette';
const MyFoo = MnObject.extend({
cidPrefix: 'foo'
});
const foo = new MyFoo();
console.log(foo.cid); // foo1234
destroy
MnObjects have a destroy
method that unbind the events that are directly attached to the instance. destroy
returns the MnObject.
Invoking the destroy
method will trigger before:destroy
and destroy
events and their corresponding methods.
Note The event handlers will pass the options
argument destroy
was invoked with.
import { MnObject } from 'backbone.marionette';
// define a mnobject with an onBeforeDestroy method
const MyObject = MnObject.extend({
onBeforeDestroy(options){
// put other custom clean-up code here
}
});
// create a new mnobject instance
const obj = new MyObject();
// add some event handlers
obj.on('before:destroy', function(options){ ... });
obj.listenTo(something, 'bar', function(){...});
// destroy the mnobject: unbind all of the
// event handlers, trigger the "destroy" event and
// call the onBeforeDestroy method
obj.destroy({ foo: 'bar' });
isDestroyed
This method will return a boolean indicating if the mnobject has been destroyed.
import { MnObject } from 'backbone.marionette';
const obj = new MnObject();
obj.isDestroyed(); // false
obj.destroy();
obj.isDestroyed(); // true
Selections is a simple MnObject that manages a selection of things. Because Selections extends from MnObject, it gets initialize
and Events
for free.
import { MnObject } from 'backbone.marionette';
const Selections = MnObject.extend({
initialize(options){
this.selections = {};
},
select(key, item){
this.selections[key] = item;
this.triggerMethod('select', key, item);
},
deselect(key, item) {
delete this.selections[key];
this.triggerMethod('deselect', key, item);
}
});
const selections = new Selections({
filters: Filters
});
// use the built in EventBinder
selections.listenTo(selections, 'select', function(key, item){
console.log(item);
});
selections.select('toy', Truck);
In versions previous to v4, MnObject
was simply named Object
. This naming is still supported via the default export of the library, but should be considered deprecated and is scheduled for removal in an upcoming version.
import { MnObject } from 'backbone.marionette';
import Marionette from 'backbone.marionette';
console.log(MnObject === Marionette.Object === Marionette.MnObject); // true
© 2017 Muted Solutions, LLC
Licensed under the MIT License.
https://marionettejs.com/docs/v4.0.0/marionette.mnobject.html