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

smackbone

v0.2.18

Published

an object oriented model framework

Downloads

4

Readme

  ____                       _    _                      
 / ___| _ __ ___   __ _  ___| | _| |__   ___  _ __   ___
 \___ \| '_ ` _ \ / _` |/ __| |/ / '_ \ / _ \| '_ \ / _ \
  ___) | | | | | | (_| | (__|   <| |_) | (_) | | | |  __/
 |____/|_| |_| |_|\__,_|\___|_|\_\_.__/ \___/|_| |_|\___|

An attempt to make a more object oriented model framework.

Installation

	npm install smackbone

Usage

	smackbone = require("smackbone"); // AMD or CommonJS

Model

Should hold the data (attributes) of the application. When a attribute is changed, it fires a change event and a keyed change event. It has support for transient properties.

set set(attributes, [options]) Sets the attributes according to input.

unset unset(attribute, [options]) Unsets all objects and performs a set with the attributes. See set function.

reset reset(attributes, [options])

get get(attribute) Returns the value of the given attribute.

path path() Returns the relative path for the model.

clone clone() Returns a new but identical object.

toJSON toJSON Returns an object literal containing the attributes.

fetch fetch() Fetches the model from the backend (using one or more Syncers). Triggers a GET request to the server.

save save() Saves the model to the backend. Triggers a POST or PUT request to the server.

destroy destroy() Destroys the model in the backend. Triggers a DELETE request to the server.

	sheep = new smackbone.Model();

	sheep.set("name", "Shaun");
	sheep.set({name: "Shaun", material: "Clay"});

	sheep.unset("name");

	sheep.get("name");

	dolly = sheep.clone();
	dolly.set("name", "Dolly");

	dolly.toJSON();

	sheep.fetch();

	sheep.save();

	sheep.destroy();

Collection

An ordered set of models. It fires add and remove events when adding and removing. You can populate it by adding single models, arrays of objects or model hierarchies. It inherits from Model, so all functions available on Model can be called on a Collection.

get get(model|cid|id) Fetches a model from the collection, specified by an id, a cid, or a model.

add add(model) Adds the model to the collection. The key used is the .id attribute if it is present, otherwise the internal .cid attribute.

remove remove(model) Removes the model from the collection.

each each(func) Calls the function for each model stored with set.

contains contains(attribute) Checks if the specified model or id is stored in the collection.

isEmpty isEmpty() Returns true if it doesn't contain any objects.

at at(index) Fetches an object at the given index (in the order that they were set or added).

first first() Returns the first object stored in the model.

last last() Returns the last object stored in the model.

toJSON toJSON() Returns a copy of the stored objects that is useful for serialization (e.g. JSON.stringify).

	collection = new smackbone.Collection();
	model = new smackbone.Model();

	@collection.add(model);

	@collection.remove(model);

	collection.each(function(object) {
		console.log("object:", object);
	});

	collection.contains(model);

	collection.isEmpty();

	collection.at(0);

	collection.first();

	collection.last();

	collection.toJSON();

Event

Use to enable triggering and binding of custom events.

	class EventEmitter extends smackbone.Event
	emitter = new EventEmitter

on on("event", callback) Adds the callback for the specified event. The event string can contain multiple space-separated event names.

off off([event], [callback]) Removes the callback for the specified event. If no callback is specified, then all callbacks for that event is removed. If event isn't specified, all callbacks are removed for this object.

trigger trigger("event"[,args...]) Triggers the callbacks for the specified event.

	emitter.on("event", function(data) {
		console.log("data:", data);
	});
	emitter.trigger("event");
	emitter.off("event");

Syncer

Performs sync to and from the backend. The sync commands are fetch, save and destroy.

cat = new Model({id:42});
var syncer = new Syncer({model: cat});
syncer.urlRoot = "http://some.server:32000/cats";
cat.fetch(); // Fetches the cat with id 42