0.1.19 • Published 11 years ago

mvstar v0.1.19

Weekly downloads
6
License
-
Repository
-
Last release
11 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

11 years ago

0.1.18

11 years ago

0.1.17

11 years ago

0.1.16

11 years ago

0.1.15

11 years ago

0.1.14

11 years ago

0.1.13

11 years ago

0.1.12

11 years ago

0.1.8

11 years ago

0.1.7

11 years ago

0.1.6

11 years ago

0.1.5

11 years ago

0.1.4

11 years ago

0.1.3

11 years ago

0.1.2

11 years ago

0.1.1

11 years ago

0.1.0

11 years ago

0.0.23

11 years ago

0.0.22

11 years ago

0.0.21

11 years ago

0.0.20

11 years ago

0.0.19

11 years ago

0.0.18

11 years ago

0.0.17

11 years ago

0.0.15

11 years ago

0.0.14

11 years ago

0.0.13

11 years ago

0.0.11

11 years ago

0.0.10

11 years ago

0.0.9

11 years ago

0.0.8

11 years ago

0.0.7

11 years ago

0.0.6

11 years ago

0.0.5

11 years ago

0.0.4

11 years ago

0.0.3

11 years ago

0.0.2

11 years ago

0.0.1

11 years ago