Code Monkey home page Code Monkey logo

backbone.subviews's Introduction

Backbone.Subviews

A minimalist view mixin for creating and managing subviews in your Backbone.js applications.

This plugin is designed to manage a fixed number of subviews. See Backbone.CollectionView for a plugin to manage a dynamic number of subviews (i.e. an array of subviews).

Benefits

  • Provides a clear syntax for subviews in templates: <div data-subview="mySubview"></div>
  • Automatically puts references to subviews in a hash by name: this.subviews.mySubview
  • Maintains subview objects when a parent view is re-rendered, preserving subview state.
  • Automatically cleans up (i.e. removes) subviews when a parent view is removed.
  • Promotes small encapsulated ui components that can be reused.
  • Can be mixed into any view class.

BONUS: Use Backbone.Subviews in conjunction with Backbone.Courier and Cartero / Parcelify for a completely modularized backbone.js experience.

Example

In a view template, insert a subview "mySubview" using a placeholder div with a data-subview attribute:

<script type='text/template' id="MyItemViewTemplate">
	<h1>This is my item view template</h1>

	<div data-subview="mySubview"></div>
</script>

Now in MyItemView.js:

MyItemViewClass = Backbone.View.extend( {
	initialize : function() {
		// Add backbone.subview functionality to this view.
		Backbone.Subviews.add( this );
	},

	subviewCreators : {
		"mySubview" : function() {
			var options = {};

			// Do any logic required to create initialization options,
			// then instantiate and return the new subview object.
			return new MySubviewClass( options );
		}
	},

	render : function( data ) {
		// `render` funciton is just like normal.. nothing new here.
		var templateFunction = _.template( $( "#MyItemViewTemplate" ).html() );
		this.$el.html( templateFunction( data ) );

		// After we are done rendering, our subviews will automatically be rendered in order
	},

	onSubviewsRendered : function() {
		// This method (if it exists) is called after subviews are finished rendering.
		// Anytime after subviews are rendered, you can find the subviews in the `subviews` hash

		this.listenTo( this.subviews.mySubview, "highlighted", this._mySubview_onHighlighted );
	},

	...
} );

Usage

To insert a subview, just put <div data-subview="[subviewName]"></div> in the appropriate place in the parent view's template. This placeholder div will be completely replaced with the subview's DOM element.

Then include an entry for the subview in the subviewCreators hash. The key of each entry in this hash is a subview's name, and the value is a function that should create and return the new subview.

After the parent view's render function is finished, the subviews will automatically be created and rendered (in the order their placeholder divs appear inside the parent view). Once all subviews have been created and rendered, the parent view's onSubviewsRendered method is called (if one exists).

When a parent view is re-rendered, its subviews will be re-rendered (i.e. their render function will be called). By default the original subview objects will by reused in order to preserve subview state. To force the subview objects to be recreated instead of reused, call parentView.removeSubviews() before re-rendering the parent.

A parent view will automatically remove all its subviews when its remove method is called.

Template Helpers

To further simplify the syntax for inserting subviews in your templates, add a global template helper to your template language of choice. For example, with underscore.js templates, the underscore-template-helpers mixin can be used to support this syntax:

<script type='text/template' id="MyItemViewTemplate">
	<h1>This is my item view template</h1>

	<%= subview( "mySubview" ) %>
</script>

Change log

v0.7.3 (5/23/14)

  • Re-factored a bit so that the mechanism used to create subviews can be overriden if desired.

v0.7.2 (4/13/14)

  • Improved UMD wrapper to use DOM library already attached to jquery.

v0.7.1 (4/6/14)

  • Relax div restriction on subview placeholder - placeholders can now be of any type of element.

v0.7 (2/26/14)

  • UMD wrapper and package.json added.

v0.6 (1/10/14)

  • Renamed _onSubviewsRendered to onSubviewsRendered. (Old name depreciated but still works for now.)
  • Added view.removeSubviews() method.

backbone.subviews's People

Contributors

bitterjug avatar bradyemerson avatar colinwren avatar dgbeck avatar elliottjohnson avatar go-oleg avatar joyvuu-dave avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

backbone.subviews's Issues

Pass id and class name from mark-up to view creator function

The template of the parent view has the placeholder div, and is responsible to place the div in the correct place in parent view. That is, the parent view owns the styling of the placeholder div itself, and the subview owns the mark-up and styling of anything inside the subview. (Separation of concern.)

Backbone allows this by the special options, e.g. id, and class, that can be passed and attached to the subview upon creation.

For backbone.subviews, it would be great if we, in a more declarative way, can allow the placeholderDiv to accept the id and class attributes and pass them to the creator function.

Repeating subviews

I've started using your plugin to cleanup a semi-large backbone project. An issue I've run into is how to declare a dynamic number of repeating subviews.

I have multiple lists of items, where every item has their own view and model. Here is an issue from layoutmanager addressing the same problem: https://github.com/tbranyen/backbone.layoutmanager/issues/19

Perhaps I could dynamically add properties to the subviewCreators hash? Likely in the parant views init method, before calling render?

Backbone 1.2.0 support

Currently you have "backbone": "~1.1.0" in your dependencies section in bower.json file. Is it possible to update it to >=1.1.0,1.* for example?

Use Backbone.$ reference instead of global $

Hi,

I am playing with a Browserify setup but I got an error that the global $ can not be found.

Adding a var $ = Backbone.$; just at the module start (before Backbone.Subviews) would fix this, but maybe you want to clean/remove the global jquery dependency too.

Pass parameters from markup to subviewCreator

Hi,
I started using this plugin, and I was missing the ability to pass params (in the shape of attributes, inner html, or whatever) to the subviewCreators function.
for example, I have a generic list view, and I want to set its items from within the markup, for example:

<div data-subview="mySubview" data-num-items="2">
  <li>item 1</li>
  <li>item 2</li>
</div>

I think that's possible if you send thisPlaceHolderDiv as a param in subviewCreator.apply() (line 80).

Use as a minimal layout manager?

Hi,

I was wondering if this plugin could be used as a minimum layout view, where subviews are added and removed dynamically?

How would I do this view.add(someView) dynamically?

Problem with underscore template helper

I'm not sure if this is a problem with backbone.subviews or underscore-template-helpers but when I add the subview helper and use it in a template I get the following error: Uncaught ReferenceError: subview is not defined

Pass thisPlaceHolderDiv to subviewCreator function

I'd like my subview creator to be able to get settings from the template, in other data- properties. Thus, I'd like the post-processor to send me those settings from the template.

Reconfiguration of already-generated subviews need not be supported.

allow lookups of subview creators to be a function call

When doing a lookup for subview creators, the lookup table can be returned by a function call, and the entries in the table are functions. But there's no way for the lookup itself to be a function call, as it's currently a hard-coded [] lookup.

I understand that the data-subview attribute is intended to be a lookup key in a static table. I was instead wishing to have a different strategy available, where that key provides a classname, and the div's id= attribute provides the unique key for identifying the subview. WDYT?

Available via NPM?

Thanks for this!

Is this library available via NPM?

If not, then this is a feature request...

:)

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.