npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

backbone.viewmodel

v0.0.9

Published

Backbone.ViewModel implementation with attributes mapping and agile synchronization.

Downloads

3

Readme

Backbone.ViewModel

Yes, YA Backbone.ViewModel here! Because we are can! :)

Actually I need solutions to link two different Views on base of one Model and ViewModel is matter. See example project for more details.

Description:

Backbone.ViewModel may used between Model and View in Backbone or BB.Marionette or other BB-compatibility projects.

Its not drop-in replacement for Model, but it handle correctly some methods and properties form Model and should play well with others (you may write request with testcase if important Madel functionality looks omitted).

As ViewModel its support some additional features as computed values, controlled autosync with Model and defaults values.

Install:

node.js

npm install backbone.viewmodel

browser

Get copy of /browser_lib/backbone.viewmodel.js or use CommonJS-style with clinch.

Usage:

All examples written with CoffeeScript, you may use plain JS instead (but why?).

How its works:

class RectangleViewModel extends Backbone.ViewModel
  autoupdate  : on
  mapping : 
    color   : 'color'
    width   : 'size'
    height  : 'getHeight'
  getHeight : ->
    @model.get('size') * ( 1 + Math.random() ) | 0

rectangle = new RectangleViewModel color : 'blue', size : 20
console.log rectangle.toJSON() # -> Object {color: "blue", width: 20, height: 22} 

rectangle.model.set 'size', 50 # its bad example of `model` interaction, but ok here
console.log rectangle.toJSON() # -> Object {color: "blue", width: 50, height: 74} 

API

Backbone.ViewModel have few methods and some numbers of class properties.

Properties:

###
model prop may be used to create specific Model from raw data,
by default VM use Backbone.Model
###
model       : BaseBB.Model
###
VM do not auto-update itself properties on model changes
use this knob to turn it on
###
autoupdate  : on
###
all keys in mapping will be 'mapped' self VM functions OR
model attributes, with priority: function, than attributes
###
mapping : 
  color   : 'color'
  height  : 'getHeight'

Methods:

constructor(dataIn, constructorAttrs, options)

  • dataIn - BB.Model or raw data object
  • constructorAttrs - default VM properties may be placed here
  • options - some options, like autoupdate

update()

Just proceed process to update VM from current model state. May be called if autoupdate property not used for some reason.

BB.Model methods

Backbone.ViewModel as BB.Model will support much helpfully methods like toJSON() or get(), dozen unsuitably methods like set() unset() or clear() and (at now) will available to call erroneously methods like fetch() or sync() - just do not use them.

If you need to have synced model - create it first and use thyself methods in ViewModel update or autoupdate combinations.