Skip to content

Instantly share code, notes, and snippets.

@gib
Last active December 11, 2015 00:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gib/4515905 to your computer and use it in GitHub Desktop.
Save gib/4515905 to your computer and use it in GitHub Desktop.

I love coffescript's null soakage ? operator, but I recently started using Backbone.Model's @has (http://backbonejs.org/#Model-has, http://backbonejs.org/docs/backbone.html#section-37) when determining if an attribute is set. The JavaScript result is much more DRY since it uses a single Backbone method instead of duplicating the logic, you also save a var...

@get('attachments')?.length > 0 

becomes

var _ref; 
((_ref = this.get('attachments')) != null ? _ref.length : void 0) > 0; 

While you need more coffee script for @has,

(@has('attachments') and @get('attachments').length > 0) 

the js is a little lighter

this.has('attachments') && this.get('attachments').length > 0; 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment