Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
@artsy/backbone-mixins
Advanced tools
A library of Backbone mixins that DRY up some common domain logic and Artsy API rabbit holes..
A library of Backbone mixins that DRY up some common domain logic and Artsy API rabbit holes. Used internally at Artsy, but maybe you'll find some useful patterns here.
Depends on your project's version of Backbone so remember to install both.
npm install backbone --save
npm install @artsy/backbone-mixins --save
Functions are namespaced by common sets of functionality such as "Markdown", "Image", or "Dimensions".
_ = require 'underscore'
{ Markdown, Image } = require '@artsy/backbone-mixins'
class Artwork extends Backbone.Model
_.extend @prototype, Markdown
_.extend @prototype, Image
{ Markdown } = require '@artsy/backbone-mixins'
class Artwork extends Backbone.Model
_.extend @prototype, Markdown
Converts an attribute into markdown
artist.mdToHtml('biography')
Converts an attribute into markdown & escapes html.
artist.mdToHtmlToText('biography')
Escapes html from an attribute.
artist.htmlToText('biography')
{ Dimensions } = require '@artsy/backbone-mixins'
class Artwork extends Backbone.Model
_.extend @prototype, Dimensions
Commonly dimensions in Artsy's API are structured like { dimensions: { in: "10 x 20" }, metric: "in" }
. This provides a convenient method for outputting a formatted dimension string based on this data. Pass in some options for better formatting.
artist.set { dimensions: { in: "10 x 20 in", cm: '13 1/2 x 24 2/3 cm' }, metric: "in" }
artist.dimensions() # '10 x 20 in'
artist.dimensions(metric: 'cm') # 13 1/2 x 24 2/3 cm
artist.dimensions(metric: 'cm', format: 'superscript') # 13 ½ x 24 ⅔ cm
artist.dimensions(metric: 'cm', format: 'decimal') # 13.5 x 24.33 cm
@get('metric')
)artist.dimensions(metric: 'cm')
Turns 10 3/4 into superscript html like 10 <sup>3/4</sup>
.
artist.dimensions(format: 'superscript')
Converts porely formatted metric dimensions that appear as fractions into a proper metric decimal formatting.
artist.dimensions(format: 'decimal')
Remember to pass in the artsy url.
{ Fetch } = require '@artsy/backbone-mixins'
{ ARTSY_URL } = require('sharify').data
class Artworks extends Backbone.Collection
_.extend @prototype, Fetch(ARTSY_URL)
For paginated routes, fetches the collection's url until the endpoint returns 0 results.
artworks.fetchUntilEnd success: ->
# Phew... I have all the artworks from Artsy
It respects data params like page (if you fetch page 1 on the server and fetchUntil
end on the client for example)
# I already rendered page 1 from a `fetch`...
artworks.fetchUntilEnd
data:
page: 2
size: 20
success: ->
# I now have artwork 21 - the end
Fetches a set by key and populates the collection with the first result.
featuredLinks.fetchSetItemsByKey 'homepage:featured-sections', success: ->
featuredLinks.first().get('name').should.equal 'Magnum Photos'
{ AToZ } = require '@artsy/backbone-mixins'
class Artworks extends Backbone.Collection
_.extend @prototype, AToZ
Sorts a collection groupped alphabetically based on the alphaSortKey
function on the model. If there is no alphaSortKey
function on the model it will default to @get 'sortable_id'
.
Sample output:
{
'0-9': [ model_instance, model_instance, model_instance ],
A: [ model_instance, model_instance, model_instance ],
// ...
Z: [ model_instance, model_instance, model_instance ]
}
artworks.model.alphaSortKey = -> @get 'title'
artworks.groupByAlpha()
Groups collection output in a format suitable for rendering in an A-Z list with a specified number of columns. The models must implement a href
and displayName
method.
Sample output:
[
{ letter: '0-9', columns: [ ... ] },
{ letter: 'A', columns: [ ... ] },
...
{ letter: 'Z', columns: [ ... ] }
]
// Each column is a 2D array of objects with `href`, and `name` properties:
columns: [
[ { href: '...', name: '...' }, ... { href: '...', name: '...' } ],
[ { href: '...', name: '...' }, ... { href: '...', name: '...' } ],
[ { href: '...', name: '...' }, ... { href: '...', name: '...' } ]
]
artworks.model.href = -> "/artwork/#{@get 'id'}"
artworks.model.displayName = -> @get 'title'
artworks.groupByAlphaWithColumns()
{ Image } = require '@artsy/backbone-mixins'
class FeaturedLink extends Backbone.Model
_.extend @prototype, Image
The Artsy API often structures image data like { image_url: 'foo/bar/:version.jpg' }
this method will return a replaced string with sensible defaults.
featuredLink.imageUrl('small')
imageUrl
uses the missingImageUrl
on the model to know what image to show when it can't find a version of it in the data. Defaults to /image/missing_image.png
.
Tell imageUrl
which version to look for first. Defaults to the first item in the image_versions
or versions
attribute.
Checks the image_versions
or versions
attribute for the image version and returns true/false.
Return an image URL, preferring the earlier versions in versions
if available, but degrading to the later ones otherwise. Finally, degrades to missingImageUrl
.
feature.bestImageUrl(['large', 'medium', 'small'])
{ CalendarUrls } = require '@artsy/backbone-mixins'
class FairEvent extends Backbone.Model
_.extend @prototype, CalendarUrls({address: 'venue_address', title: 'name'})
Produces a url to create a Google calendar event.
event.googleCalendarUrl()
Produces a url to create a Yahoo calendar event.
event.yahooCalendarUrl()
Produces a url to create Outlook and iCal calendar event.
event.ics()
Please fork the project and submit a pull request with tests. Install node modules npm install
and run tests with npm test
.
MIT
FAQs
A library of Backbone mixins that DRY up some common domain logic and Artsy API rabbit holes..
We found that @artsy/backbone-mixins demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 14 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.