Git Product home page Git Product logo

mobx-event's Introduction

Mobx Event

Usage

1 - Create a event instance

const MobxEvent = require('mobx-event');
const CartEvent = new MobxEvent({ products: [], total: 0 });

2 - Listen state changes

// get state changes
CartEvent.on('change', newState => console.log(newState));
// or only changed attribute:
CartEvent.on('change-total', total => console.log(total));

3 - Manipulate state

CartEvent.state.products.push({ name: 'TV LED', price: 1200 }); 
// { products: [{ name: 'TV LED', price: 1200 }] }

Methods

instance.rollback( times: int, default = 1 )

Rollback to last state.

Return: undefined


instance.history()

Return a history array;

Return: array


Using with marko

1 - Export event instance. Ex: gallery.event.js

const MobxEvent = require('mobx-event');
module.exports = new MobxEvent({ photos:  [], mode: 'grid' });

2 - Create component to subscribe event

// gallery/index.marko
import GalleryEvent from  './gallery.event.js'

class {
	onMount() {
		this
			.subscribeTo(GalleryEvent)
			.on('change', () => { this.forceUpdate(); });
}

<div>
	<div class=GalleryEvent.getState().mode>
		<img for(photo in GalleryEvent.state.photos) src=photo.src title=photo.title />
	</div>
	<photo-mode ...GalleryEvent.getState() />
</div>

3 - Child component receive state from Event

// gallery/components/photo-mode/index.marko
import GalleryEvent from  '../../gallery.event.js'

class {
	changeMode(mode) {
		GalleryEvent.state.mode = mode;
	}
}

<div>
	<button on-click('changeMode', 'grid') class={ active: input.mode === 'grid' }>Grid</button>
	<button on-click('changeMode', 'table') class={ active: input.mode === 'table' }>Table</button>	
</div>

Advanced

Extending

const MobxEvent = require('mobx-event');

class TeamEvent extends MobxEvent {
	constructor(...args) {
		super(...args);
	}
	addMember(member) {
		this.state.members.push(member);
	}
}
module.exports = TeamEvent;

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.