Skip to content

Instantly share code, notes, and snippets.

View gryzzly's full-sized avatar

Misha Reyzlin gryzzly

View GitHub Profile
@gryzzly
gryzzly / logical OR vs array::some.md
Last active August 29, 2015 14:01
logical OR vs array::some
return !!(// double negation is required since logical operators dont always return Booleans
  ENV_VAR || 
  SomeLongStore.get('key') ||
  SomeConfig.get('layer', 'sub-layer') === 'required-value' ||
  this.anotherLongCondition()
)

vs

Example of a tedious task when using backbone MVC

You have three components each of which might have certain state. These components depend on one particular component that emits events. These three views have to all define the emitting component as a dependency and manually subscribe to the events in order to sync the state.

var View = require('lib/View'),
    source = require('source'); 
    
module.exports = View.extend({
@gryzzly
gryzzly / index.js
Last active August 29, 2015 14:27
requirebin sketch
var createRedux = require('redux').createRedux;
var reducer = function (state, action) {
return state;
};
var redux = createRedux(reducer);
redux.dispatch({
type: 'FOO',
payload: {}
});
var reverseString = function ( subj ) {
var helper = subj.split (''),
i = 0, k = helper.length, len = helper.length / 2,
swap
for ( ; i < len; i += 1, k -= 1 ) {
swap = helper[ k ];
helper[ k ] = helper[ i ];
helper[ i ] = swap;
}
@gryzzly
gryzzly / gist:1364474
Created November 14, 2011 17:10
renderTemplate from DoAT Touchy, basic JS templating
// Render JavaScript template (from http://git.io/B7KxZg DoAT Touchy)
//
// You can place your template in a script tag with type other than "text/javascript"
// such as "text/html", like this:
// <script type="text/html" id="item-template">
// <li id="{{id}}">
// <p>{{content}}</p>
// </li>
// You'd then render your template like this:
// var itemTemplate = document.getElementById('item-template').innerHTML;
@gryzzly
gryzzly / gist:1714086
Created February 1, 2012 00:07
Backbone's extend self-propagating function
// Helper function to correctly set up the prototype chain, for subclasses.
// Similar to `goog.inherits`, but uses a hash of prototype properties and
// class properties to be extended.
var inherits = function(parent, protoProps, staticProps) {
var child;
// The constructor function for the new subclass is either defined by you
// (the "constructor" property in your `extend` definition), or defaulted
// by us to simply call the parent's constructor.
if (protoProps && protoProps.hasOwnProperty('constructor')) {
@gryzzly
gryzzly / naive-bind.js
Created April 4, 2012 14:21
Basic Function.bind
// naïve bind implementation
Function.prototype.bind = function () {
var obj = arguments[0], args = [].slice.call(arguments, 1);
return function () {
this.apply(
obj,
[].concat.call(args, [].slice.call(arguments))
);
}
@gryzzly
gryzzly / testtest.js
Created April 29, 2012 17:04
Testing
function () {}
@gryzzly
gryzzly / backboneView.handleEvent.md
Created May 16, 2012 23:54
Using handleEvent with Backbone

Combining handleEvent and Backbone

I'm sure for most of you it's the old news, but here's a short reminder anyway:

song = {
	handleEvent: function (event) {
    	switch (event.type) {
          case: "click":

console.log(this.name);

@gryzzly
gryzzly / backbone-touch-click.js
Created May 21, 2012 15:41
Backbone touch / click events
var down = "touchup" in window ? "touchup" : "click";
var abstractView = Backbone.View.extend({
// we don't want both click and touch handlers
// delegated on touch-enabled devices
events: function () {
"click .toggler" : "foo",
"touchup .toggler" : "bar"
},
initialize: function () {