Skip to content

Instantly share code, notes, and snippets.

@gaearon
gaearon / BaseModel.js
Last active December 23, 2015 20:29
A base Backbone model that supports nested models/collections with simple syntax. See http://stackoverflow.com/a/18989596/458193
// First, define `nestedTypes` in your model.
// Each nested type may be a Model or Collection subtype.
window.app.viewer.Model.GallerySection = window.app.Model.BaseModel.extend({
nestedTypes: {
background: window.app.viewer.Model.Image,
images: window.app.viewer.Collection.MediaCollection
}
});
@gaearon
gaearon / promise_scheduler.js
Last active March 29, 2018 06:12
Q promise concurrency limiter
'use strict';
var q = require('q');
/**
* Constructs a function that proxies to promiseFactory
* limiting the count of promises that can run simultaneously.
* @param promiseFactory function that returns promises.
* @param limit how many promises are allowed to be running at the same time.
* @returns function that returns a promise that eventually proxies to promiseFactory.
var mongoose = require('mongoose')
, Q = require('q')
, Wallet = mongoose.model('Wallet')
, _ = require('underscore')
, bitcoin = require('bitcoin');
function connect(coin) {
return new bitcoin.Client({
host: coin.host,
port: coin.port,
@gaearon
gaearon / feedback_widget.js
Last active August 29, 2015 13:56
Sample React widgets for SO question
/** @jsx React.DOM */
/* jshint trailing:false, quotmark:false, newcap:false */
define(function (require) {
'use strict';
var React = require('react'),
Promise = require('bluebird'),
$ = require('jquery');
define(function () {
'use strict';
var _ = require('underscore');
var ValidationMixin = {
getInitialState: function () {
return {
errors: []
define(function () {
'use strict';
var _ = require('underscore');
var EDITABLE_STATE = 'editable',
SUBMITTING_STATE = 'submitting',
SUBMITTED_STATE = 'submitted';
'use strict';
var Backbone = require('backbone'),
_ = require('underscore');
/**
* A mixin for watching for Backbone models and collections
* Usage:
*
* var SomeComponent = React.createClass({
var Popover = React.createClass({
mixins: [ClassNameMixin],
className: 'Popover',
propTypes: {
anchorOffset: React.PropTypes.oneOf(['normal', 'fromRoundBtn', 'fromSmallRoundBtn']),
expandFrom: React.PropTypes.shape({
horizontal: React.PropTypes.oneOf(['left', 'center', 'right']).isRequired,
@gaearon
gaearon / stores_and_pagination.js
Last active August 29, 2015 14:05
Removing duplication from similar paginating Flux Stores
// Assumes you're using normalizr https://github.com/gaearon/normalizr
// ---------------
// Helpers:
// ---------------
// StoreUtils
'use strict';
var Article = React.createClass({
mixins: [createStoreMixin(ArticleStore)],
propTypes: {
articleId: PropTypes.number.isRequired
},
getStateFromStores() {
return {
article: ArticleStore.get(this.props.articleId);