Skip to content

Instantly share code, notes, and snippets.

@jhollingworth
Last active August 29, 2015 14:19
Show Gist options
  • Save jhollingworth/e2d1916af034449e1114 to your computer and use it in GitHub Desktop.
Save jhollingworth/e2d1916af034449e1114 to your computer and use it in GitHub Desktop.
var UserStore = require('./stores/userStore');
var UserActions = require('./actions/userActions');
class Application extends Marty.Application {
constructor() {
super();
this.userStores = new UserStore({
app: this,
foo: "bar"
});
this.userActions = new UserActions({
app: this
});
//Possible convinience methods:
// Register could take an object hash where each value is a function that is
// invoked with the application (e.g. could be an class or a factory)
this.register({
userQueries: require('./queries/userQueries'),
otherQueries: options => new OtherQueries(options)
});
// Or it could accept a function that returns a set of instances?
this.register(app => {
return {
something: new Something({ app: app }),
otherThing: new OtherThing({ app: app })
};
})
}
}
var app = new Application();
// Creates an instance of dispatcher
app.dispatcher.register(console.log.bind(console));
// All types are available immediately
app.userStores.getUser();
app.userActions.deleteUser(123);
app.userQueries.getUser();
// bindTo is just adding the application instance to the context
var Home = app.bindTo(require('./views/home'), {
name: 'app' // Can call app whatever you want
});
React.render(<Home />);
class Home extends React.Component {
constructor(props, context) {
this.userActions = context.app.userActions;
}
deleteUser() {
this.userActions.deleteUser();
}
}
// Convenience method for { app: React.PropTypes.instanceOf(Marty.Application) }
Home.contextTypes = Marty.contextTypes;
class UserStore extends Marty.Store {
getUser() {
return this.fetch({
remotely() {
return this.app.userQueries.getUser()
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment