0.1.19 • Published 9 years ago

mvstar v0.1.19

Weekly downloads
6
License
-
Repository
-
Last release
9 years ago

mvstar Build Status

MV★ framework. No fancy virtual DOM. Minimal bits needed for sensible client-side applications. Declarative one-way databinding. Event emitters. Simple routing. Computed properties. Completely explicit. No dependencies outside of a dollar library.

Views

Views encapsulate bits of DOM. Generally like Backbone views but with declarative one-way databinding.

class LineItem extends View
  # Can reference a template tag in the DOM, which will be used to create new
  # view instances
  template: '#line-item-template'

  # Or alternately bind to existing element:
  # el: '#line-item'

  # Declare bindings from state -> selectors in DOM. You can use @ to target
  # specific attrs.
  bindings:
    img:  'img.thumbnail @src'
    sku:  'input.sku     @value'
    name: 'a.title'

  # Computed properties, generated from different bits of state.
  computed:
    desc: (color, size) -> [color, size]

  # Watch for changes, and recompute on changes.
  watching:
    desc: ['color', 'size']

  # Format state before rendering into DOM.
  formatters:
    desc: (v) ->
      if v.length > 1
        v.join ' / '
      else
        v.join ''

  # Bind to events.
  events:
    'change .quantity input': 'updateQuantity'
    'click .remove-item':     'removeItem'

  # You need to manually unbind events if you define any.
  removeItem: ->
    @unbind()
    @remove()

  # Handle change event
  updateQuantity: (e, el) ->
    # Get quantity, el refers to current target of event
    quantity = parseInt $(el).val(), 10

    # Update quantity on state bound to this view
    @set 'quantity', quantity

    false

Events

Extend from mvstar.EventEmitter and wire up things with events.

class Cart extends EventEmitter
  constructor: (@cart) ->
    super
    @cart    ?= {}
    @subtotal = 0
    @quantity = 0

  get: (sku) ->
    @cart[sku]

  # You must manually emit things you care about.
  add: (item) ->
    @emit 'add', item

    @quantity += item.quantity
    @subtotal += item.quantity * item.price

    if (@get item.sku)?
      @cart[item.sku].quantity += item.quantity
    else
      @cart[item.sku] = item

    @

  remove: (sku) ->
    @emit 'remove', @cart[sku]
    delete @cart[sku]
    @

  clear: ->
    @emit 'clear'
    @cart = {}
    @
0.1.19

9 years ago

0.1.18

9 years ago

0.1.17

9 years ago

0.1.16

9 years ago

0.1.15

9 years ago

0.1.14

9 years ago

0.1.13

9 years ago

0.1.12

9 years ago

0.1.8

9 years ago

0.1.7

9 years ago

0.1.6

9 years ago

0.1.5

9 years ago

0.1.4

9 years ago

0.1.3

9 years ago

0.1.2

9 years ago

0.1.1

9 years ago

0.1.0

9 years ago

0.0.23

9 years ago

0.0.22

9 years ago

0.0.21

9 years ago

0.0.20

9 years ago

0.0.19

9 years ago

0.0.18

9 years ago

0.0.17

9 years ago

0.0.15

9 years ago

0.0.14

9 years ago

0.0.13

9 years ago

0.0.11

9 years ago

0.0.10

9 years ago

0.0.9

9 years ago

0.0.8

9 years ago

0.0.7

9 years ago

0.0.6

9 years ago

0.0.5

9 years ago

0.0.4

9 years ago

0.0.3

9 years ago

0.0.2

9 years ago

0.0.1

10 years ago