@artsy/backbone-mixins

中文文档
3.0.0发布于 2021/09/23近 30 天下载 6847

A library of Backbone mixins that DRY up some common domain logic and Artsy API rabbit holes..

原始使用文档
文档目录26 节
  1. Installation
  2. Usage
  3. Markdown
  4. mdToHtml(attr)
  5. mdToHtmlToText(attr)
  6. htmlToText(attr)
  7. Dimensions
  8. dimensions([options])
  9. Fetch
  10. fetchUntilEnd(options)
  11. fetchSetItemsByKey(key, options)
  12. AToZ
  13. groupByAlpha()
  14. groupByAlphaWithColumns([numberOfColumns])
  15. Image
  16. imageUrl([version])
  17. missingImageUrl()
  18. defaultImageVersion()
  19. hasImage(version)
  20. bestImageUrl([versions])
  21. Calendar Urls
  22. googleCalendarUrl()
  23. yahooCalendarUrl()
  24. ics()
  25. Contributing
  26. License

@artsy/backbone-mixins

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.

Installation

Depends on your project's version of Backbone so remember to install both.

npm install backbone --save
npm install @artsy/backbone-mixins --save

Usage

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

{ Markdown } = require '@artsy/backbone-mixins'

class Artwork extends Backbone.Model

  _.extend @prototype, Markdown

mdToHtml(attr)

Converts an attribute into markdown

artist.mdToHtml('biography')

mdToHtmlToText(attr)

Converts an attribute into markdown & escapes html.

artist.mdToHtmlToText('biography')

htmlToText(attr)

Escapes html from an attribute.

artist.htmlToText('biography')

Dimensions

{ Dimensions } = require '@artsy/backbone-mixins'

class Artwork extends Backbone.Model

  _.extend @prototype, Dimensions

dimensions([options])

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

options.metric: ('in' or 'cm') (defaults to @get('metric'))

artist.dimensions(metric: 'cm')

options.format: 'superscript'

Turns 10 3/4 into superscript html like 10 <sup>3/4</sup>.

artist.dimensions(format: 'superscript')

options.format: 'decimal'

Converts porely formatted metric dimensions that appear as fractions into a proper metric decimal formatting.

artist.dimensions(format: 'decimal')

Fetch

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)

fetchUntilEnd(options)

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

fetchSetItemsByKey(key, options)

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

{ AToZ } = require '@artsy/backbone-mixins'

class Artworks extends Backbone.Collection

  _.extend @prototype, AToZ

groupByAlpha()

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()

groupByAlphaWithColumns([numberOfColumns])

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

{ Image } = require '@artsy/backbone-mixins'

class FeaturedLink extends Backbone.Model

  _.extend @prototype, Image

imageUrl([version])

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')

missingImageUrl()

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.

defaultImageVersion()

Tell imageUrl which version to look for first. Defaults to the first item in the image_versions or versions attribute.

hasImage(version)

Checks the image_versions or versions attribute for the image version and returns true/false.

bestImageUrl([versions])

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'])

Calendar Urls

{ CalendarUrls } = require '@artsy/backbone-mixins'

class FairEvent extends Backbone.Model

  _.extend @prototype, CalendarUrls({address: 'venue_address', title: 'name'})

googleCalendarUrl()

Produces a url to create a Google calendar event.

event.googleCalendarUrl()

yahooCalendarUrl()

Produces a url to create a Yahoo calendar event.

event.yahooCalendarUrl()

ics()

Produces a url to create Outlook and iCal calendar event.

event.ics()

Contributing

Please fork the project and submit a pull request with tests. Install node modules npm install and run tests with npm test.

License

MIT

版本与发布记录

@artsy/backbone-mixins 共发布 2 个版本,首次发布于 2020/08/14;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.0.0latest2021/09/235 年前
2.0.02020/08/146 年前

关于 @artsy/backbone-mixins 的常见问题

@artsy/backbone-mixins 是什么?

A library of Backbone mixins that DRY up some common domain logic and Artsy API rabbit holes..

如何安装 @artsy/backbone-mixins?

在项目目录运行 npm install @artsy/backbone-mixins,或使用 pnpm add / yarn add / bun add。

@artsy/backbone-mixins 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/artsy__backbone-mixins 是否存在。

@artsy/backbone-mixins 可以商用吗?

@artsy/backbone-mixins 使用 未声明 许可。package.json 没有声明许可证。没有许可证的代码默认保留全部权利,商用前请联系作者或查看仓库中的 LICENSE 文件。以上为概括,不构成法律意见,请以许可原文为准。

@artsy/backbone-mixins 的下载量有多少?

根据 npm 官方下载统计,@artsy/backbone-mixins 在 2026/10/07 – 2026/11/05 这 30 天内被下载 6,847 次,日均约 228 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@artsy/backbone-mixins 还在维护吗?

最新版本 3.0.0 发布于 2021/09/23(5 年前);至今共发布 2 个版本,其中过去 12 个月发布了 0 个;首次发布于 2020/08/14。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。