Code Monkey home page Code Monkey logo

redux-marionette's Introduction

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;
	},
});

redux-marionette's People

Contributors

stutrek avatar andrewhenderson avatar

Watchers

Hala Dajani avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.