1.3.3 • Published 7 years ago

redux-marionette v1.3.3

Weekly downloads
11
License
ISC
Repository
github
Last release
7 years ago

Redux-Marionette

This is a simple connection between Marionette and redux made to ease a transition to redux. It could be used to bring elements of the one way data flow to Marionette but the mess you end up with will be yours and yours alone.

It consists of two parts: Views and Components that let you put React in Marionette and vice versa, and model/dispatch based connections to keep your models in sync.

See it in action

The best way to learn to connect your models may be the tutorial PR.

Putting Marionette inside React Components

A Component is provided that can contain a Marionette view. It will start the View when the Component is rendered and destroy it when the Component is unmounted. There is also a convenience function that can take a view and return a component.

The Convenience Function

This will wrap a view and rerenders it when the values in this.props change.

import { wrapView } from 'redux-marionette';

import ViewToWrap from 'somewhere/in/your/old/app';

function convertOptionsToProps (props) {
	return props;
}

const wrappedView = wrapView(ViewToWrap, convertOptionsToProps);

note: it will destroy and recreate the Marionette view, it does not do a simple rerender

Using The Component Directly

By using the component directly you can call methods on this.marionetteComponent when this.props change.

import { MarionetteComponent } from 'redux-marionette'

export default class WrappedView extends MarionetteComponent {

	componentWillReceiveProps (newProps) {
		if (newProps.thingy !== this.props.thingy) {
			this.marionetteComponent.toggleThingy();
		}
	}

	createMarionetteComponent (props) {
		var options = {
			// stuff from props
		};
		return new MarionetteView(options);
	}
}

Putting React inside Marionette Views

Doing this requires that the view be connected to your store. Because this would be importing something (your store) from your app I haven't included it in this package. You can find an example here.

Connecting Marionette to Redux

Add this to your store:

import {marionetteMiddleware, marionetteDispatch} from 'redux-marionette';

export default function configureStore(initialState) {
  const store = compose(
    applyMiddleware(marionetteMiddleware(Backbone, Marionette, _))
  )(createStore)(rootReducer, initialState)
  
  marionetteDispatch(store.dispatch, Backbone, Marionette, _);

  return store
}

note: you have to pass in Backbone, Marionette, and underscore becase we're not going to judge your old architecture.

Dispatching actions from Marionette

Redux-Marionette binds to the lifecycle of your views, so any model or collection attached to a view will transparently be attached to your reducer and dispatcher then disconnected when the view is removed.

Directly On The App Object

var MyApp = Marionette.Application.extend({
	handleAction: function (action) {
		// act on global actions
	}
});

myApp.dispatch({
	type: 'MY_ACTION',
	meta: {
		foo: 'bar'
	}
});

note: you may also assign app.handleAction after your app has been created.

Connecting Specific Backbone Models or Collections

// this will work exactly the same way for Collections
var MyModel = Backbone.Model.extend({

	initialize: function () {
		// if you're overriding initialize, you must do this.
		Backbone.Model.prototype.initialize.call(this);
	},

	// this will be called on every Redux action
	handleAction: function (action) {
		if (action.id !== this.id) {
			return;
		}

		switch (action.type) {
			case 'VALUE_CHANGE':
				this.set('value', action.value);
				return;
		}
	},
	
	// this will be called on every event this model triggers
	createAction: function (eventName) {
		var action = this.toJSON();
		switch (eventName) {
			case 'change:value':
				action.type = 'VALUE_CHANGE';
				return action;
		}

		return;
	},
});
1.3.3

7 years ago

1.3.2

7 years ago

1.3.1

8 years ago

1.2.1

8 years ago

1.2.0

9 years ago

1.1.0

9 years ago

1.0.2

9 years ago

1.0.1

9 years ago

1.0.0

9 years ago