npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

redux-marionette

v1.3.3

Published

Connect Redux and Marionette

Downloads

29

Readme

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