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

ampersand-model-patch-mixin

v3.4.0

Published

"Sync implementation for Ampersand and Backbone that implements the RFC 6902 json+patch spec on updates."

Downloads

42

Readme

ampersand-model-patch-mixin

Sync implementation for Ampersand and Backbone that implements the RFC 6902 json+patch spec on updates.

How is it different than the default approach?

If the model is new, Ampersand model's behavior is completely ordinary. Backbone models are modified to pick up their child models/collections data in toJSON as per Ampersand's default behavior.

When the model is not new, the mixin sets watchers on all of the model's own properties, child models, and child collections and creates patch operations for any additions, changes, or removals that occur. The model's save method is overridden to send the accumulated patch operations with the HTTP PATCH verb.

As operations are accumulated, the current operation count is published as a patcher:op-count event to allow auto-saving based on your criteria.

How do I use it?

With Ampersand model, you could do something like this:

var Model = require('ampersand-model');
var Collection = require('ampersand-collection');
var patcherMixin = require('ampersand-model-patch-mixin');

var PatchingModel = Model.extend(patcherMixin(Model, {
    initialize: function (attrs) {
        this.initPatcher(attrs);
    },
    props: {
        id: 'number',
        name: ['string', true],
        age: 'number'
    }
    children: {
        car: Model.extend({
            props: {
                id: 'number',
                make:  ['string', true],
                model: ['string', true],
                color: 'string'
            }
        })
    },
    collections: {
        shoes: Collection.extend({
            model: Model.extend({
                props: {
                    id: 'number',
                    color: ['string', true],
                    style: ['string', true]
                }
            })
        })
    }
}));

module.exports = PatchingModel;

Backbone is a little bit more complicated, but still pretty simple:

var _ = require('underscore');
var Backbone = require('backbone');
var patcherMixin = require('ampersand-model-patch-mixin');

var PatchingModel = Backbone.Model.extend(patcherMixin(Backbone.Model, {
    initialize: function (attrs) {
        this._initChildren(attrs);
        this._initCollections(attrs);
        this.initPatcher(attrs);
    },
    sync: sync,
    _children: {
        car: Backbone.Model.extend({})
    },
    _collections: {
        shoes: Backbone.Collection.extend({
            model: Backbone.Model.extend({})
        })
    },
    _initChildren: function (attrs) {
        _.each(this._children, function (childConstructor, name) {
            this[name] = new childConstructor(attrs[name] || {});
        }.bind(this));
    },
    _initCollections: function (attrs) {
        _.each(this._collections, function (collectionConstructor, name) {
            this[name] = new collectionConstructor(attrs[name] || []);
        }.bind(this));
    }
}));

module.exports = PatchingModel;

NOTE: The mixin is a function that must be called with the Model constructor that we are extending from. This allows us to pick up the methods that we will be wrapping from its prototype. You may also pass a protoProps object as you normally would directly to the extend method.

Configuration

You can customize behavior by adding a _patcherConfig object to your (optional) protoProps object:


var PatchingModel = Model.extend(patcherMixin(Model, {
    _patcherConfig: {
        originalProperty: '_original',
        modelProperty: '_children',
        collectionProperty: '_collections',
        autoSave: undefined,
        debug: undefined
    }
}));

The above is the equivalent of the default behavior.

config.originalProperty

This is the property on the model where the last known server state is stored. It is used primarily to ensure that operation paths for collection models are generated correctly.

config.modelProperty

This is the property of the model where we will find the names of child models to watch for changes.

config.collectionProperty

This is the property of the model where we will find the names of child collections to watch for changes.

config.autoSave

You can set this item with either a number or a function to control when automatic saving will occur.

If it's a function, when a new patch operation is created the function will be called with the model and current operation count as arguments and the model as this context. If the function returns a truthy value, the model will be saved.

If it's a number and the operation count is greater than or equal to the number, the model will be saved.

config.debug

If you set this to true, debug output will be sent to the console when the mixin runs and when its methods are called.