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-base-view

v3.1.0

Published

Baseview is a extended backbone view with convenient methods for manipulating subviews and events.

Downloads

65

Readme

Backbone base view

Build Status Coverage Status NPM Status

Backbone view extension with enhanced event handling and convenient helpers for handling sub-views. Compose view components with simple and easy to use api. Weighs less than 3KB.

BaseView extends backbone view events functionality so you can bind one time events, inject variables into event strings and setup global window and document listeners that will be properly unbound once view is removed. Enables easy composition of views with simple parent-child and model binding api that keeps you safe from memory leaks.

Examples and api

events

Define one time events, inject variables and add window and document listeners.

events: {
    'click .selector': 'handler',
    'click {{this.someVariable}}': 'handler', // variable will be injected
    'one:submit form': 'oneSubmit', // handler will run only once
    'resize window': 'onWindowResize',
    'keyup document': 'onDocumentKeyup'
}

assignOptions: false|true|'deep'

If defined user passed options will be merged with defaults and written to viewInstance.options. False by default.

var View = BaseView.extend({
    assignOptions: true,
    defaults: {test: 1},
    initialize: function() {
        console.log(this.options); // outputs {foo:'bar', test: 1}
    }
});
var view = new View({foo:'bar'});

Options type checking and validation

Options provided by type defaults and and constructor parameters can be type checked and validated.

var MusicianView = BaseView.extend({
    optionRules: {
        instrument: String,
        age: {type: Number, default: 18, validator: function(age) {
            return age >= 18;
        }},
        mentorView: {type: BaseView, required: false}
        url: [String, Function]
    }
});

delegatedEvents: true|false

View event handlers are delegated to instance element by default. Set to false to bind directly to elements found via event string selector.

var View = BaseView.extend({
    delegatedEvents: false,
    events: {
        'click .selector': 'handler'
    }
});
var view = new View({foo:'bar'});

addDismissListener(listenerName)

When escape key is pressed or something outside of view.$el is clicked view.listenerName will be invoked.

...
open: function() {
    this.$el.addClass('active');
    this.addDismissListener('close');
}
close: function() {
    this.$el.removeClass('active');
    this.removeDismissListener('close');
}
...

removeDismissListener(listenerName)

Use to remove dismiss listeners. See example above.


addView(view, groupName)

Use to setup parent-child view relationship. Child view is added to parent view group if groupName is specified.

...
initialize: function() {
    this.collection.each(function(model) {
        this.addView(new ChildView, 'itemList');
    }, this);
}
...

getGroupViews(groupName)

Retrieve child views stored in parent group as array.

...
render: function() {
    _.each(this.getGroupViews('itemList'), function(subView) {
        subView.render();
    });
}
...

removeViews(viewGroup)

Removes all sub views. If viewGroup is specified removes only group views.

parentView.removeViews('itemList');

remove()

Does extended cleanup and triggers "beforeRemove" and "afterRemove" events on view instance.

view.remove();

getViewByModel(model)

Get parent sub-view by providing it's model instance.

var childView = parentView.getViewByModel(model);

removeViewByModel(model)

Close sub-view by providing it's model instance.

parentView.removeViewByModel(model);

hasView(childView)

Check if parent view has child sub-view.

console.log(parentView.hasView(childView));

detachView()

Detach view from parent sub-view registry.

childView.detachView();

attachToView(parentView, group)

Detach view from parent sub-view registry and attach to another view.

childView.attachToView(parentView)

appendTo(view)

Append view.$el to another view.$el. Other available methods are 'prependTo', 'insertBefore', 'insertAfter'.

view.appendTo(parentView);

when(resources, doneCallback, failCallback)

Shortcut for $.when with default context set to view instance for all callbacks. Additionally adds all deferreds to view instance deferreds stack so effective cleanup can be performed on view removal. Accepts resources as single or array of deferreds.

Installation

Backbone base view is packaged as UMD library so you can use it in CommonJS and AMD environment or with browser globals.

npm install backbone-base-view --save
// with bundlers
var BaseView = require('backbone-base-view');

// with browser globals
var BaseView = window.BaseView;