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-forkable-model

v1.1.0

Published

Extend Backbone.js models to be forked and merged.

Downloads

4

Readme

backbone-forkable-model

Extend Backbone.js models to make them forkable from another model, and later able to be merged back to the original.

Use Case

The original use case for this was that I had a lot of UI views that let users edit models in a modal window (popup editor form).

The modal dialog would always have "Cancel" and "Save" buttons.

On close, I would need to revert the model back to its original state. We got in the habbit of passing a .clone() of the model into the editor window as the Model instead of the "real" copy of the model that was loaded from the server. Then when "cancel" was clicked, no action had to be taken. The cloned copy of the model that the editor was using could just be discarded. On "save", the cloned (edited) model's attributes could be copied back tot he "main" copy with a simple original.set(cloned.attributes);

In spirit, that is what this model mixin provides.

Installation

(not yet published to NPM and Bower. It is on my todo list!)

Add to page scripts after backbone.js.

This library does not support Require.js or Browserify at this time (in other words, it is not written as a module).

Usage / API

This mixin attaches itself to the global Backbone object as Backbone.ForkableModel.

Extend a Backbone model (or backbone-deep-model):

var MyModel = Backbone.Model.extend(Backbone.ForkableModel.mixin).extend({
    yourCustomStuff: function() {},
    goesInHere: function() {}
});

You can chain .extend() calls to add more mixins. For example I typically use DeepModel and the Backbone.Validation library:

var MyModel = Backbone.DeepModel
    .extend(Backbone.ForkableModel.mixin)
    .extend(Backbone.Validation.mixin)
    .extend({

    yourCustomStuff: function() {},
    goesInHere: function() {}
});

The ForkableModel.mixin adds these functions to instances of the model:

.fork()

Returns a new instance of the model wil all attributes copied tot he new instance. This is bascially the same as Model.clone();

.unfork()

Sets the attributes of the instance of the model that was originally forked to match this forked copy (Copies property values back to the original).

This is done using Model.set() and Model.unset() functions internally, so all events will be triggered on the model instance as if .set() was called normally.

Returns the instance of the model that was orignally forked from.

.forkHasChanges()

Returns boolean, true if the forked copy of the model has any changes from the original. Otherwise returns false.

Sample Code

Marionette.ItemView.extend({
    events: {
        'click #editButton': 'onEdit'
    },

    // When the user wants to edit this item, an edit form is opened.
    onEdit: function() {
        var editDialog = new EditDialogView({
            model: this.model.fork()
        });

        // the EditDialogView triggers a "save" event when the user clicks the save button
        this.listenTo(editDialog, 'save', function(editedModel) {
            var originalModel = editedModel.unfork();

            // originalModel is the same instance as "this.model" that .fork() was called on.

            originalModel.save().done(function() {
                // notify user of successful save
            }).fail(function() {
                // notify user of error
            });
        });

        editDialog.show();
    }
});