Skip to content

Instantly share code, notes, and snippets.

@ianmstew
Last active April 9, 2018 16:49
Show Gist options
  • Save ianmstew/7492c10a3a374c44058c to your computer and use it in GitHub Desktop.
Save ianmstew/7492c10a3a374c44058c to your computer and use it in GitHub Desktop.
Code Style
// Derived from AirBnB + LinkedIn Idiomatic + Crockford
// https://github.com/airbnb/javascript
// https://github.com/linkedin/idiomatic.js (Idiomatic fork with Crockford inner spacing)
// http://javascript.crockford.com/code.html
{
// Keywords
"requireSpaceAfterKeywords": ["if", "else", "for", "while", "do", "switch", "return", "try", "catch"],
"disallowKeywords": ["with", "eval"],
// Functions
"requireParenthesesAroundIIFE": true,
"disallowSpacesInNamedFunctionExpression": {
"beforeOpeningRoundBrace": true
},
"requireSpacesInAnonymousFunctionExpression": {
"beforeOpeningRoundBrace": true
},
// Blocks
"disallowEmptyBlocks": true,
"disallowNewlineBeforeBlockStatements": true,
"requireSpaceBeforeBlockStatements": true,
// Curlies, brackets, parens
"disallowSpaceAfterObjectKeys": true,
"requireSpacesInsideObjectBrackets": "allButNested",
"disallowSpacesInsideArrayBrackets": true,
"disallowSpacesInsideParentheses": true,
// Line breaking
"requireCommaBeforeLineBreak": true,
"requireOperatorBeforeLineBreak": ["+", "-", "/", "*", "=", "==", "===", "!=", "!==", ">", ">=", "<", "<="],
"disallowMultipleLineBreaks": true,
// Unary operators
"disallowSpaceAfterPrefixUnaryOperators": ["++", "--", "+", "-", "~", "!"],
"disallowSpaceBeforePostfixUnaryOperators": ["++", "--"],
// Binary operators
"disallowSpaceBeforeBinaryOperators": [","],
"requireSpaceBeforeBinaryOperators": ["?", "+", "-", "/", "*", "=", "==", "===", "!=", "!==", ">", ">=", "<", "<=", "&&", "||"],
"requireSpaceAfterBinaryOperators": [",", "?", "+", "-", "/", "*", "=", "==", "===", "!=", "!==", ">", ">=", "<", "<=", "&&", "||"],
// Ternary operator
"requireSpacesInConditionalExpression": {
"afterTest": true,
"beforeConsequent": true,
"afterConsequent": true,
"beforeAlternate": true
},
// Per-line whitespace
"validateIndentation": 2,
"disallowTrailingWhitespace": true,
"requireLineFeedAtFileEnd": true,
// Do not allow reassignment of "this" (prefer bind instead)
"safeContextKeyword": "bind",
// Misc
"disallowMultipleLineStrings": true,
"requireDotNotation": true,
"requireSpaceAfterLineComment": true,
"disallowYodaConditions": true,
"disallowTrailingComma": true,
// JSHint formatting
"requireCapitalizedConstructors": true,
"maximumLineLength": 100,
"validateQuoteMarks": { "mark": "'", "escape": true },
"requireCamelCaseOrUpperCaseIdentifiers": true,
"disallowMultipleVarDecl": true
// TODOs (require new JSCS development)
// - allow "} else", disallow "}else"
// - allow "{ a: 1 }", disallow "{ a:1 }"
// - requireCurlyBraces: option to except single statements
// allow
// if (true) doThis();
// if (true) {
// doThis();
// }
// disallow
// if (true)
// doThis();
// - disallowMultipleVarDecl: option to except multiple vars without assignments
// allow "var one, two, three;"
// disallow "var one = 1, two = 2, three = 3;"
// - disallow "test() ;", require "test();"
// - Require chaining on new lines to have single indent
// allow
// that.doSomething()
// .doSomethingElse();
// disallow
// that.doSomething()
// .doSomethingElse();
}
// Dervied from AirBnB
// https://github.com/airbnb/javascript/blob/master/linters/jshintrc
// Note: All _formatting_ guidelines have moved to .jscsrc
{
// Define globals exposed by modern browsers.
"browser": true,
// Define globals exposed by jQuery.
"jquery": true,
// Define globals exposed by Node.js
"node": true,
// Custom globals
"globals": {
"define": true,
"require": true,
"_": true,
"Promise": true
},
// Unfortunate overkill, but necessary for preceding || and &&.
// See https://github.com/jshint/jshint/issues/735.
"laxbreak": true,
// Prohibit use of == and != in favor of === and !==.
"eqeqeq": true,
// Suppress warnings about == null comparisons.
"eqnull": true,
// Prohibit use of a variable before it is defined.
"latedef": true,
// Prohibit use of explicitly undeclared variables.
"undef": true,
// Warn when variables are defined but never used.
"unused": "vars",
// Avoid long function definitions
"maxparams": 4,
// Prevent deep nesting
"maxdepth": 2,
// Concise functions
"maxstatements": 15,
// Prevent excessive branching
"maxcomplexity": 6
}
define(function (require) {
var Presenter = require('lib/classes/presenter');
var LoadingView = require('modules/loading/loading.view');
/*
* Brian Mann's loading controller: http://www.backbonerails.com/screencasts/loading-views
* Shows a loading view until the original view's model/collection are ready, then swaps.
*/
var LoadingPresenter = Presenter.extend({
_options: null,
initialize: function (options) {
_.bindAll(this, '_loaded');
},
onPresent: function (options) {
var view = options.view;
var loadingView = new LoadingView();
// Gather entity promises
var syncings = this._getSyncings(view);
// Show loading view
this.show(loadingView, { nobind: true });
// When entities are ready, show original view
Promise.all(syncings)
.finally(_.partial(this._loaded, view, loadingView));
},
// Gather the 'syncing' promise from a view's model, collection, or both.
// Relies upon lib/shim/backbone-syncing-state.
_getSyncings: function (view) {
return _.chain(view)
.pick('model', 'collection')
.pluck('syncing')
.compact()
.value();
},
_loaded: function (view, loadingView) {
if (this.region.currentView !== loadingView) {
// Another view besides the original has superseded the user. This means the user has
// moved on before data returned, so the original view is outdated and should be discarded.
view.destroy();
} else {
// Loading view still showing and data is back--swap it out for the original!
this.show(view);
}
}
});
return LoadingPresenter;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment