Giter Site home page Giter Site logo

backbone.nativeview's Introduction

Backbone.NativeView

A drop-in replacement for Backbone.View that uses only native DOM methods for element selection and event delegation. It has no dependency on jQuery.

To Use:

Load Backbone.NativeView with your favorite module loader or add as a script tag after you have loaded Backbone in the page. Wherever you had previously inherited from Backbone.View, you will now inherit from Backbone.NativeView.

var MyView = Backbone.NativeView.extend({
  initialize: function(options) {
    // ...
  }
});

As an alternative, you may extend an existing View's prototype to use native methods, or even Backbone.View itself:

var Backbone.View = Backbone.View.extend(Backbone.NativeViewMixin);

var MyView = Backbone.View.extend({
  initialize: function(options) {
    // ...
  }
});

Features:

Delegation:

var view = new MyView({el: '#my-element'});
view.delegate('click', view.clickHandler);

Undelegation with event names or listeners,

view.undelegate('click', view.clickHandler);
view.undelegate('click');

View-scoped element finding:

// for one matched element
_.first(view.$('.box')).focus();

// for multiple matched elements
_.each(view.$('.item'), function(el) {
  el.classList.remove('active')
});
var fields = _.invoke(view.$('.field'), 'innerHTML');

Requirements:

NativeView makes use of querySelector/querySelectorAll. For IE7 and below you must include a polyfill.

Notes:

  • The $el property no longer exists on Views. Use el instead.
  • The $ method returns a NodeList instead of a jQuery context. You can iterate over either using _.each.

With many thanks to @wyuenho for his initial code.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.