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

baseviewmodel

v0.2.2

Published

Backbone like model extension for knockout viewmodels, including automatic observable and computed function creation for model properties.

Downloads

3

Readme

BaseViewModel

Backbone like model extension for knockout viewmodels, including automatic observable and computed function creation for model properties. Based on ideas in bmac's BaseViewModel and jcreamer898's ko.ninja. Uses Backbone's extend function to provide inheritance. BaseViewModel Project Page.

Getting started

If you wish to contribute to BaseViewModel's development, clone the project and install the dependencies with npm install. That's it!

To build the files for deployment, run grunt build.

BaseViewModel is available on npm and bower and is a UMD module.

Examples

Create a BaseViewModel sub-class

You can extend the BaseViewModel class by using the extend method, passing in an object to be copied over to the new sub-class. Additionally, passing in a defaults hash will create default model observables on the sub-class.

var Person = BaseViewModel.extend({
  defaults: {
    type: 'Human',
    getType: function() {
      return 'Species: ' + this.type();
    }
  },
  init: function(){
    // Do something when class instantiated
  }
});

var me = new Person();
me.type(); // 'Human'
me.getType(); // 'Species: Human'

Passing a model

As well as sub-classing, you can pass an object to the BaseViewModel class or one of it's sub-classes to act as the model for that view model.

var meModel = {
  name: 'Lee',
  age: 28,
  address: {
    city: 'London'
  },
  greeting: function() {
    return 'Hello, I\'m ' + this.name() + ' from ' + this.address.city();
  }
};

var me = new Person(meModel);
me.name(); // 'Lee'
me.age(); // 28
me.address.city(); // 'London'
me.greeting(); // 'Hello, I'm Lee from London'

Getting back the model

To return the original model there is a toModel method to return the original model as an object, or a toJSON method that returns a JSON representation of the original model. Both methods ignore default properties.

var model = {
  foo: 'foo',
  bar: 'bar'
};
var VM = new BaseViewModel.extend({
  defaults: {
    foobar: 'foobar'
  }
});
var myVM = new VM(model);
var obj = myVM.toModel();
var objJSON = myVM.toJSON();

console.log(obj.foobar); // undefined
console.log(obj.foo); // 'foo'
console.log(obj); // { foo: 'foo', bar: 'bar' }
console.log(objJSON); // '{"foo":"foo","bar":"bar"}'